Java – spring MVC form errors have not been processed on JSP

I'm trying to validate the login form After clicking the login button, the control enters my controller class. With the help of bindingresult, I also know that there is an error in my field, but I can't display the error on my JSP page I've tried the rejectvalue method, but my JSP page throws an exception, that is, the attribute is unreadable e.t.c

This is my modeling class

public class UserBean
{
  @NotEmpty
  private String username;
  @NotEmpty
  private String password;

  public String getpassword()
  {
    return this.password;
  }

  public String getUsername()
  {
    return this.username;
  }

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

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

This is my JSP page

<form:form method="POST" modelattribute="login-entity"
    action="process-login.html">
    <table>
        <tr>
            <td><form:label path="username">username:</form:label></td>
            <td><form:input path="username" /></td>
            <td><form:errors path="username" /></td>
        </tr>
        <tr>
            <td><form:label path="password">password:</form:label></td>
            <td><form:input path="password" /></td>
            <td><form:errors path="password" /></td>
        </tr>
        <tr>
            <td colspan="2"><input type="submit" value="Login" /></td>
        </tr>
    </table>
</form:form>

This is my controller class

@RequestMapping(value = "/process-login",method = RequestMethod.POST)
  public ModelAndView processLogin(@Valid UserBean loginModel,BindingResult bindingResult,Model model)
  {
    if(bindingResult.hasErrors())
    {
      ModelAndView errorView = new ModelAndView("loginpage");
      bindingResult.rejectValue("username","invalid username");
      bindingResult.rejectValue("password","invalid password");
      errorView.addObject("login-entity",bindingResult.getModel());
      return errorView;
    }
    return new ModelAndView("redirect:/some_other_page","","");
  }

When debugging, my final error view object contains this

ModelAndView: reference to view with name 'loginpage'; model is {login-entity={userBean=UserBean [userId=0,username=,password=],org.springframework.validation.BindingResult.userBean=org.springframework.validation.BeanPropertyBindingResult: 4 errors
Field error in object 'userBean' on field 'username': rejected value []; codes [NotEmpty.userBean.username,NotEmpty.username,NotEmpty.java.lang.String,NotEmpty]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [userBean.username,username]; arguments []; default message [username]]; default message [may not be empty]
Field error in object 'userBean' on field 'password': rejected value []; codes [NotEmpty.userBean.password,NotEmpty.password,NotEmpty]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [userBean.password,password]; arguments []; default message [password]]; default message [may not be empty]
Field error in object 'userBean' on field 'username': rejected value []; codes [invalid username.userBean.username,invalid username.username,invalid username.java.lang.String,invalid username]; arguments []; default message [null]

Can I help you? I think I missed something very basic

Solution

Redirect from controller to When the JSP page and assigns an error message to it, you can add attributes to the request (ServletRequest. SetAttribute (string VAR1, object var2);) Then, in your JSP page, find the properties and process the error message accordingly

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>