public class SearchCriteria extends FrameworkBean {
@NotEmpty(message = "Event type may not be null")
@Size(min = 4, max = 4, message = "EventType must be length four")
private String eventType;
This is not a good idea. Last month I demonstrated how to access Spring resource bundles from within your JSP. This blog builds on that idea and demonstrates how to add error messages to your property files that are related to your JSR 303 annotations, and can be displayed by your JSPs. All this sounds complicated, but it isn’t, it’s just not that well documented. Assuming that you’re already using a ResourceBundleMessageSource, then all you need to do is to add a few entries into your property file - one for each annotation on each attribute of each command object. The format is:
{constraint-name}.{command-object-name}.{attribute-name}=My Error Message Text
Given the Java code snippet above, then we can add the following to our property file for the eventType attribute of the searchCriteria command object:
NotEmpty.searchCriteria.eventType=Event type may not be empty Size.searchCriteria.eventType=EventType must be four characters
...which, when we have a validation error, will give us the following screen:
In this case, the error message has been displayed using Spring’s form:errors tag:
<form:errors path="eventType" />
No comments:
Post a comment