简体   繁体   English

Spring MVC、Hibernate 验证、表单请求未验证

[英]Spring MVC, Hibernate Validation, Form request is not validated

I need some help about validations in spring mvc 4.3 and hibernate.我需要一些有关 spring mvc 4.3 和 hibernate 验证的帮助。 I have imported "hibernate-validator-6.0.3.Final.jar" and "validation-api-2.0.0.Final.jar" (part of "hibernate-validator-6.0.3.Final.jar").我已经导入了“hibernate-validator-6.0.3.Final.jar”和“validation-api-2.0.0.Final.jar”(“hibernate-validator-6.0.3.Final.jar”的一部分)。

When i fill form with data (username and password), username and password is not validated so request point to index当我用数据(用户名和密码)填写表单时,未验证用户名和密码,因此请求指向索引

User.java用户.java

package invoice.user;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

public class User {

    @NotNull(message = "required")
    @Size(min = 3, max = 50, message = "Username incorrect")
    private String username;

    @NotNull(message = "required")
    @Size(min = 3, max = 50, message = "Password incorrect")
    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;
    }

    @Override
    public String toString() {
        return "User{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

UserController.java用户控制器.java

package invoice.user;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.validation.Valid;

@Controller
public class UserController {

    @RequestMapping("/")
    public String showLoginPage(@ModelAttribute("user") User user){
        return "user/user-login";
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String processLoginForm(@Valid @ModelAttribute("user") User user, BindingResult results){
        if(results.hasErrors()){
            return "user/user-login";
        }
            return "user/index";
    }
}

In UserController , showLoginPage will accept all the request and returns user/user-login .remove this then checkUserController 中showLoginPage将接受所有请求并返回user/user-login .remove this 然后检查

@Controller
public class UserController {

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String processLoginForm(@Valid @ModelAttribute("user") User user, BindingResult results){
        if(results.hasErrors()){
            return "user/user-login";
        }
            return "user/index";
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM