samedi 11 avril 2015

spring @Valid annotation not working

When i added @Valid annotation at that time post method doesn't call and without @Valid annotation, post method is call. I don't know why this happen? my code is below.


Bean.java



public class Bean {

@NotEmpty(message="firstname is required.")
private String firstname;

@NotEmpty(message="lastname is required.")
private String lastname;

//getter and setter method
}


Controller.java



@RequestMapping(value="/add", method=RequestMethod.POST)
public String addForm(ModelMap model, @Valid @ModelAttribute(value="bean") Bean bean,
BindingResult result) {

if(result.hasErrors())
return "registration";

return "registration";
}


registration.jsp



<div>
<c:url value="/merchant/handleRegistration" var="registrationURL"></c:url>
<form:form action="${registrationURL}" id="registrationForm" method="POST" modelAttribute="bean">
<form:errors path="*" element="div" />
<table>
<tr>
<td><form:label path="firstname">Firstname* : </form:label></td>
<td><form:input path="firstname" cssClass="required" /></td>
<td><form:errors path="firstname" cssClass="error" /></td>
</tr>
<tr>
<td><form:label path="lastname">Lastname* : </form:label></td>
<td><form:input path="lastname" cssClass="required" /></td>
<td><form:errors path="lastname" cssClass="error" /></td>
</tr>
<tr>
<td colspan="3"><input type="submit" value="Register" ></td>
</tr>
</table>
</form:form>

Aucun commentaire:

Enregistrer un commentaire