How to detect submit button clicked in multiple submit buttons scenario in single Action class? [duplicate]

You can define two actions in struts.xml file and use action attribute of <s:submit> tag in order to submit to different actions http://struts.apache.org/docs/submit.html.

In JSP:

<s:submit value="Search" action="searchEmployeeAction"/>
<s:submit value="Add New" action="addEmployeeAction"/>

In struts.xml:

<action name="addEmployeeAction" method="add" class="example.EmployeeAction">
    <result>/example/add.jsp</result>
</action>

<action name="searchEmployeeAction" method="search" class="example.EmployeeAction">
    <result>/example/search.jsp</result>
</action>

And in your action create two public String methods add and search.

Read about Multiple Submit Buttons http://struts.apache.org/docs/multiple-submit-buttons.html.

Update

Starting from Struts2 version 2.3.15.3 you need to set struts.mapper.action.prefix.enabled constant to true in order to enable support for action: prefix.

Put that in your struts.xml file:

<constant name="struts.mapper.action.prefix.enabled" value="true" />

Leave a Comment