Spring + Hibernate验证错误

时间:2016-05-06 05:59:46

标签: java spring hibernate validation

我正在尝试将Hibernate + Spring Validation实现到我的应用程序,但是有一个问题,在我提交错误数据后它不会在页面上显示错误消息。这是代码:

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;


public class StudentVO {

private int studentID;
private String userName;

@NotNull
@Size(min=2, max=45, message="Your Name should be between 2 - 45 characters.")
private String firstName;

private String middleName;

@NotNull
@Size(min=2, max=45, message="Your Surname should be between 2 - 45 characters.")
private String lastName;

JSP

<form:form id="myform" modelAttribute="student" method="post"  class="form-horizontal" action="/register/registerStudent">
<div class="form-group">
<label for="firstname" class="col-sm-3 control-label">First Name</label>
    <div class="col-sm-6">
        <form:input type="text" class="form-control float_left" id="firstname" path="firstName" placeholder="First Name" />
        <form:errors path="firstName"></form:errors>
      </div>
 </div>
  <div class="form-group">
  <label for="middlename" class="col-sm-3 control-label" id="md">Middle Name</label>
      <div class="col-sm-6">
          <form:input type="text" class="form-control" id="middlename" path="middleName" placeholder="Middle Name" />
      </div>
</div>
<div class="form-group">
 <label for="lastname" class="col-sm-3 control-label">Last Name</label>
     <div class=" col-sm-6">
         <form:input type="text" class="form-control float_left"   id="lastname" path="lastName" placeholder="Last Name" required="true" />
          <form:errors path="lastName"></form:errors>
     </div>

    

控制器

@RequestMapping(value = { "/register/registerStudent" }, method = { RequestMethod.GET, RequestMethod.POST })
public String registerSubmit(@Valid StudentVO student, BindingResult result, Model model, HttpServletRequest request,
        @RequestParam String action, @RequestParam(value = "parentID", required = false) String parentID,
        @RequestParam(value = "studentID", required = false) String studentID,
        RedirectAttributes redirectAttributes) {
    if(result.hasErrors()){
        model.addAttribute("student", student);
        List<Map<String, String>> age = lookupService.getFields("age");
        model.addAttribute("age", age);
        return "studentSignup";
    }

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我在registerSubmit方法中发现问题我没有提到@ModelAttribute,因此方法的代码应如下所示:

@RequestMapping(value = { "/register/registerStudent" }, method = { RequestMethod.GET, RequestMethod.POST })
public String registerSubmit(@Valid  @ModelAttribute StudentVO student, BindingResult result, Model model, HttpServletRequest request,
    @RequestParam String action, @RequestParam(value = "parentID", required = false) String parentID,
    @RequestParam(value = "studentID", required = false) String studentID,
    RedirectAttributes redirectAttributes) {
if(result.hasErrors()){
    model.addAttribute("student", student);
    List<Map<String, String>> age = lookupService.getFields("age");
    model.addAttribute("age", age);
    return "studentSignup";
}