Hi ....
I try to learn Spring MVC 3.0 validation. but I got NoSuchMessageException: No message found under code 'name.required' for locale 'en_US'
error message when form submted.
I have create message.properties in src/message.properties and the content of that file is:
name.required = User Name is required
password.required = Password is required
gender.required = Gender is required
I have set ResourceBundleMessageSource in my app-servlet.xml
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource" p:basename="messages" />
My validator code is:
@Component("registrationValidator")
public class RegistrationValidator implements Validator {
@Override
public boolean supports(Class<?> clazz) {
return RegistrationCommand.class.isAssignableFrom(clazz);
}
@Override
public void validate(Object target, Errors errors) {
RegistrationCommand registrationCommand = (RegistrationCommand) target;
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "name.required");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "password.required");
ValidationUtils.rejectIfEmpty(errors, "gender", "gender.required");
ValidationUtils.rejectIfEmpty(errors, "country", "country.required");
//ValidationUtils.rejectIfEmpty(errors, "community", "community.required");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "description", "description.required");
if (registrationCommand.getCommunity().length == 0) {
errors.rejectValue("community", "community.required");
}
}
}
and JSP Page is:
<form:form commandName="registrationCommand">
<p class="name">
<label for="name">Name</label>
<form:input path="name" />
<form:errors path="name" cssClass="error"></form:errors>
</p>
<p class="password">
<label for="password">Password</label>
<form:password path="password" />
<form:errors path="password" cssClass="error"></form:errors>
</p>
<p class="gender">
<label>Gender</label>
<form:radiobutton path="gender" value="M" label="M" />
<form:radiobutton path="gender" value="F" label="F" />
<form:errors path="gender" cssClass="error"></form:errors>
</p>
<p class="submit">
<input type="submit" value="Submit" />
</p>
</form:form>