Spring 3 Form Validation适用于@NotNull,但不适用于@Size注释

时间:2014-04-06 15:21:22

标签: spring validation

我遇到了Spring表单验证的问题。 它适用于@NotNull注释,但有些不适用于@Size。我在下面附上一些代码。提前谢谢。

    package com.doctor;
    import javax.validation.constraints.NotNull;
    import javax.validation.constraints.Size;

    public class Doctor 
    {
        @NotNull(message="Cannot be Null")
        private String uname;


        @Size(min=1,max=8,message="Min 1 and Max 8")
        private String password;
        private String doctor_fname,doctor_lname,address,dept_id,experience,email,phone,resume,image;

        public String getUname() {
            return uname;
        }


        public void setUname(String uname) {
            this.uname = uname;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }
....................
...................

DoctorController

package com.doctor;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.Model;
import javax.validation.Valid;

@Controller
public class DoctorController {


    @RequestMapping(value="/registerDoctor", method=RequestMethod.GET)
    public String showRegisterForm(Model model) 
    {
        System.out.println("test");
        model.addAttribute(new Doctor());
        //return "register1";
        return "doctor/edit";
    }
    @RequestMapping(value="/registerDoctor", method=RequestMethod.POST)
    public String addDoctorformForm(@Valid Doctor doctor,BindingResult bindingresult) 
    {
        if(bindingresult.hasErrors())
        {
            return "doctor/edit";

        }
        else
        {               
            return "doctor/added";
        }


    }
}

显示页面

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


    <h2>Create a Doctor account</h2>
    <sf:form method="POST" modelAttribute="doctor"
        enctype="multipart/form-data">
        <fieldset>
            <table>
                <tr>
                    <td><sf:label path="uname">User Name:</sf:label></td>
                    <td><sf:input path="uname"  /><br /> <sf:errors
                            path="uname" /> </td> 
                </tr>

                <tr>
                    <td><sf:label path="password">Password:</sf:label></td>
                    <td><sf:password path="password" showPassword="true" />
                        <br /> <sf:errors
                            path="password" /></td>
                </tr>

                <tr>
                    <td><sf:label path="doctor_fname">First Name:</sf:label></td>
                    <td><sf:input path="doctor_fname" size="15" /><br /> </td>
                </tr>
                <tr>
                    <td><sf:label path="doctor_lname">Last Name:</sf:label></td>
                    <td><sf:input path="doctor_lname" size="15" /><br /></td>
                </tr>
                <tr>
                    <td><sf:label path="address">Address:</sf:label></td>
                    <td><sf:input path="address" size="15" /><br /></td>
                </tr>
                <tr>
                    <td><sf:label path="dept_id">Department:</sf:label></td>
                    <td><sf:input path="dept_id" size="15" /><br /></td>
                </tr>
                <tr>
                    <td><sf:label path="experience">Experience:</sf:label></td>
                    <td><sf:input path="experience" size="15" /><br /></td>
                </tr>
                <tr>
                    <td><sf:label path="email">Email Address:</sf:label></td>
                    <td><sf:input path="email" size="30" /> <small>In case
                            you forget something</small><br /> </td>
                </tr>
                <tr>
                    <td><sf:label path="phone">Phone:</sf:label></td>
                    <td><sf:input path="phone" size="30" /> <small>In case
                            you forget something</small><br /> </td>
                </tr>
                <tr>
                    <td><label for="resume">Resume:</label></td>
                    <td><input name="resume" type="file" />
                </tr>
                <tr>
                    <td><label for="image">Profile image:</label></td>
                    <td><input name="image" type="file" />
                </tr>

                <tr>
                    <th></th>
                    <td><input name="commit" type="submit">
                </tr>
            </table>
        </fieldset>
    </sf:form>

Output

我不明白为什么@Size密码无效。

帮助将受到高度赞赏。

2 个答案:

答案 0 :(得分:2)

这是因为null是从@Size角度来看的有效值。

您需要两个注释:

@Size(min=1,max=8,message="Min 1 and Max 8")
@NotNull
private String password;

BTW:您的表单是一个没有文件上传的简单表单,所以不需要`enctype =“multipart / form-data” - 我会将其删除

答案 1 :(得分:1)

问题似乎是你正在使用多部分表单,但我不认为我看到有关multipartResolver的提及。如果需要,可以添加它

<bean id="multipartResolver class="org.springframework.web.multipart.commons.CommonsMultipartResolver">