当输入值为空时,java验证会转换NPE,从而导致内部服务器错误

时间:2016-07-08 07:11:57

标签: java spring jsp spring-mvc

我希望在MultiActionControllers上实现验证,

我有pojo,pojo验证器,控制器和jsp,

当我在用户名和密码字段中键入一些文本时没有任何反应,但是当一个字段为空时,显示并且错误显示带有错误文本的jsp,我尝试使用commandName和modelAttribute,代码如下:

我得到的错误就是这个:

org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [dispatcher] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.beans.NotReadablePropertyException: Invalid property 'USERNAME' of bean class [me.example.to.Login]: Bean property 'USERNAME' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?] with root cause
 org.springframework.beans.NotReadablePropertyException: Invalid property 'USERNAME' of bean class [me.example.to.Login]: Bean property 'USERNAME' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?

登录POJO

    public class Login 
{

    private String username;  
    private String password;

    public Login()
    {}

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

登录验证员

public class AdmLoginValidator implements Validator

{

@Override
public boolean supports( Class<?> clazz ) 
{
    return Login.class.isAssignableFrom( clazz );
}//

@Override
public void validate( Object target, Errors errors ) 
{

    Login bean = (Login) target;
    //code to validate the form salary field
    if (bean.getUsername().equals(""))
    errors.rejectValue("USERNAME", "error.salary.exceed.limit", "Salary      can't be greater than 2000000");

}//

} //类

控制器类:

public class AdmLoginController extends BaseController
{


      public ModelAndView login(HttpServletRequest request, HttpServletResponse response) 
                throws Exception 
        {
            return new ModelAndView("/jspadmin/admlogin","login",new Login() );
        }//


      public ModelAndView auth(
              HttpServletRequest request, 
              HttpServletResponse response,
              Login admLogin) 
              throws Exception 
        {

            ServletRequestDataBinder binder = new ServletRequestDataBinder( admLogin, "login" );

            System.err.println( "mostrando login" );
            System.err.println( admLogin.getUsername() + " - " + admLogin.getPassword() );

            //set the validator
            binder.setValidator( new AdmLoginValidator() );
            binder.validate();

            if( binder.getBindingResult().hasErrors()  
                    && binder.getBindingResult().getErrorCount() > 0 )
            {
            BindingResult br = binder.getBindingResult();
            return new ModelAndView( 
                    "/jspadmin/admlogin",
                    "/jspadmin/admlogin", 
                    admLogin ).addAllObjects( br.getModel() );
            }//
           return new ModelAndView( "/jspadmin/admdashboard" );

        }//

}//class

JSP(仅限重要代码)

<%@page import="org.springframework.validation.Errors"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
...

 <form:form method="post" action="/admin/auth.html" modelAttribute="login"  >
<!--                    <input type="text" name="username" placeholder="Username">
                    <input type="password" name="password" placeholder="Password">-->
                                        <form:input path="username" />
                                        <form:input path="password" />
                    <input type="submit" name="login" class="login loginmodal-submit" value="Login">
                  </form:form>

0 个答案:

没有答案