java-Spring MVC-带注释的约束验证-错误消息未显示

前端之家收集整理的这篇文章主要介绍了java-Spring MVC-带注释的约束验证-错误消息未显示 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我希望能够在Web界面中显示我的实体字段约束验证错误.

例如,我在实体上定义了以下约束:

@RooJavaBean
@RooToString
@RooJpaActiveRecord
public class Member {

    @NotNull
    @Email
    @Column(unique = true)
    private String email;

    private String pseudo;

    @NotNull
    @Size(min = 6)
    private String password;

    @NotNull
    @Size(min = 2)
    private String firstName;

    @NotNull
    @Size(min = 2)
    private String lastName;

    @Temporal(TemporalType.DATE)
    @DateTimeFormat(pattern = "dd/MM/yyyy")
    private Date dateOfBirth;

    @NotNull
    @Enumerated
    private Orientation orientation;

    @NotNull
    private String token;

    @NotNull
    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(pattern = "dd/MM/yyyy")
    private Date registrationDate;

    private boolean activated;
}

我还定义了一个在Spring MVC控制器中使用的辅助JavaBean.这里是:

@RooJavaBean
public class RegistrationInfo {

    private Member member;

    private String passwordConfirmation;

}

这是我的Spring MVC控制器中的相关代码

@RequestMapping(value = "/registration",method = RequestMethod.POST,produces = "text/html")
public String registerMember(@modelattribute("registrationInfo") @Valid RegistrationInfo registrationInfo,Model model,BindingResult bindingResult) {
    if (!registrationInfo.getMember().getPassword().equals(registrationInfo.getPasswordConfirmation())) {
        bindingResult.addError(new ObjectError("member.password","passwords don't match"));
    }
    if (!registrationService.isEmailAvailable(registrationInfo.getMember().getEmail())) {
        bindingResult.addError(new ObjectError("member.email","email already used"));
    }
    if (bindingResult.hasErrors()) {
        //HOW TO ADD BEAN CONSTRAINTS VALIDATION ERRORS HERE????
        populateForm(model,registrationInfo);
        return "registration";
    }
    registrationService.registerMember(registrationInfo.getMember());
    return "redirect:/";
}

从我的用户界面:

<div id="errors">
            <ul th:if="${#fields.hasErrors('*')}">
                <li th:each="err : ${#fields.errors('*')}" th:text="${err}"></li>
            </ul>
        </div>

但是,到目前为止,将要显示的唯一错误是我手动添加错误(例如该bindingResult.addError)…

谁能提供建议?

编辑:如果我用@Valid注释成员变量,则在验证表单时会收到此异常:

org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 5 errors
Field error in object 'registrationInfo' on field 'member.password': rejected value []; codes [Size.registrationInfo.member.password,Size.member.password,Size.password,Size.java.lang.String,Size]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [registrationInfo.member.password,member.password]; arguments []; default message [member.password],2147483647,6]; default message [la taille doit �tre entre 6 et 2147483647]
Field error in object 'registrationInfo' on field 'member.firstName': rejected value []; codes [Size.registrationInfo.member.firstName,Size.member.firstName,Size.firstName,Size]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [registrationInfo.member.firstName,member.firstName]; arguments []; default message [member.firstName],2]; default message [la taille doit �tre entre 2 et 2147483647]
Field error in object 'registrationInfo' on field 'member.token': rejected value [null]; codes [NotNull.registrationInfo.member.token,NotNull.member.token,NotNull.token,NotNull.java.lang.String,NotNull]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [registrationInfo.member.token,member.token]; arguments []; default message [member.token]]; default message [ne peut pas �tre nul]
Field error in object 'registrationInfo' on field 'member.registrationDate': rejected value [null]; codes [NotNull.registrationInfo.member.registrationDate,NotNull.member.registrationDate,NotNull.registrationDate,NotNull.java.util.Date,NotNull]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [registrationInfo.member.registrationDate,member.registrationDate]; arguments []; default message [member.registrationDate]]; default message [ne peut pas �tre nul]
Field error in object 'registrationInfo' on field 'member.lastName': rejected value []; codes [Size.registrationInfo.member.lastName,Size.member.lastName,Size.lastName,Size]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [registrationInfo.member.lastName,member.lastName]; arguments []; default message [member.lastName],2]; default message [la taille doit �tre entre 2 et 2147483647]
    org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:111)
    org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:75)
    org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:156)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:117)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:147)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
最佳答案
将@Valid添加到RegistrationInfo内的member属性

@RooJavaBean
public class RegistrationInfo {

    private @Valid Member member;

    private String passwordConfirmation;

}

然后在您的控制器方法中,bindingResult必须紧接在registrationInfo之后:

public String registerMember(@modelattribute("registrationInfo") @Valid RegistrationInfo registrationInfo,BindingResult bindingResult,Model model) {
原文链接:https://www.f2er.com/spring/531831.html

猜你在找的Spring相关文章