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

2

6

I have a form in a jsp. There are two submit buttons: "Search" and "Add New" button.

<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.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 Struts Action class, we know that there is only one method that processing http request, that is execute() method.

In my expected case, when I clicked Search button, it will perform searching data and render data to /example/search.jsp, when I clicked Add New button, it will perform redirecting page to /example/add.jsp. However, both buttons when clicked will go into execute() method. So I need to know how to detect which button clicked in the execute() method.

The scenario 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";
    }
}
Checkbook answered 12/11, 2012 at 12:45 Comment(0)
A
17

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" />
Antlia answered 12/11, 2012 at 12:47 Comment(4)
@AleksandrM: could you give more detailed answers? Like how the struts.xml <action> mapping looks like. About the multiple submit buttons article, I had read that actually :). The first one (using boolean props) is promising, but it didn't work somehow, it seems the button's string label cannot be converted into boolean by struts controller, probably it's used to work in old versions (I'm using struts 2.3). The second one is not good, because of the tight coupling with the buttons label.Checkbook
@AleksandrM: Thanks. How about the action attribute in <s:form> in the jsp? What value should I put? When I removed the action attribute, there is exception when I hit the submit buttons.Checkbook
Put one of your actions. Consider this like a default for you form. E.g. if you will use javascript to submit the form that action will be used.Antlia
@AleksandrM: alright, it's working, thanks. Btw, don't forget to use return SUCCESS; in search() and add() method because your <result> has no name.Checkbook
P
6

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.

Pyrethrin answered 12/11, 2012 at 12:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.