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