绑定结果始终为假Spring MVC

lxkprmvk  于 2022-11-14  发布在  Spring
关注(0)|答案(3)|浏览(101)

我正在尝试在我的mvc应用程序中实现spring验证框架。我的登录页面有用户名和密码,这是必需的。我在模型类中将它们定义为非null和非空。如果它们留空,我希望spring将错误绑定到BindingResult对象。我不确定我哪里做错了.. BindingResult的hasError()方法在我的控制器类中总是返回false。
下面是我的登录控制器

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

下面是我的模型类..

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

}

我的spring xml有这些条目...

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

下面是我的登录jsp页面..

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

我的控制器类中的bindingResult.hasErrors()方法总是返回false。你能告诉我,我遗漏了什么吗?
pom具有以下必需条目..

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

rbl8hiat1#

应使用此依赖项

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

删除依赖项javax。

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

pw136qt22#

在pom.xml中尝试

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

mf98qq943#

尝试使用spring-boot-starter-validation

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

相关问题