Struts2 handle multiple actions in single form

2019-01-24 18:10发布

I have a form contains three buttons print/export/save.

<s:form action="/userAction">
    <s:submit type="image" value="%{'print'}" src="/print.png" />
    <s:submit type="image" value="%{'export'}" src="/export.png" />
    <s:submit type="image" value="%{'save'}" src="/save.png" />
</s:form>

How can I map this in struts.xml?

2条回答
The star\"
2楼-- · 2019-01-24 18:51

In order to use method attribute of the <s:submit> tag DynamicMethodInvocation must be enabled. Another solution is to use action attribute.

In JSP:

<s:form action="save">
    <s:submit type="image" value="%{'print'}" src="/print.png" action="print" />
    <s:submit type="image" value="%{'export'}" src="/export.png" action="export" />
    <s:submit type="image" value="%{'save'}" src="/save.png" />
</s:form>

In struts.xml:

<action name="print" class="...">
  <result>...</result>
</action>
<action name="export" class="...">
  <result>...</result>
</action>
<action name="save" class="...">
  <result>...</result>
</action>
查看更多
萌系小妹纸
3楼-- · 2019-01-24 18:58

In the struts.xml the action is mapped via the <action tag

<action name="userAction" class="...

the submit buttons should include method attribute to call corresponding methods of the action

<s:submit type="image" value="%{'print'}" src="/print.png" method="print" />
<s:submit type="image" value="%{'export'}" src="/export.png" method="export" />
<s:submit type="image" value="%{'save'}" src="/save.png" method="save" />
查看更多
登录 后发表回答