4

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?

Roman C
  • 49,761
  • 33
  • 66
  • 176
gmeka
  • 4,269
  • 3
  • 25
  • 25

2 Answers2

2

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

Note: To map a method attribute you should have DMI turned on.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • 2
    You can also use action annotations over xml if you prefer. An action class can have it's methods exposed with dynamic method invocation in a couple ways: https://cwiki.apache.org/WW/action-configuration.html#ActionConfiguration-DynamicMethodInvocation I just like to make distinct actions and just use namespace and action attributes of the s:submit button. – Quaternion Jan 22 '13 at 23:32
  • @Roman C: so I have to write three tags with same name="userAction" and different methods. In that case all the methods return success. how will struts know the response is from save() or export()? – gmeka Jan 22 '13 at 23:33
  • When you map an action, there is a result. If you use DMI (dynamic method invocation) then the mapping for the action applies (that result will be used) but you use a different method other than execute. If you want a different result per method then the method should be mapped as an action. A single action class can support multiple actions, you just need to define them with xml or annotations. – Quaternion Jan 23 '13 at 02:05
  • 1
    @javabie If you configure ` – Roman C Jan 23 '13 at 10:39
1

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>
Aleksandr M
  • 24,264
  • 12
  • 69
  • 143