如何使用验证器类在spring mvc中进行服务器端验证

时间:2017-03-20 09:45:44

标签: spring hibernate validation spring-mvc

你是我对spring mvc的新手我必须使用单独的验证器类进行服务器端验证。我有两个关于一对多关系的模型我必须在注册之前注册它们我是spring mvc中的新服务器端验证使用spring form和所有

require 'conf.inc.php';

控制器

<form id="studentEnrollmentForm" method="post" class="form-horizontal" action="saveStudentByAdmin">

                                        <div class="form-group">
                                            <label class="col-xs-2 control-label">Student Full Name</label>
                                            <div class="group">
                                            <div class="col-xs-3">
                                                <input type="text" class="form-control" name="studentFirstName" id="fn" placeholder="First name" />
                                            </div>
                                            </div>
                                            <div class="col-xs-3">
                                                <input type="text" class="form-control" name="studentMiddleName" placeholder="Middle name" />
                                            </div>
                                            <div class="col-xs-3">
                                                <input type="text" class="form-control" name="studentLastName"  placeholder="Last name" />
                                            </div>
                                        </div>

                                        <div class="form-group">
                                            <label class="col-xs-2 control-label">Parents Full Name</label>
                                            <div class="col-xs-3">
                                                <input type="text" class="form-control" name="parentFirstName" placeholder="First name" />
                                            </div>
                                            <div class="col-xs-3">
                                                <input type="text" class="form-control" name="parentMiddleName" placeholder="Middle name" />
                                            </div>
                                            <div class="col-xs-3">
                                                <input type="text" class="form-control" name="parentLastName" placeholder="Last name" />
                                            </div>
                                        </div>
                                        <div class="form-group">
                                        <label class="col-xs-2 control-label">Date-of-birth</label>
                                            <div class="col-xs-3 ">
                                                <div class="input-group input-append date" id="studentDOB">
                                                    <input type="Text" class="form-control" name="studentDOB" /> <span
                                                        class="input-group-addon add-on"><span
                                                        class="glyphicon glyphicon-calendar"></span></span>
                                                </div>
                                            </div>
                                            <label class="col-xs-3 control-label">Gender</label>
                                            <div class="col-xs-3">
                                                <div class="btn-group" data-toggle="buttons">
                                                    <label class="btn btn-default"> 
                                                    <input type="radio" name="studentGender" value="male" />Male</label>
                                                     <label class="btn btn-default"> 
                                                     <input type="radio" name="studentGender" value="female" />Female</label>
                                                </div>
                                            </div>
                                        </div>

                                        <div class="form-group">
                                            <label class="col-xs-2 control-label">Phone</label>
                                            <div class="col-xs-3">
                                                <input type="text" class="form-control" name="parentPhoneNumber" placeholder="Phone number" />
                                            </div>
                                            <label class="col-xs-3 control-label">Email</label>
                                            <div class="col-xs-3">
                                                <input type="text" class="form-control" name="parentEmail" placeholder="Email" />
                                            </div>
                                        </div>




                                        <div class="form-group">
                                            <label class="col-xs-2 control-label">Permanent Address</label>
                                            <div class="col-xs-3">
                                                <textarea class="form-control" rows="3" name="studentPermanentAddress" /></textarea>
                                            </div>
                                            <label class="col-xs-3 control-label">Present Address</label>
                                            <div class="col-xs-3">
                                                <textarea class="form-control" rows="3" name="studentPresentAddress" /></textarea>
                                            </div>
                                        </div>



                                        <div class="form-group">
                                            <label class="col-xs-2 control-label">Class to join</label>
                                            <div class="col-xs-3">
                                            <!--    <input type="text" class="form-control" name="className" placeholder="Enter Class" /> -->
                                            <select name="className" class="form-control">
                                                    <option value="">Select class </option>
                                                    <option value="1">1</option>
                                                    <option value="2">2</option>
                                                    <option value="3">3</option>
                                                    <option value="4">4</option>
                                                    <option value="5">5</option>
                                                    <option value="6">6</option>
                                                    <option value="7">7</option>
                                                    <option value="8">8</option>
                                                    <option value="9">9</option>
                                                    <option value="10">10</option>                                                  
                                                </select>
                                            </div>
                                        </div>


                                        <div class="form-group">
                                            <div class="col-xs-1 ">
                                                <button type="submit" class="btn btn-primary ">Submit</button>
                                            </div>
                                            <div class="col-xs-1 ">
                                                <button type="reset" class="btn btn-default" id="rstbutton">Refresh</button>
                                            </div>
                                        </div>
                                    </form>

我的验证程序类

@Controller
@RequestMapping("/")
public class AdminRegistrationController {
    @Autowired
    private IAdminRegistrationService  adminRegistrationService;    


     @RequestMapping(value = "/register", method = RequestMethod.GET)
        public String viewRegistrationPage(Model model) {
         StudentDTO studentDTO = new StudentDTO();
         ParentDTO pdto=new ParentDTO();
            model.addAttribute("teacherDTO", studentDTO);
            model.addAttribute("teacherDTO", pdto);
            return "StudentEnrollmentByAdmin";
        }

    @RequestMapping(value = "/saveStudentByAdmin", method = RequestMethod.POST)
    public String saveTeacherByAdmin(@ModelAttribute StudentDTO sdto,@ModelAttribute ParentDTO pdto) {      
        System.out.println(sdto.getStudentFirstName());
        System.out.println(pdto.getParentFirstName());
        return "redirect:/register"; // this line redirecting to above method to avoid same data insertion again when i press f5
        //return "TeacherEnrollmentByAdmin"; To know duplication insertion comment above line and above method current this line
        //when you get response page(after insertion of data) press f5 and see in data base
    }
}

1 个答案:

答案 0 :(得分:0)

StudentDTOParentDTO类中,您可以从javax.validation.constraints包和org.hibernate.validator.constraints包中提供各种验证器注释(如果使用hibernate)。

public class StudentDTO {   
    @Size(min=6, max=12)
    private String firstName;
    @Size(min=6, max=12)
    private String lastName;
   //getter setter
}

然后在控制器方法中添加javax.validation.Valid注释和 org.springframework.validation.BindingResult接口,如下所示。

@RequestMapping(value = "/saveStudentByAdmin", method = RequestMethod.POST)
    public String saveTeacherByAdmin(@Valid @ModelAttribute StudentDTO sdto,@ModelAttribute ParentDTO pdto,
    BindingResult bindingResult) {

 if(bindingResult.hasErrors()) {
    System.out.println(bindingResult.getAllErrors().get(0).getDefaultMessage());
 }
}