我遇到了以下问题:错误消息未在用户表单中显示,但存在于BindingResult中。需要你的帮助。 我正在使用Spring Roo生成的控制器SignUpController
@RequestMapping("/signup/**")
@Controller
public class SignUpController {
List<ObjectError> signUpErrors;
@Autowired
private SignUpValidator validator;
@ModelAttribute("UserRegistrationForm")
public UserRegistrationForm formBackingObject() {
return new UserRegistrationForm();
}
@RequestMapping(params = "form", produces = "text/html")//, method = RequestMethod.GET)
public String createForm(Model uiModel) {
populateSignUpForm(uiModel, new UserRegistrationForm());
//uiModel.addAttribute("signUpErrors", signUpErrors);
return "signup/index";
}
@RequestMapping(method = RequestMethod.POST, produces = "text/html")
public String create(@Valid UserRegistrationForm userRegistration,
BindingResult bindingResult, Model uiModel, HttpServletRequest request) {
validator.validate(userRegistration, bindingResult);
if (bindingResult.hasErrors()) {
populateSignUpForm(uiModel, userRegistration);
//uiModel.addAttribute("signUpErrors", bindingResult.getAllErrors());
return "signup/index";
}
...
用于自定义表单支持对象UserRegistrationForm
public class UserRegistrationForm {
@NotNull(message="Must be filled.")
@Size(min=6, max = 45)
private String login;
@Email
private String email;
@NotNull
@Size(min=6, max = 45)
,尝试使用自定义验证器验证它。我可以向用户显示错误消息,直接将BindingResults传递给jspx,并使用如下代码:
<c:if test="${signUpErrors.size()>0}">
<util:panel id="title" title="${title}">
<h2>
<spring:message code="signup_index_error" />
</h2>
<p></p>
<c:forEach var="err" items="${signUpErrors}">
<br />
<tr>
<td>${err.objectName} </td>
<td>${err.codes} </td>
<td>${err.arguments} </td>
<td>${err.defaultMessage} </td>
</tr>
</c:forEach>
</util:panel>
<p></p>
</c:if
但标准的Roo和Spring标签不起作用:
<field:input field="login"
id="fc_.._UserRegistrationForm_login" required="true"
z="" />
<sf:errors path="login" cssStyle="error"></sf:errors>
<field:input field="email"
id="fc_.._UserRegistrationForm_email" required="true"
z="" />
<sf:errors path="*" cssStyle="error"></sf:errors>