如何使用Struts2中的Bean将对象从操作类传递到JSP? [英] How to pass object from action class to JSP using Bean in Struts2?

查看:144
本文介绍了如何使用Struts2中的Bean将对象从操作类传递到JSP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将Bean类的对象从我的动作类发送回JSP.我正在我的动作类中实例化Bean类,并在我的动作类中设置一些值.

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"); 

在我的JSP中,我也声明了bean类.

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>

在bean类中,我用public getter和setter声明了变量private. 但是在JSP中,这些值变为空. 调试后,我发现动作类没有问题.但是当命令转到bean类时,值将变为null.

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.

推荐答案

第一次在操作中实例化bean,第二次在JSP中实例化.它与您在操作中填充的bean不同,并且不包含值.如果要从第一个bean获取值,则应为包含bean实例的变量创建变量.您还应该创建更改器以访问该bean的属性.在JSP中,您可以访问第一个bean,因为操作实例位于valueStacktop上.只需使用

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" />

请注意,没有#的OGNL不会搜索其他变量,它将直接进入valueStack.但是不要在bean标记内执行此操作,因为新的(第二个)实例被推到了valueStack,并且搜索是从上到下执行的,因此它可以与bean的属性匹配.只需删除bean标签.

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.

这篇关于如何使用Struts2中的Bean将对象从操作类传递到JSP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆