简体   繁体   English

在 Struts2 中登录期间验证获取 NullPointerException 的方法

[英]Validate method getting NullPointerException during login in Struts2

I am doing programmatic validation, when I click the submit button, the validate method has been called, but it shows NullPointerException .我正在做编程验证,当我单击提交按钮时,验证方法已被调用,但它显示NullPointerException It won't validate, but I am unable to find my mistake.它不会验证,但我无法找到我的错误。

截屏


index.jsp:索引.jsp:

<%@ taglib uri="/struts-tags" prefix="s"%>

<s:actionerror />
<s:form action="insert">
    <s:textfield name="" key="user.name" />
    <s:textfield name="" key="user.pass" />
    <s:submit value="login" />
</s:form>

UserLogin.java:用户登录.java:

package jack;

import com.opensymphony.xwork2.ActionSupport;

public class UserLogin extends ActionSupport {

private String username;
private String password;

//getter and setter method

@Override
public void validate() {
    System.out.println("validate");
    if (this.username.equals("") || username.length() == 0) {
        this.addFieldError(username, getText("user.wrong"));
    }
    if (this.password.equals("") || password.length() == 0) {
        this.addFieldError(password, getText("pass.wrong"));
    }
}

@Override
public String execute() throws Exception {
    System.out.println("execute");
    return SUCCESS;
}
}

struts.xml: 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="a" extends="struts-default">
        <action name="insert" class="jack.UserLogin">
            <result name="success">/success.jsp</result>
            <result name="error">/error.jsp</result>
            <result name="input">/index.jsp</result>
        </action>
    </package>
</struts>

struts.properties: struts.properties:

 struts.custom.i18n.resources=Mybundle

Mybundle.properties: Mybundle.properties:

user.name=username
user.pass=password
user.wrong=enter username
pass.wrong=enter password

Frame work is unable set values for your variables, your jsp should be as below框架无法为你的变量设置值,你的jsp应该如下

<s:textfield name="username" key="user.name" />
<s:textfield name="password" key="user.pass" />

Hope this solves your issue.希望这能解决您的问题。

You have a null pointer exception on line 29 of UserLogin.java.您在 UserLogin.java 的第 29 行有一个空指针异常。 Assuming that this is the line:假设这是这条线:

if (this.username.equals("") || username.length() == 0) {

It means that username is null.这意味着username名为空。 You can't call a method (in this case equals ) of a variable (in this case username ) that is null.您不能调用 null 变量(在本例中为username )的方法(在本例中为equals )。 You will get exactly the error as described.您将得到与所描述的完全相同的错误。 What's happening is that struts is not properly assigning values to these variables.发生的事情是 struts 没有正确地为这些变量赋值。 You need to fix that first.你需要先解决这个问题。

Before validation check for null in validate method.在验证之前检查 validate 方法中的 null。 See below.见下文。

if(this.username!=null && this.password != null) 

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

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