I have to send object of Bean class back to JSP from my action class. I am instantiating the Bean class in my action class and and setting some values in my Action class.
ElasticitiesVariable elasticitiesVariable = new ElasticitiesVariable();
elasticitiesVariable.setAuto_Auto_cost_peak("-0.047");
elasticitiesVariable.setAuto_Ride_cost_peak("0.000");
elasticitiesVariable.setAuto_Van_cost_peak("0.000");
elasticitiesVariable.setAuto_Transit_cost_peak("0.050");
elasticitiesVariable.setAuto_Bike_cost_peak("0.000");
and in my JSP I am declaring the bean class as well.
<s:bean name="org.apache.struts.register.model.ElasticitiesVariable" var="elasticitiesBean" />
<td class="edit_area">
<s:property value="#elasticitiesBean.auto_Auto_cost_peak" /></td>
in the bean class i have declared the variable private with public getter and setter.
But the values coming null in JSP.
After debugging I found that there is no problem in action class. But when command goes to bean class, values becomes null
.
First time you instantiate the bean in the action, second time in the JSP. It's not the same bean you've populated in the action, and doesn't contain the values. If you want to get the values from the first bean then you should create mutators to a variable that hold the instance of the bean. You should also create mutators to access the properties of that bean. In the JSP you have access to the first bean because the action instance is on the top
of the valueStack
. Just use
<s:property value="elasticitiesBean.auto_Auto_cost_peak" />
Note, that without #
OGNL will not search the other variables, it will look directly into the valueStack
. But don't do it inside the bean
tag, because the new (second) instance is pushed to the valueStack
and the search is performed from top to bottom, thus it could match the properties of the bean. Just remove the bean
tag.
来源:https://stackoverflow.com/questions/17634353/how-to-pass-object-from-action-class-to-jsp-using-bean-in-struts2