java—数据验证失败时请求Map方法未运行(SpringMVC)

5fjcxozz  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(244)

我目前正在学习springmvc,数据验证看起来很简单,我的问题是:当我输入无效的数据并按下submit按钮时,请求Map方法根本不会启动,它会将我发送到“http status 400–bad request”。我的目标是使用“bindingresult”查看是否有任何错误,但如果输入的数据无效,则方法体不会运行(使用hibernate validatior)。这里有一些代码。
用户对象:

public class User {
    @NotNull
    @Size(min = 4, max = 25, message = "Must be between 4 and 25 characters")
    private String firstName;

    @NotNull
    @Size(min = 4, max = 25, message = "Must be between 4 and 25 characters")
    private String lastName;

    @Email
    private String email;

    @Size(min = 8)
    private String userPassword;
}

控制器:打开窗体页

@RequestMapping("createAccount")
public String register(Model model) {
    model.addAttribute("newUser", new User());
    return "register";
}

问题来了。这个方法应该在我按下submit按钮时运行,当数据有效时,它会返回userinteface.jsp和所有输入的数据,但是当输入的数据无效时,它会直接将我发送到http status 400-bad request。

@RequestMapping("signUp")
public String signUp(@Valid @ModelAttribute("newUser") User user,
    Model model, BindingResult bindingResult) {

    model.addAttribute("dbUser", user);

    if (bindingResult.hasErrors()) return "register";

    return "userinterface";
}

这是我的register.jsp

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html>

    <html>

    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>Register</title>
        <meta name="description" content="An interactive getting started guide for Brackets.">
        <link rel="stylesheet" type="text/css" href="resources/css/main.css" />
        <link href="https://fonts.googleapis.com/css2?family=Nova+Round&display=swap" rel="stylesheet">
    </head>

    <body>

    <div class = "container">

        <h1>- Register -</h1>

        <ul>

            <form:form action ="signUp" modelAttribute="newUser">

                <form:input path="firstName" class = "cred" placeholder="First Name"/>
                <form:errors path="firstName" cssClass="error"/><br><br>

                <form:input path="lastName" class = "cred" placeholder="Last Name"/><br><br>
                <form:errors path="lastName" cssClass="error"></form:errors><br><br>

                <form:input path="email" class = "cred" placeholder="E-mail"/>
                <form:errors path = "email" cssClass="error"/><br><br>

                <form:password path="userPassword" class = "cred" placeholder="Password"></form:password><br><br>
                <form:errors path="userPassword" cssClass="error"/>

                <form:input path="age" type = "number" class = "cred" placeholder="Age"/><br><br>

                - <input type ="submit" class = "submitButton" value = "Add User"> -<br><br>
                <a href = index.jsp>cancel</a>

            </form:form>

        </ul>

    </div>

    </body>

    </html>

mnemlml8

mnemlml81#

问题出在方法参数上。我已经把用户,模型,绑定结果。其中应该是user、bindingresult、model,所以基本上绑定结果必须紧跟在user对象之后。
道路:

@RequestMapping("signUp")
public String signUp(@Valid @ModelAttribute("newUser") User user,
                     Model model, BindingResult bindingResult){

正确的方法:

public String signUp(@Valid @ModelAttribute("newUser") User user,
                     BindingResult bindingResult, Model model){

我想知道为什么这些问题总是只发生在我身上!

相关问题