@Size Validator在我的Spring Project中不起作用

时间:2017-09-09 12:25:35

标签: java spring spring-mvc

您好我正在尝试将验证放在我的spring项目中,但它对我不起作用我还在我的lib文件夹中添加了所有必需的验证器jar。

我的控制器类看起来像

package qm;

import java.sql.Date;
import java.text.SimpleDateFormat;

import javax.validation.Valid;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class StudentAdmissionController {

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.setDisallowedFields(new String[] {"studentSkill","studentDOB"});
        SimpleDateFormat dateFormat=new SimpleDateFormat("dd-mm-yyyy");
        binder.registerCustomEditor(Date.class,"studentDOB", new CustomDateEditor(dateFormat, false));
    }

    @RequestMapping(value="/registration.html",method=RequestMethod.GET)
    public ModelAndView getStudentAdmissionForm() {
        ModelAndView model=new ModelAndView("AdmissionForm");

        return model;
    }

    @ModelAttribute
    public void addCommonObject(Model model) {
        model.addAttribute("headerMessage","Q-Manager.com");
    }

    @RequestMapping(value="/submit-detail.html",method=RequestMethod.POST)
    public ModelAndView studentDetail(@Valid @ModelAttribute("student1") Student student,BindingResult result) {
        if(result.hasErrors()) {
            ModelAndView model=new ModelAndView("AdmissionForm");
            return model;
        }       
        ModelAndView model=new ModelAndView("AdmissionSuccess");
        return model;

    }


}

我的学生班

package qm;

import java.util.ArrayList;
import java.util.Date;

import javax.validation.constraints.Size;

public class Student {

    @Size(min=2,max=30)
    private String studentName;

    private String studentHobby;
    private Long studentMobile;
    private Date studentDOB;
    private ArrayList<String> studentSkill;
    private Address studentAddress;

    public String getStudentName() {
        return studentName;
    }
    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }
    public String getStudentHobby() {
        return studentHobby;
    }
    public void setStudentHobby(String studentHobby) {
        this.studentHobby = studentHobby;
    }
    public Long getStudentMobile() {
        return studentMobile;
    }
    public void setStudentMobile(Long studentMobile) {
        this.studentMobile = studentMobile;
    }
    public Date getStudentDOB() {
        return studentDOB;
    }
    public void setStudentDOB(Date studentDOB) {
        this.studentDOB = studentDOB;
    }
    public ArrayList<String> getStudentSkill() {
        return studentSkill;
    }
    public void setStudentSkill(ArrayList<String> studentSkill) {
        this.studentSkill = studentSkill;
    }
    public Address getStudentAddress() {
        return studentAddress;
    }
    public void setStudentAddress(Address studentAddress) {
        this.studentAddress = studentAddress;
    }


}

我的地址类

package qm;

public class Address {

    private String country,city,street,pincode;

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

    public String getPincode() {
        return pincode;
    }

    public void setPincode(String pincode) {
        this.pincode = pincode;
    }

}

,这是我的AdmissionForm.jsp

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<html>
    <head>
        <title>Student Form</title>
    </head>
    <body>
            <center><h4>${headerMessage}</h4></center>
            <center><h4>Student Admission Form</h4></center>
            <center>
                <form:errors path="student1.*"/>
            </center>
            <form action="submit-detail.html" method="post">
            <table>
                <tr>
                        <td>Student Name</td>
                        <td><input type="text" name="studentName"/></td>
                </tr>
                <tr>
                        <td>Hobby</td>
                        <td><input type="text" name="studentHobby"/></td>
                </tr>
                <tr>
                        <td>Student Mobile</td>
                        <td><input type="text" name="studentMobile"/></td>
                </tr>
                <tr>
                        <td>DOB</td>
                        <td><input type="text" name="studentDOB" value="May 04 09:51:52 CDT 2009"/></td>
                </tr>
                <tr>
                        <td>Skills</td>
                        <td>
                            <select name="studentSkill" multiple>
                                <option value="Core java">Core java</option>
                                <option value="Spring Core">Spring Core</option>
                                <option value="Spring MVC">Spring MVC</option>
                            </select>   
                        </td>
                </tr>


            </table>



            <table>

                <tr><td>Student Address</td></tr>
                <tr><td>Country<input type="text" name="studentAddress.country"/></td></tr>
                <tr><td>City<input type="text" name="studentAddress.city"/></td></tr>
                <tr><td>Street<input type="text" name="studentAddress.street"/></td></tr>
                <tr><td>Pin Code<input type="text" name="studentAddress.pincode"/></td></tr>

                <tr>    
                        <td><input type="submit" value="Submit"/></td>
                </tr>

            </table>


            </form>

    </body>
</html>

任何人都建议我在哪里错了所以我会在我以前的研究中纠正我的代码我发现大多数人建议关于罐子,即需要所以我在春天定义所有必需的罐子进行验证。

  1. classmate-1.0.0 jars
  2. hibernate-validator-annotation-processor-5.1.2.Final jar
  3. jboss-logging-3.1.3.ga jar
  4. validation-api-1.1.0.final
  5. 我使用了Spring 4.3。* jars这个项目

2 个答案:

答案 0 :(得分:0)

您需要在Spring调度程序中添加mvc注释标签。

 <mvc:annotation-driven/>

答案 1 :(得分:0)

尝试在pom.xml中添加以下依赖项

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