Reputation: 1473
At the moment I am trying to get to know the correct workflow for form submission/validation/error handling in Spring MVC 3.1. No I got some questions.
Upvotes: 3
Views: 3617
Reputation: 1009
1) In Spring 3.1 you can use RedirectAttributes. They were designed specifically for Post/Redirect/Get screnario. You can see a great example here: Spring - Redirect after POST (even with validation errors)
2) I think JSR-303 validators were meant to be simple, self-reliant, and independent from each other. While it may be possible to write them in such a way that they access other persistence entities, etc - it is not a best practice. I personally check for duplicate emails in the controller. If the email already exists - I add a new FieldError to the BindingResult.
Upvotes: 1
Reputation: 10709
From reference documentation:
1.) Use FlashMap attributes from RequestContextUtils.
2.) When using the MVC namespace a JSR-303 validator is configured automatically assuming a JSR-303 implementation is available on the classpath.Any ConstraintViolations will automatically be exposed as errors in the BindingResult renderable by standard Spring MVC form tags.
3.use path="*" to list all errors
<form:form>
<form:errors path="*" cssClass="errorBox" />
<table>
<tr>
<td>First Name:</td>
<td><form:input path="firstName" /></td>
<td><form:errors path="firstName" /></td>
</tr>
<tr>
<td>Last Name:</td>
<td><form:input path="lastName" /></td>
<td><form:errors path="lastName" /></td>
</tr>
<tr>
<td colspan="3">
<input type="submit" value="Save Changes" />
</td>
</tr>
</table>
</form:form>
Upvotes: 4