0

我最近开始使用 spring (3.0.5) 形式,带有休眠验证器(4.0.2.GA)。我的验证工作正常我可以显示来自休眠验证器的错误消息。如何显示其他消息,例如:username and password mismatch和其他消息?这是我的表格:

   <form action="loginprocess" method="post">
       <h1>Log in</h1>
        <@spring.bind "loginForm" />
        <@spring.showErrors '*', 'errors' />
        <p>
           <label class="uname" data-icon="u"> UserName :</label>
           <@spring.formInput "loginForm.username","placeholder='eg. myusername'" />
           <@spring.showErrors "loginForm.username","error" />
        </p>
        <p>
           <label class="youpasswd" data-icon="p">Password :</label>
           <@spring.formPasswordInput "loginForm.password","placeholder='eg. @dakdjlkdja'"  />
           <@spring.showErrors "loginForm.password", "error" />
        </p>

    <input type="submit" style="background:#000; border: solid 1px #000;" value="Login"/>
        <p class="change_link">
          Not a member yet ?
        <a href="/site/authentication/signup/form" class="to_register">Sign up</a>
        </p>
  </form>

我的控制器看起来像这样:

 @RequestMapping(value = "/site/authentication/login")
public ModelAndView nativeLogin(){
    LoginForm loginForm = new LoginForm();
   return  new ModelAndView("login","loginForm",loginForm);
}

@RequestMapping(value = "/site/authentication/loginprocess")
public ModelAndView processNativeLogin(@ModelAttribute @Valid LoginForm loginForm, BindingResult result, Model model){
    if(result.hasErrors()){

        return new ModelAndView("login","loginForm",loginForm);
    }

    try {
        Subject currentUser = SecurityUtils.getSubject();

        if (!currentUser.isAuthenticated()) {
            UsernamePasswordToken token = new UsernamePasswordToken(loginForm.getUsername(), loginForm.getPassword());
            SecurityUtils.getSubject().login(token);
        }

    } catch (UnknownAccountException uae) {
   // how to set this message to show on the form ?"Login Failed! Please check your credentials and try again.");
    } catch (IncorrectCredentialsException ice) {
   //"Login Failed! Please check your credentials and try again.");
    }
 }

谁能阐明如何实现它?感谢您阅读本文。

4

1 回答 1

1
<#if Session.SPRING_SECURITY_LAST_EXCEPTION?? && Session.SPRING_SECURITY_LAST_EXCEPTION.message?has_content>
            <@spring.message "label.loginerror"/>: ${Session.SPRING_SECURITY_LAST_EXCEPTION.message}
</#if>

我希望这将有所帮助。

于 2012-08-15T10:18:54.987 回答