Popeye Popeye - 6 months ago 16
Java Question

Spring + Hibernate Validation error

I am trying to implement Hibernate+Spring Validation to my application,but there is one issue, it won't display error message on page after I submit faulty data. Here is code:

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>





Controller

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


Thanks for help!

Answer

I found problem in my registerSubmit method I didn't mention @ModelAttribute, so code for method should look like:

@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";
}
Comments