简体   繁体   English

如何在单个Action类中检测单击多个提交按钮场景中的提交按钮?

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

I have a form in a jsp. 我在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 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. 在Struts Action类中,我们知道只有一个处理http请求的方法,即execute()方法。

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 . 在我预期的情况下,当我单击“ 搜索”按钮时,它将执行搜索数据并将数据呈现到/example/search.jsp ,当我单击“ 添加新按钮”时,它将执行将页面重定向到/example/add.jsp However, both buttons when clicked will go into execute() method. 但是,单击时两个按钮都将进入execute()方法。 So I need to know how to detect which button clicked in the execute() method. 所以我需要知道如何检测execute()方法中单击的按钮。

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";
    }
}

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 . 您可以在struts.xml文件中定义两个操作,并使用<s:submit>标记的action属性,以便提交不同的操作http://struts.apache.org/docs/submit.html

In JSP: 在JSP中:

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

In struts.xml: 在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 . 并在您的操作中创建两个public String方法addsearch

Read about Multiple Submit Buttons http://struts.apache.org/docs/multiple-submit-buttons.html . 阅读有关多个提交按钮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. 从Struts2的版本2.3.15.3开始,你需要设置struts.mapper.action.prefix.enabled不变,以便能够支持到真正的action:前缀。

Put that in your struts.xml file: 把它放在你的struts.xml文件中:

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

In your model layer, define a String property named 'button'. 在模型层中,定义名为“button”的String属性。 Now, for both your submit buttons, specify name or property attribute as 'button'. 现在,对于两个提交按钮,将nameproperty属性指定为“button”。 So, in your execute() method, in property 'button', you will get the corresponding value. 因此,在您的execute()方法中,在属性“button”中,您将获得相应的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM