black sensei
black sensei

Reputation: 6677

Validation form with spring and freemarker

i've started using spring (3.0.5) form recently, with hibernate validator(4.0.2.GA). my validation works alright i could show the error message that are from hibernate validator. how to show other messages like : username and password mismatch and other messages? here is my form :

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

my controller looks like this :

 @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.");
    }
 }

can anyone shed some light on how to achieve it? thanks for reading this.

Upvotes: 0

Views: 4663

Answers (1)

alexeuka
alexeuka

Reputation: 11

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

I hope this will help.

Upvotes: 1

Related Questions