在 Struts 动作类中,来自 jsp 的输入值为空.如何在 Action 类中访问注册表单输入值 [英] Input values from jsp are null in Struts action class. How to access the registration form input values in Action class

查看:18
本文介绍了在 Struts 动作类中,来自 jsp 的输入值为空.如何在 Action 类中访问注册表单输入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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"><头><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>在此处插入标题</title><身体><中心><%@ taglib uri="/struts-tags" prefix="s"%><s:form action="getRegistered.action" method="post" validate="true"><div><表格><s:textfield label="First Name" key="firstname"/><s:textfield label="Last Name" key="lastname"/><s:password label="创建您的密码" 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>

</s:form></中心></html>

我正在将寄存器输入值传递给 Action 类.

Struts.xml:<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE 支柱公共"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"http://struts.apache.org/dtds/struts-2.0.dtd"><支柱><package name="default" extends="struts-default"><action name="getLogin" class="login.action.LoginAction"方法=登录"><result name="success">/Success.jsp</result><result name="input">/LoginError.jsp</result></动作><action name="getRegistered" class="login.action.LoginAction"方法=注册"><interceptor-ref name="validation"><param name="excludeMethods">register</param></interceptor-ref><result name="success">/Success.jsp</result><result name="input">/login.jsp</result></动作></包></支柱>

这是struts xml

LoginAction.java:包 login.action;导入 javax.servlet.http.HttpServletRequest;导入 javax.servlet.http.HttpServletResponse;导入 org.apache.struts2.interceptor.ServletRequestAware;导入 org.apache.struts2.interceptor.ServletResponseAware;导入 com.opensymphony.xwork2.ActionSupport;导入 login.service.LoginDao;导入 login.service.RegisterDao;@SuppressWarnings("串行")公共类 LoginAction 扩展 ActionSupport 实现 ServletRequestAware,ServletResponseAware {私人字符串用户名;私人字符串密码;私有 HttpServletRequest httpServletRequest;私人字符串名字;私人字符串姓氏;私人字符串注册密码;私人字符串密码;私人字符串regemail;私人字符串conemail;私人字符串电话;公共字符串寄存器(){RegisterDao rdao = new RegisterDao();System.out.println("firstname action::: "+firstname);rdao.registerdao(firstname,lastname,regpassword,conpassword,regemail,conemail,phone);返回成功;}公共字符串登录(){httpServletRequest.getSession().setAttribute("key", username);httpServletRequest.getSession().setAttribute("key", password);LoginDao db = new LoginDao();Boolean validate = db.loginresult(username, password);如果(验证 == 真){返回成功;} 别的 {返回输入;}}公共 HttpServletRequest getHttpServletRequest() {返回 httpServletRequest;}public void setHttpServletRequest(HttpServletRequest httpServletRequest) {this.httpServletRequest = httpServletRequest;}公共字符串 getFirstname() {返回名字;}public void setFirstname(String firstname) {this.firstname = 名字;}公共字符串 getLastname() {返回姓氏;}public void setLastname(String lastname) {this.lastname = 姓氏;}公共字符串 getRegpassword() {返回注册密码;}public void setRegpassword(String regpassword) {this.regpassword = regpassword;}公共字符串 getConpassword() {返回密码;}public void setConpassword(String conpassword) {this.conpassword = conpassword;}公共字符串 getRegemail() {返回regmail;}public void setRegemail(String regemail) {this.regemail = regemail;}公共字符串 getConemail() {返回锥形邮件;}公共无效setConemail(字符串conemail){this.conemail = 锥体邮件;}公共字符串 getPhone() {回电话;}公共无效setPhone(字符串电话){this.phone = 电话;}公共无效 setServletRequest(HttpServletRequest 请求){this.httpServletRequest = 请求;}公共字符串 getUsername() {返回用户名;}公共无效设置用户名(字符串用户名){this.username = 用户名;}公共字符串 getPassword() {返回密码;}public void setPassword(字符串密码){this.password = 密码;}@覆盖公共无效 setServletResponse(HttpServletResponse arg0) {//TODO 自动生成的方法存根}}

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

以上问题已解决.

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

我该如何解决?

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

 <interceptor-ref name="defaultStack"><param name="validation.excludeMethods">登录</param></interceptor-ref><result name="success">/Success.jsp</result><result name="input">/LoginError.jsp</result></动作>

解决方案

您只使用了一个 Interceptor,您需要使用整个 Stack:

改变这个:

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

到这里

<param name="validation.excludeMethods">注册</param></interceptor-ref>

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>

I'm passing the register input values to Action class.

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>

This is the 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

    }



}

When i submit the registration form , the input values pass to Action class is null. How can i access the registration values in action class under register() method.

The above issue is resolved .

Now i'm facing another strange issue .. When i submit the form action = getLogin.action , its always returning INPUT from the interceptor . I dont wish to exclude the method login from the validator.

How could i resolve it ?

When i change the struts xml like below , its redirecting to success jsp . But i dont want to exclude the login method from validation

       <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>

解决方案

You are using only one Interceptor, you need to use the whole Stack:

Change this:

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

to this

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

这篇关于在 Struts 动作类中,来自 jsp 的输入值为空.如何在 Action 类中访问注册表单输入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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