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

北城以北 提交于 2019-11-26 11:24:25

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

In your model layer, define a String property named 'button'. Now, for both your submit buttons, specify name or property attribute as 'button'. So, in your execute() method, in property 'button', you will get the corresponding value.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!