I have a problem for form validation in a spring 3 mvc application.
It seems everything is set up, but when a bad entry is enetered in the form field it is detected by validator and no error message is shown.
here's the validator
@Component public class SettingsValidator implements Validator{ @Override public boolean supports(Class<?> clazz) { return VrSettings.class.isAssignableFrom(clazz); } @Override public void validate(Object target, Errors errors) { ValidationUtils.rejectIfEmpty(errors, "applicationBaseUri", "field.required", "field required"); ValidationUtils.rejectIfEmpty(errors, "remoteDnsServiceUri","field.required", "field required"); VrSettings settings = (VrSettings) target; try { URI uri = new URI(settings.getApplicationBaseUri()); } catch (URISyntaxException e) { errors.rejectValue("applicationBaseUri", "error.invalid.uri", "invalid uri"); } try { URI uri = new URI(settings.getLocalApplicationBaseURI()); } catch (URISyntaxException e) { errors.rejectValue("localApplicationBaseURI", "error.invalid.uri", "invalid uri"); } try { URI uri = new URI(settings.getRemoteDnsServiceUri()); } catch (URISyntaxException e) { errors.rejectValue("remoteDnsServiceUri", "error.invalid.uri", "invalid uri"); } } }
the controller...
@Controller public class UserInterfaceController { @InitBinder protected void initBinder(WebDataBinder binder) { binder.setValidator(new SettingsValidator()); } @RequestMapping(method=RequestMethod.POST, value="ui/settings") public ModelAndView postSettings(@ModelAttribute("settings") @Valid VrSettings settings, BindingResult result) { ModelAndView mav = new ModelAndView("settings"); mav.addObject("settings", settings); if(result.hasErrors()) { mav.addAllObjects(result.getModel()); return mav; } LinkedList<VrSettings> vrSettingsList = persistenceManager.getAll(new VrSettings()); if(vrSettingsList.isEmpty()) { //do things } else { //do things } return mav; } } and finally the JSP
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%> <%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<h3>Configuration Parameters</h3> <span style="font-weight:bold; color: red"> <form:errors path="*" /> </span><br /> <div class="form"> <form:form method="post" action="settings" commandName="settings"> <form:label for="baseuri" path="applicationBaseUri">base URI*</form:label> <form:input class="text" path="applicationBaseUri" size="35" name="baseuri"/> <form:label for="localbaseuri" path="localApplicationBaseURI">local base URI</form:label> <form:input class="text" path="localApplicationBaseURI" size="35" name="localbaseuri"/> <form:label for="ldns" path="remoteDnsServiceUri">ldns URI*</form:label> <form:input class="text" path="remoteDnsServiceUri" size="35" name="ldns"/> <div class="form-row"> <input type="submit" value="submit"/> </div> </form:form> </div> </body> I can't understand what I'm missing. Any help is appreciated, thanks in advance!