How to detect the submit button clicked in multiple submit button scenarios in a single action class?
I have a form in JSP There are two submit buttons: search and add new
<s:form name="searchForm" action="employeeAction" method="post">
<s:textfield name="id" label="Employee ID"/>
<s:textfield name="name" label="Employee Name"/>
<s:submit value="Search"/>
<s:submit value="Add New"/>
</s:form>
In struts In XML
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<default-action-ref name="index" />
<global-results>
<result name="error">/error.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="java.lang.Exception" result="error"/>
</global-exception-mappings>
</package>
<package name="example" namespace="/example" extends="default">
<action name="employeeAction" class="example.EmployeeAction">
<result name="search">/example/search.jsp</result>
<result name="add" type="redirect">/example/add.jsp</result>
</action>
</package>
</struts>
In the struts action class, we know that there is only one method to process HTTP requests, namely the execute () method
In my expectation, when I click the search button, it will perform a search for data and render the data to / example / search JSP, which will redirect the page to / example / add. JSP when I click the "add new button" jsp. However, when clicked, both buttons enter the execute () method So I need to know how to detect the button clicked in the execute () method
The scene looks like this
public class EmployeeAction extends ActionSupport {
public String execute() throws Exception {
//PSEUDOCODE
//IF (submitButton is searchButton)
// return doSearch();
//ELSE IF (submitButton is addNewButton)
// return doAddNew();
return SUCCESS;
}
public String doSearch() throws Exception {
//perform search logic here
return "search";
}
public String doAddNew() throws Exception {
return "add";
}
}
Solution
You can use struts Two operations are defined in the XML file and the operation attribute of < s: submit > is used Mark for submission to http://struts.apache.org/docs/submit.html Different operations of
In jsp:
<s:submit value="Search" action="searchEmployeeAction"/> <s:submit value="Add New" action="addEmployeeAction"/>
In struts In 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 create two public string methods to add and search in your operation
Read about multiple submit buttons http://struts.apache.org/docs/multiple-submit-buttons.html Information about
to update
From struts 2 version 2.3 15.3 to start, you need to add struts mapper. action. prefix. The enabled constant is set to true to enable support for action: prefix
Put it in your struts In the XML file:
<constant name="struts.mapper.action.prefix.enabled" value="true" />
