3

Is there any way to populate multiple forms with Struts and make them available to the JSP page?

I'm building a page that has two different forms on it that needs custom data pre-populated from a database. Both forms have to be on the same page.

Roman C
  • 49,761
  • 33
  • 66
  • 176
MechaMarinara
  • 620
  • 1
  • 7
  • 22

2 Answers2

3

Yes, it's possible.

You could specify multiple ActionForm implementations for this issue (preferred) or use only one - no matter.

<nested:root name="myFirstForm">
    <nested:form action="/MyAction.do?method=foo" method="post">
        <%-- some code --%>
    </nested:form>
    <nested:form action="/MyAction.do?method=bar" method="post">
        <%-- some code --%>
    </nested:form>
</nested:root>
<nested:root name="mySecondForm">
    <nested:form action="/MyAction.do?method=foobar" method="post">
        <%-- some code --%>
    </nested:form>
</nested:root>

From Apache Struts Newbie FAQ:

Q: Do I have to have a separate ActionForm bean for every HTML form?

A: This is an interesting question. As a newbie, it is a good practice to create a new ActionForm for each action sequence.

bsiamionau
  • 8,099
  • 4
  • 46
  • 73
3

When you call the struts action the only one form is created by the framework. It's the form that is associated with the action. This form is available via parameter supplied with the execute method.

However, the JSP may contain multiple forms to do multiple actions. If the action dispatch to the JSP then only one form would be processed to map the tags to the properties of the form bean.

However, nothing prevents you to create other form instances in the action and process it manually in the JSP via EL expressions. Instances of the form beans better place in the request or session by the form name, so it will be easily accessible via EL expressions.

Roman C
  • 49,761
  • 33
  • 66
  • 176