如何检测单个Action类中多个提交按钮场景中单击的提交按钮? [英] How to detect submit button clicked in multiple submit buttons scenario in single Action class?

查看:122
本文介绍了如何检测单个Action类中多个提交按钮场景中单击的提交按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在jsp中有一个表单。有两个提交按钮:搜索和添加新按钮。

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>

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>

在Struts Action类中,我们知道只有一种处理http请求的方法,即执行()方法。

In Struts Action class, we know that there is only one method that processing http request, that is execute() method.

在我预期的情况下,当我点击搜索按钮时,它将执行搜索数据并将数据呈现到 /example/search.jsp ,当我点击添加新按钮时,它将执行重定向页面到 /example/add.jsp 。但是,单击时两个按钮都将进入execute()方法。所以我需要知道如何检测在 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. 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.

场景看起来像这个

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


推荐答案

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

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.

在JSP中:

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

在struts.xml中:

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>

在您的操作中创建两个 public String 方法添加搜索

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

了解多次提交按钮 http://struts.apache.org/docs/multiple-submit-buttons .html

更新

从Struts2版本2.3开始.15.3您需要将 struts.mapper.action.prefix.enabled 常量设置为true才能启用对操作的支持:前缀。

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.

将其放入 struts.xml 文件中:

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

这篇关于如何检测单个Action类中多个提交按钮场景中单击的提交按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆