形式:错误不会在jsp上呈现 [英] form:errors doesn't render on jsp

查看:81
本文介绍了形式:错误不会在jsp上呈现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下控制器:

@RequestMapping(value = "/member/createCompany/addParams", method = RequestMethod.POST)
    public String setCompanyParams(
            @ModelAttribute MyDto myDto,
            @RequestParam(value = "g-recaptcha-response") String recapchaResponse,
            HttpSession session, Principal principal, Model model) throws Exception {

        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        Validator validator = factory.getValidator();
        Set<ConstraintViolation<MyDto>> violations;        
        if(condition1){
               violations = validator.validate(myDto, ValidationGroup1.class);
        } else {
              violations = validator.validate(myDto);
        }
        if (violations.size() > 0) {
            return "member/createCompany/addParams";
        }
        ...
}

MyDto类:

class MyDto {
    @NotEmpty
    String companyName;    

    @NotEmpty(groups = ValidationGroup1.class)
    String generatedPassword;
}

和以下jsp:

   <form:form commandName="myDto" id="addParams" action="/member/createCompany/addParams" method="POST">
                        <form:errors cssClass="error"  path="companyName"/><br/>
                        <form:input type="text" id="companyName" path="companyName" name="companyName" maxlength="255" value="${campaign.campaignName}"/><br/>

                        <form:errors cssClass="error"  path="generatedPassword"/><br/>
                        <form:input path="generatedPassword" type="text" id="generatedPassword" name="generatedPassword" value="${generatedPassword}" />
                         ... 
   </form:form>     

我提交表单时看到

violations.size() > 0是真的

但是当我看到渲染的页面时,我没有看到错误消息.
什么问题

but when I see rendered page I dont see error messages.
What the problem?

推荐答案

正如我在我对你的回答中所说的那样问题,这不是Spring MVC验证的工作方式.

As I said in my answer to you previous question, that's not the way Spring MVC validations work.

Spring MVC带有很多魔力……只要您遵循其规则即可.为了使<form:errors .../>标记起作用,必须在响应中将Errors对象绑定到ModelAttribute对象.如果将一个BindingResult作为方法参数紧接在model属性之后,则会自动发生(请参见ref ed 答案)

Spring MVC comes with a lot of magic ... provided you follow its rules. For the <form:errors .../> tags to work, you must have an Errors object in the response tied to the ModelAttribute object. It happens automagically if you put one BindingResult as a method parameter immediately after the model attribute (see refed answer)

不幸的是,当您这样做时,Spring将验证每个字段,而这不是您想要的,而没有任何简单的方法来忽略某些字段上的错误.所以您可以:

Unfortunately, when you do that, Spring will validate every field, which is not what you want, without any simple way to ignore errors on some fields. So you could :

    通过@InitBinder注释的方法
  • 不提供自动验证程序
    • 通过手动验证(或取决于您的 violation )明确拒绝字段-有点手动,但功能强大的方法
    • 在JSR-303验证程序周围使用SpringValidatorAdapter,因为它可以通过其SmartValidator接口将验证组用作验证提示,并在控制器中显式调用其方法void validate(Object target, Errors errors, Object... validationHints).
    • put no automatic validator through an @InitBinder annotated method
    • and either
      • explicitely reject fields by doing a manual validation (or depending of your violation) - a bit manual, but robust method
      • use a SpringValidatorAdapter around your JSR-303 validator as it can use validation groups as validation hints through its SmartValidator interface and explicitely calls its method void validate(Object target, Errors errors, Object... validationHints) in your controller.

      无论如何,我强烈建议您不要为每个请求创建一个验证器,而是将其创建为bean并将其注入到控制器中.

      Anyway, I strongly advice you not to create a validator for each request, but create it as a bean and inject it in your controller.

      这篇关于形式:错误不会在jsp上呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆