• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

struts setting value to a bean which is called by reflections in action class

 
Greenhorn
Posts: 3
PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

In struts action I have a string property which has getters and setters. Setting this property value from the jsp form is easy. Its the same case with a java bean with set of properties. In my case I load a simple java class with few properties with respective getters and setters on runtime using reflections and put it in a hashmap property of the action class which is pushed to value stack. On the jsp page I am able to read the values on the java class from hashmap but not able to set the properties using the post/get method.

A simple class

public class ClassB {
private String name;
private String language;
public void setName(String name)
{
this.name=name;
}
public String getName()
{
return name;
}
public void setLanguage(String language)
{
this.language=language;
}
public String getLanguage()
{
return language;
}
}

-----------------------------------------------

package org.test;

import java.util.Map;

public class ClassA {
public ClassA()
{

}
public String doTask(Map<String, Object> data){
System.out.println("Running some task");
ClassB classB=new ClassB()
classB.setName("JavaRanch");
data.put("CLASSB", classB);
}
}

--------------------------------------------------------------------------

Struts action class

public class ActionClass implements SessionAware {
private Map data=new HashMap();
public String execute(){
callTaskAction("ClassA");
return "SUCCESS";
}
public void callTaskAction(String taskAction)
{
try {
Class <?> actionClass = Class.forName("org.test."+taskAction);
Object actionObject=actionClass.newInstance();
setActionResult(actionClass.getMethod("doTask",new Class[]{Map.class}).invoke(actionObject,data).toString());
} catch (Exception e) {
e.printStackTrace();
}
}
public Map getData() {
return data;
}
public void setData(Map data) {
this.data = data;
}
}

------------------------------------------------------------------

jsp page

output.jsp

<s:property value="#data["CLASSB"].name"/> <!-- prints JavaRanch -->

<form method="POST" action="mappednameinstruts.xml">
<input type="text" name="#data["CLASSB"].language" value="JAVA"/> <!-- does not set language to java-->
<input type="submit" value="submit"/>
</form>



Any suggestion would be greatly appreciated.

Regards,
Naveen
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Naveen Kumar Idss wrote:On the jsp page I am able to read the values on the java class from hashmap but not able to set the properties using the post/get method.




This value will not be submitted because it is not a form tag.



I think the problem here is that the name of the data element gets processed by struts and becomes the value rather than the location. See our Struts FAQ for an example.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic