Pass a bean like a property of another bean (struts 1.x)

◇◆丶佛笑我妖孽 提交于 2019-12-12 01:33:23

问题


I have two forms (and the corresponding form beans), one on page1.jsp and another on page2.jsp (this second form is dinamically created via json). The form1 is of mypackage.MyActionForm1 type, while the form2 is of mypackage.MyActionForm2 type. In the Action executed when the first form is submitted, I create a MyActionForm2 and set the MyActionForm1 form as its property:

MyActionForm2 secondBean = new MyActionForm2();
secondBean.setBeanProp(form1);
request.setAttribute("secondbean", secondBean);

In the jsp I succeed in accessing the properties of form1:

${secondbean.beanProp.prop1}

but how pass the first bean to the Action executed when the second form, form2, is submitted?

form2.getBeanProp().getProp1() // form2.getBeanProp() gives a NullPointerException

I wouldn't to use session scope.


回答1:


If you don't have a hidden field for every property of your form2.getBeanProp() bean, then obviously Struts won't be able to reconstruct this bean from thin air.

To be able to populate the property prop1 of the bean returned by actionForm.getBeanProp(), you need a hidden field named beanProp.prop1. If you don't want to have the whole state of beanProp in your HTML form as hidden fields, then store this bean in session.



来源:https://stackoverflow.com/questions/13585948/pass-a-bean-like-a-property-of-another-bean-struts-1-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!