user5017804
user5017804

Reputation:

BindingResult always false Spring MVC

I'm trying to implement spring validation framework in my mvc application. My login page has user name and password which are mandatory. I defined them as not null and not empty in the model class. If they are left blank I expect spring to bind the errors to BindingResult object. I'm not sure where I'm doing it wrong.. the hasError() method of BindingResult always return false in my controller class.

Below is my login controller

public class LoginController {

@Autowired
private UserRoleService userRoleService;

@Autowired
private AuthenticationService authenticationService;

@RequestMapping(value = "/login", method=RequestMethod.GET)
public ModelAndView showLoginPage() {
    System.out.println("coming into LoginController!!");
    ModelAndView mav = new ModelAndView("login");
    mav.addObject("userModel", new UserInfo());
    return mav;
}

@RequestMapping(value = "/welcome", method=RequestMethod.POST)
public ModelAndView login(@Valid @ModelAttribute("userModel") UserInfo user, BindingResult bindingResult) {
    System.out.println("authenticate username and password -- invoke LDAP!!");
    System.out.println(user.getUsername() + user.getPassword());
    String userId = user.getUsername();
    String password = user.getPassword();

    if(bindingResult.hasErrors())
    {
        return new ModelAndView("login");
    }

    if(authenticationService.athenticate(userId,password)) {            
        List<String> roles = userRoleService.searchRolesByUserId(userId);           
        for(String role : roles)
            System.out.println("role is: "+role);
        ModelAndView mav = new ModelAndView("welcome");
        return mav;
    }
    else {
        ModelAndView mav = new ModelAndView("login");
        mav.addObject("loginFailed", "User Name or Password is incorrect");
        return mav;
    }
}
}

Below is my model class..

public class UserInfo{

@NotNull
@NotEmpty(message="User name cannot be empty")
private String username;

@NotNull
@NotEmpty(message="Password cannot be empty")
private String password;

public String getUsername() {
    return username;
}

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

public String getPassword() {
    return password;
}

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

}

My spring xml has these entries...

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
    <property name="prefix">
        <value>/views/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>
<mvc:resources mapping="/resources/**" location="/WEB-INF/resources/"/>
<mvc:annotation-driven />

Below is my login jsp page..

<form:form class="form-signin" commandName="userModel" method="POST" action="welcome.do" id="loginform">

    <h2 class="form-signin-heading">Welcome to BPAY</h2>

    <div style="color: red">${loginFailed}</div>                                        

            <label for="inputEmail" class="sr-only">Username</label> 

            <form:input type="text" path="username" id="usernameId" class="form-control"
                placeholder="Username" required autofocus/>
            <form:errors path="username" cssClass="error"/>

            <label for="inputPassword" class="sr-only">Password</label> 

            <form:input type="password" id="passwordId" path="password" class="form-control"
                placeholder="Password" required/>
            <form:errors path="password" cssClass="error"/>

            <button class="btn btn-lg btn-primary btn-block" type="submit" value="OK">Login</button>                

    </form:form>

The bindingResult.hasErrors() method in my controller class always return false. Can you please let me know, what am I missing?

pom has required entries as below..

<!-- Validation framework -->
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>1.1.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>5.3.5.Final</version>
    </dependency>

Upvotes: 0

Views: 3129

Answers (3)

Michael . _ .
Michael . _ .

Reputation: 19

Try using spring-boot-starter-validation:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-validation</artifactId>
</dependency>

Upvotes: 0

Eduardo Diniz
Eduardo Diniz

Reputation: 1

Try in pom.xml

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
</dependency>

Upvotes: 0

Thai Nguyen
Thai Nguyen

Reputation: 224

You should use this dependency

`<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>6.0.10.Final</version>
</dependency>`

And remove the dependency javax.validation

<dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
    <version>1.1.0.Final</version>
</dependency>

Upvotes: 2

Related Questions