来自jsp的输入值在Struts操作类中为null。如何在Action类中访问注册表单输入值

时间:2014-06-17 08:41:42

标签: struts2

Registration.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <center>
        <%@ taglib uri="/struts-tags" prefix="s"%>
        <s:form action="getRegistered.action" method="post" validate="true">
            <div>
                <table>
                    <s:textfield label="First Name" key="firstname" />
                    <s:textfield label="Last Name" key="lastname" />
                    <s:password label="Create your password" key="regpassword" />
                    <s:password label="Confirm your password" key="conpassword" />
                    <s:textfield label="Email" key="regemail1" />
                    <s:textfield label="Re-Type Email" key="conemail" />
                    <s:textfield label="Phone" key="phone" />   
                    <tr>
                    <td><s:submit value="Register" theme="simple"/></td>
                    <td><s:submit value="Cancel" theme="simple" onclick="document.forms[0].action='login.jsp';" /></td>
                    </tr>
                </table>
            </div>
        </s:form>
    </center>
</body>
</html>

我将寄存器输入值传递给Action类。

Struts.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <package name="default" extends="struts-default">

        <action name="getLogin" class="login.action.LoginAction"
            method="login">
            <result name="success">/Success.jsp</result>
            <result name="input">/LoginError.jsp</result>
        </action>

        <action name="getRegistered" class="login.action.LoginAction"
            method="register">
            <interceptor-ref name="validation">
                <param name="excludeMethods">register</param>
            </interceptor-ref>
            <result name="success">/Success.jsp</result>
            <result name="input">/login.jsp</result>
        </action>

    </package>
</struts>

这是struts xml

LoginAction.java:

package login.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import com.opensymphony.xwork2.ActionSupport;
import login.service.LoginDao;
import login.service.RegisterDao;

@SuppressWarnings("serial")
public class LoginAction extends ActionSupport implements ServletRequestAware,
        ServletResponseAware {

    private String username;
    private String password;
    private HttpServletRequest httpServletRequest;
    private String firstname;
    private String lastname;
    private String regpassword;
    private String conpassword;
    private String regemail;
    private String conemail;
    private String phone;


    public String register(){

        RegisterDao rdao = new RegisterDao();
        System.out.println("firstname action::: "+firstname);
        rdao.registerdao(firstname,lastname,regpassword,conpassword,regemail,conemail,phone);
        return SUCCESS;
    }


    public String login() {
        httpServletRequest.getSession().setAttribute("key", username);
        httpServletRequest.getSession().setAttribute("key", password);
        LoginDao db = new LoginDao();
        Boolean validate = db.loginresult(username, password);
        if (validate == true) {
            return SUCCESS;

        } else {
            return INPUT;
        }
    }

    public HttpServletRequest getHttpServletRequest() {
        return httpServletRequest;
    }

    public void setHttpServletRequest(HttpServletRequest httpServletRequest) {
        this.httpServletRequest = httpServletRequest;
    }

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public String getRegpassword() {
        return regpassword;
    }

    public void setRegpassword(String regpassword) {
        this.regpassword = regpassword;
    }

    public String getConpassword() {
        return conpassword;
    }

    public void setConpassword(String conpassword) {
        this.conpassword = conpassword;
    }

    public String getRegemail() {
        return regemail;
    }

    public void setRegemail(String regemail) {
        this.regemail = regemail;
    }

    public String getConemail() {
        return conemail;
    }

    public void setConemail(String conemail) {
        this.conemail = conemail;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public void setServletRequest(HttpServletRequest request) {
        this.httpServletRequest = request;

    }


    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }



    @Override
    public void setServletResponse(HttpServletResponse arg0) {
        // TODO Auto-generated method stub

    }



}

当我提交注册表单时,传递给Action类的输入值为null。如何在register()方法下访问action类中的注册值。

上述问题已得到解决。

现在我面临另一个奇怪的问题..当我提交表单action = getLogin.action时,它总是从拦截器返回INPUT。我不希望从验证器中排除方法登录。

我怎么解决?

当我像下面那样更改struts xml时,它会重定向到成功jsp。但我不想从验证中排除登录方法

       <action name="getLogin" class="login.action.LoginAction"
        method="login">
                            <interceptor-ref name="defaultStack">
            <param name="validation.excludeMethods">login</param>
        </interceptor-ref>
        <result name="success">/Success.jsp</result>
        <result name="input">/LoginError.jsp</result>
    </action>

1 个答案:

答案 0 :(得分:1)

你只使用一个拦截器,你需要使用整个堆栈:

改变这个:

<interceptor-ref name="validation">
    <param name="excludeMethods">register</param>
</interceptor-ref>

到这个

<interceptor-ref name="defaultStack">
    <param name="validation.excludeMethods">register</param>
</interceptor-ref>