Spring form validation problems
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 (bi开发者_如何学Cndresult.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?
This is a typical symptom of model attribute name mismatch.
Model attribute name should be the same in three places:
In form setup method, where you put initial state of the form object into the model.
In
<form:form>
element of JSP page. Default name here iscommand
, it can be configured usingmodelAttribute
attribute (orcommandName
, it's the same thing).In form processing method, such as your
doGestProdotti
. Default name is inferred as an argument class name with the first letter decapitalized (prodotto
in your case). You can specify the name explicitly with@ModelAttribute("..."
).
Mismatch between points (2) and (3) causes validation errors not being displayed. Mismatch between points (1) and (2) causes binding errors when displaying a form.
So, you need either to configure prodotto
as a model attribute name in points (1) and (2)
<form:form modelAttribute = "prodotto" ...>
....
<form:errors path="codice" />
...
</form:form>
or configure command
as an attribute name in point (3)
@ModelAttribute("command") Prodotto prod
If you don't have any messages.properties file I would expect there to be an error message in the logs because it can't find any message under the code "Il codice e richiesto".
精彩评论