@Size验证器在我的春季项目中无法使用



嗨,我试图将验证放在我的春季项目中,但它对我不起作用,我还将所有必需的验证器jar添加到我的lib文件夹中。

我的控制器类看起来像

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;
    }
}

这是我的AndissionForm.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.2.Final Jar
  3. jboss-logging-3.1.3.ga jar
  4. 验证-api-1.1.0.final

我使用了弹簧4.3。*此项目的罐子

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

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

您需要在Spring Dispatcher中添加MVC注释标签。

 <mvc:annotation-driven/>

最新更新