Spring MVC parameter validation
Posted
by Don
on Stack Overflow
See other posts from Stack Overflow
or by Don
Published on 2010-04-21T13:32:45Z
Indexed on
2010/04/21
20:43 UTC
Read the original article
Hit count: 507
spring-mvc
|validation
Hi,
I've defined a controller, validator and command class for a Spring 2.5 MVC application like this:
public class ResourceController extends AbstractCommandController {
private MessageRetriever messageRetriever;
protected ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object command,
BindException errors) throws Exception {
ResourceCommand resourceCommand = (ResourceCommand) command;
// I NEED TO CHECK HERE IF COMMAND IS VALID?
}
public static class ResourceCommand {
private String module;
private String site;
private String lang;
// GETTERS AND SETTERS OMITTED
}
public static class ResourceValidator implements Validator {
public boolean supports(Class clazz) {
return ResourceCommand.class.isAssignableFrom(clazz);
}
public void validate(Object obj, Errors errors) {
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "module", "MODULE_REQUIRED");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "site", "SITE_REQUIRED");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "lang", "LANG_REQUIRED");
}
}
}
I have wired these all together in the application context:
<bean id="resourceController" class="com.amadeus.jcp.ui.framework.localization.ResourceController">
<property name="commandClass" value="com.amadeus.jcp.ui.framework.localization.ResourceController.ResourceCommand"/>
<property name="validator">
<bean class="com.amadeus.jcp.ui.framework.localization.ResourceController.ResourceValidator"/>
</property>
</bean>
However, I can't figure out how to actually check whether the command is valid or not - I assume the framework calls the validator, but how do I get access to the result?
Incidentally, I'm using Java 1.4, so can't use any solutions that require annotations or other Java 1.5 features.
Thanks, Don
© Stack Overflow or respective owner