Spring MVC表单验证错误结果

时间:2016-05-11 17:36:08

标签: html spring forms validation spring-mvc

我做了一个简单的表单,我正在尝试验证它但我遇到了一些麻烦。这是我的控制者:

ImageView

这是我的DTO课程:

MapFragment

我的表单HTML:

import ninja.majewski.store.forms.ContactDTO;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.validation.Valid;

@Controller
public class MainController {

    @RequestMapping(value = "/contact", method = RequestMethod.GET)
    public String contact(Model model) {
        addBasicInfo(model);

        model.addAttribute("form", new ContactDTO());

        return "contact";
    }

    @RequestMapping(value = "/contact", method = RequestMethod.POST)
    public String contact(Model model, @ModelAttribute("form") @Valid ContactDTO form, BindingResult result) {
        addBasicInfo(model);

        model.addAttribute("form", new ContactDTO());

        // return false
        System.out.println(result.hasErrors());

        if (result.hasErrors()) {
            return "contact";
        } else {
            return "redirect:/home";
        }
    }

}

我的应用配置:

import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;

import javax.validation.constraints.Size;

public class ContactDTO {

    @NotEmpty
    @Size(min = 3)
    private String name;

    @NotEmpty
    @Email
    private String email;


    private String message;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

问题在于即使我提交空表单<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> </body> </html> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <!DOCTYPE html> <html lang="en"> <head> <title>I AM A HORSE</title> <spring:url value="/resources/css/bootstrap.css" var="bootstrapCss"/> <link href="${bootstrapCss}" rel="stylesheet" type="text/css"/> </head> <body> <jsp:include page="parts/header.jsp"/> <jsp:include page="parts/leftMenu.jsp"/> <table> <td> <form:form action="/contact" modelAttribute="form" method="post"> Name: <form:input path="name" id="name"/> <form:errors path="name" cssclass="error"/> <br/> Email: <form:input path="email" id="email"/> <form:errors path="email" cssclass="error"/> <br/> Message: <form:input path="message" id="message"/> <form:errors path="message" cssclass="error"/> <br/> <input type="submit" value="Send message"/> </form:form> </td> </table> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <script src="/resources/js/bootstrap.min.js"></script> </body> </html> 也会<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <mvc:annotation-driven/> <context:component-scan base-package="ninja.majewski"/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean> </beans> 。我在哪里弄错了?

1 个答案:

答案 0 :(得分:1)

@ModelAttribute应该是方法的第一个参数。尝试用Model替换它,并在@ModelAttribute之前声明@Valid注释。

 @RequestMapping(value = "/contact", method = RequestMethod.POST)
public String contact(@Valid @ModelAttribute("form") ContactDTO form, BindingResult result, Model model) {
    addBasicInfo(model);