麻烦在struts 2应用程序中的一个<s:form>中使用多个<s:submit> </s:form> </s:submit>

时间:2014-11-18 11:36:56

标签: struts2

问题出在标题中。我尝试过一个特殊的链接http://struts.apache.org/release/2.3.x/docs/multiple-submit-buttons.html 但没有成功。

这是我的struts.xml代码

<?xml version="1.0" encoding="UTF-8"?>

<!-- The core configuration file for the framework is the default (struts.xml) file
and should reside on the classpath of the webapp (generally /WEB-INF/classes). -->

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <!-- devMode equals mode debug information and reload everything for every request -->
    <constant name="struts.devMode" value="true" />
    <constant name="struts.ui.theme" value="simple" />

    <package name="direstrutsIndex" extends="struts-default">
        <default-action-ref name="indexLogin"/>

        <global-results>
            <result name="error">/error.jsp</result>
        </global-results>

        <!-- all exceptions not caught by the application will lead to error.jsp -->
        <global-exception-mappings>
            <exception-mapping exception="java.lang.Exception" result="error"/>
        </global-exception-mappings>

        <!-- 'index' action leads to the view provided by index.jsp -->
        <action name="indexLogin">
            <result>/loginPage.jsp</result>
        </action>

        <action name="loginAction" class="direstruts.action.LoginAction" method="login">
            <result name="success">/tretas.jsp</result>
            <result name="error">/loginPage.jsp</result>
            <result name="input">/loginPage.jsp</result>
        </action>
        <action name="registerAction" class="direstruts.action.RegisterAction" method="register">
            <result name="success">/tretas.jsp</result>
            <result name="error">/loginPage.jsp</result>
            <result name="input">/loginPage.jsp</result>
        </action>


    </package>

</struts>

...我的行动代码

GenericAction

package direstruts.action;

import com.opensymphony.xwork2.ActionSupport;
import java.util.Map;
import org.apache.struts2.interceptor.SessionAware;

public class GenericAction extends ActionSupport implements SessionAware{
    protected static final long serialVersionUID = 4L;
    protected Map<String, Object> session;

    @Override
    public void setSession(Map<String, Object> session) {
        this.session = session;
    } 
}

我的LoginAction:

package direstruts.action;

import static com.opensymphony.xwork2.Action.SUCCESS;
import direstruts.model.UserBean;

public class LoginAction extends GenericAction {

    @Override
    public void validate() {
        UserBean uB = getUserBean();
        if(uB.getUsername().isEmpty()) {
            addFieldError("userBean.username", "Por favor Insira o Username de Utilizador");
        }
        if(uB.getPassword().isEmpty()) {
            addFieldError("userBean.password", "Por favor Insira a Password de Utilizador");
        }
    }

    public String login() throws Exception {
        System.err.println("Login!");
        if(getUserBean().getAuthentication()) {
            return SUCCESS;
        }
        return ERROR;
    }

    public UserBean getUserBean() {
        if(!session.containsKey("userBean"))
            this.setUserBean(new UserBean());
        return (UserBean) session.get("userBean");
    }

    public void setUserBean(UserBean userBean) {
        session.put("userBean", userBean);
    }
}

我的RegisterAction:

package direstruts.action;

import static com.opensymphony.xwork2.Action.ERROR;
import static com.opensymphony.xwork2.Action.SUCCESS;
import direstruts.model.UserBean;

public class RegisterAction extends GenericAction {

    @Override
    public void validate() {
        UserBean uB = getUserBean();
        if(uB.getUsername().isEmpty()) {
            addFieldError("userBean.username", "Por favor Insira o Username de Utilizador");
        }
        if(uB.getPassword().isEmpty()) {
            addFieldError("userBean.password", "Por favor Insira a Password de Utilizador");
        }
    }

    public String register() throws Exception {
        System.err.println("A registar...");
        if(getUserBean().setNewUser()) {
            return SUCCESS;
        }
        return ERROR;
    }

    public UserBean getUserBean() {
        if(!session.containsKey("userBean"))
            this.setUserBean(new UserBean());
        return (UserBean) session.get("userBean");
    }

    public void setUserBean(UserBean userBean) {
        session.put("userBean", userBean);
    }
}

我的登录页面:

<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%><!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="css/styles.css"> 
        <title>MeeTO</title>
    </head>
    <body>

        <s:form action="loginAction" method="post" >
            <s:text name="Please fill in the form below in order to Register or Log in:" /> <br>
            <s:text name="Username:"/> <s:textfield name="userBean.username" /> <br>
            <s:text name="Password:"/> <s:password name="userBean.password" /> <br>
            <s:submit value="Login" /> <br>
            <s:submit value="Register" action="registerAction" /> <br>
        </s:form>
        <s:fielderror/>
    </body>
</html>

没有执行错误。当我单击“登录”按钮时,会发生什么。登录。但是当我单击“注册”时,它也会登录。不知何故,struts在Register按钮中使用相同的Action(LoginAction)调用相同的方法(登录),我不知道为什么。我不想使用多个表单标签。

1 个答案:

答案 0 :(得分:2)

我只需要在struts.xml文件中放一个常量

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

像Aleksandr M所说,通过指出:

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

非常感谢大家。 (我的第一个Stack Overflow问题BTW:D)