简体   繁体   中英

Spring mvc form error not comng on jsp

I am trying to validate a login form. Upon clicking login button, the control comes to my controller class and with the help of BindingResult i also came to know that my field has errors, but i am unable to show the error back on my jsp page. I have tried rejectValue method , but my jsp page throws exception that property is not readable etc

Here is my model 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;
  }  
}

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

and here 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","","");
  }

Upon debugging, my final error view object contains this thing.

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.java.lang.String,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 someone please help? I think i am missing some very fundamental thing.

从控制器重定向到.jsp页面并向其分配错误消息时,可以向请求添加属性(ServletRequest.setAttribute(String var1,Object var2);)然后在jsp页面中查找属性和句柄相应的错误消息

You'll have to add the @ModelAttribute annotation, and you must not create a new ModelAndView 'cause than the new model will not have the binding result erros. Following should work out for you

 public ModelAndView processLogin(@ModelAttribute("login-entity") @Valid UserBean loginModel,BindingResult bindingResult, Model model)

this way, the errors will become available in the model, and the lookup key will match what's expected in the form:errors tags

About the model part you should simply do something like

  if(bindingResult.hasErrors())
    {
      return null; // calling page
    }

and the messages will be there

You can define first your model attribute just like that,

@ModelAttribute("UserBean")
public UserBean userCriteria() {
    return new UserBean();
}

And from Controller

public ModelAndView processLogin(@ModelAttribute("UserBean") @Valid UserBean loginModel,BindingResult bindingResult, Model model)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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