One way to display user-friendly binding error messages is to override SimpleFormController.onBindAndValidate() as follows:
This creates a map of user-friendly error messages, keyed by the field names with which the error messages are associated. You can then use it in your JSP, possibly with the
@Override
protected void onBindAndValidate(HttpServletRequest request, Object command,
BindException errors) throws Exception {
if (errors.hasErrors()) {
MaperrorsMap = new HashMap ();
ListerrorList = errors.getAllErrors();
for (FieldError error : errorList) {
String msg = "";
try {
msg = this.translate(error.getCode(), error.getArguments());
} catch (Exception e) {}
if (msg.contains("quantity")) {
msg = msg.replace(error.getField(), "quantity");
} else if (msg.contains("unitPrice")) {
msg = msg.replace(error.getField(), "unit price");
}
msg += ".";
errorsMap.put(error.getField(), msg);
}
request.setAttribute("errorsMap", errorsMap);
}
}
No comments:
Post a Comment