Java Reference
In-Depth Information
Figure 15.4: Response after clicking the Cancel button
Ajax and Validation Errors
When the user enters a money amount in the text field, it must be a
valid integer value. What happens if the user enters something invalid?
The whole form gets redisplayed in the 'result' element, producing the
“double vision” effect illustrated in Figure 15.5 , on the following page.
Why did this happen? Remember that when a validation error occurs,
Stripes returns to the form instead of executing the event handler. That
response is put back in the 'result' element by our Ajax response han-
dler, so the form reappears. The validation error is not shown because
we don't have the <s:errors/> tag in the form.
What we want to do is avoid returning the whole page and instead
return just the fragment that displays the validation errors:
Download ajax/web/WEB-INF/jsp/errors.jsp
<s:errors/>
All that's left to do is implement ValidationErrorHandler in the action bean
to override the default behavior of returning the source page resolu-
tion when validation errors occur. Instead, we'll return a resolution to
errors.jsp :
Download ajax/src/stripesbook/action/ErrorHandlingActionBean.java
public class ErrorHandlingActionBean extends BaseActionBean
implements ValidationErrorHandler
{
private static final String ERRORS = "/WEB-INF/jsp/errors.jsp";
public Resolution handleValidationErrors(ValidationErrors errors) {
return new ForwardResolution(ERRORS);
}
}
After submitting an invalid value, the error message is now shown
within the page, as illustrated in Figure 15.6 , on the next page. That's
much better.
 
 
 
Search WWH ::




Custom Search