我最近开始使用spring(3.0.5)表单,使用hibernate验证器(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.");
}
}
任何人都可以了解如何实现它吗? 感谢您阅读本文。
答案 0 :(得分: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>
我希望这会有所帮助。