简体   繁体   English

Spring MVC验证状态400

[英]Spring MVC validation status 400

I have just started learning Spring and stuck with form validation (There is dao/service and all the default set) 我刚刚开始学习Spring并停留在表单验证上(有dao / service和所有默认设置)

I try to make a validation task to prevent inserting into the field more than 3 symbols 我尝试进行验证任务,以防止在字段中插入超过3个符号

@Size (min = 1, max = 3)

After that I want to insert the form data to DB and output it on my homepage, when the data satisfies my validation and consists of 1 to 3 symbols, then everything is ok, but when these conditions are not met and I try to insert 4 or more symbols, I receive HTTP STATUS 400 . 之后,我想将表单数据插入DB并将其输出到我的主页上,当数据满足我的验证并且由1到3个符号组成时,一切正常,但是当不满足这些条件时,我尝试插入4或更多符号,我收到HTTP STATUS 400 No DB inserts and no view outputs. 没有数据库插入,也没有视图输出。 So I understand that there is validation, however I don't understand why does it show me HTTP STATUS 400 所以我知道这里有验证,但是我不明白为什么它告诉我HTTP STATUS 400

@Controller
public class BookController {

    @Autowired
    private BookService bookService;

    @RequestMapping(value = "addBook", method = RequestMethod.GET)
    public String addUser(Model model) {

        model.addAttribute("user", new ValidationField());
        model.addAttribute("book", new Book());

        return "addBook";
    }

    @RequestMapping(value = "addBook", method = RequestMethod.POST)
    public String addBook( @ModelAttribute("user") @Valid ValidationField validationField, Book book, BindingResult result) {
        /*this.user(book, result);*/

        if (result.hasErrors()) {
            return "addBook";
        }
        this.bookService.addBook(book);

        return "redirect:/";
    }
}

Validation Class 验证类别

public class ValidationField {

    @Size(min = 1, max = 3)
    private String name;

    @Size(min = 1, max = 3)
    private String genre;
}

and View addBook.jsp 并查看addBook.jsp

<%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@taglib prefix="t" tagdir="/WEB-INF/tags" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<t:tamplate>
  <form:form method="post" action="addBook" commandName="book" modelAttribute="user">
      <form:errors path="*" cssClass="alert alert-danger" element="div" />
        <table>
            <tr>
                <td><form:input path="name"/></td>
                <td><form:errors path="name"/></td>
            </tr>
            <tr>
                <td><form:input path="genre" /></td>
                <td><form:errors path="genre"/></td>
            </tr>
            <tr>
                <td colspan="2"> <input type="submit" value="add book"> </td>
            </tr>
        </table>
    </form:form>
</t:tamplate>

You should set your BindingResult to immediately follow your ModelAttribute eg 您应该将BindingResult设置为立即遵循ModelAttribute,例如

public String addBook( @ModelAttribute("user") @Valid ValidationField validationField, BindingResult result, Book book)

Check the section od the doc http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-methods 检查文档的部分http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-methods

Most arguments can be used in arbitrary order with the only exception of BindingResult arguments 可以以任意顺序使用大多数参数,只有BindingResult参数例外

and in continuation 并继续

The Errors or BindingResult parameters have to follow the model object that is being bound immediately as the method signature might have more that one model object and Spring will create a separate BindingResult instance for each of them Errors或BindingResult参数必须跟随立即绑定的模型对象,因为方法签名可能不止一个模型对象,并且Spring将为每个模型对象创建一个单独的BindingResult实例。

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

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