1

我遇到以下问题:错误消息未显示在用户表单中,但存在于 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>
4

0 回答 0