Search code examples
formsjavabeansstruts-1

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


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.


Solution

  • 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.