I'm writing a web app with spring and I'm having problems with forms validation. It's the first time I use that, so I still have to understand it...
Basically I have a controller with the form handling method that is :
@RequestMapping(params = "action=gestprodotti")
public ModelAndView doGestProdotti(
@RequestParam(value = "page", required = false, defaultValue = "0") int page,
@RequestParam(value = "activity", required = false) String activity,
@ModelAttribute Prodotto prod, BindingResult bindresult,
HttpSession session) throws Exception {
the controller is annotated and doesn't extend anything. Later in the method, after receiving the object bound to the form, I do:
Validator valid = activityHandler.getValidator();
valid.validate(backingObject, bindresult);
if (bindresult.hasErrors()){
return handleRequest(action, ACTIVITY_NEW_ITEM, jspComponent,
page, itemId, backingObject, bindresult, appstatus);
}
that means that I call manually the validator and give back the same form page (a ModelAndView result).
The validator code:
public static final class ProdottoValidator implements Validator{
@Override
public boolean supports(Class<?> other) {
return Prodotto.class.isAssignableFrom(other);
}
@Override
public void validate(Object obj, Errors errors) {
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "codice", "Il codice è richiesto");
}
}
and the relevant JSP code:
<td colspan="2" align="left">Codice<form:input id="code"
path="codice" /><form:errors path="codice" /></td>
that works fine for binding, but I don't get any error. I didn't create any messages.properties file, but I think I should see something anyway. Errors are detected because bindresult.hasErrors() gives true, but the rendered html doesn't show up anything in place of form:errors. Am I missing something like configuration or whatever? Is it because there is not a messages.properties file that it doesn't work?