简体   繁体   English

形式:错误不会在jsp上呈现

[英]form:errors doesn't render on jsp

I have following controller: 我有以下控制器:

@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类:

class MyDto {
    @NotEmpty
    String companyName;    

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

and following jsp: 和以下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>     

When I submit form I see that 当我提交表格时,我看到

violations.size() > 0 is true violations.size() > 0为true

but when I see rendered page I dont see error messages. 但是当我看到渲染页面时,我看不到错误消息。
What the problem? 什么问题

As I said in my answer to you previous question, that's not the way Spring MVC validations work. 正如我在回答上一个问题时所说的那样,这不是Spring MVC验证工作的方式。

Spring MVC comes with a lot of magic ... provided you follow its rules. 如果您遵循它的规则,Spring MVC会带来很多魔力。 For the <form:errors .../> tags to work, you must have an Errors object in the response tied to the ModelAttribute object. 为了使<form:errors .../>标记起作用,您必须在响应中将一个Errors对象绑定到ModelAttribute对象。 It happens automagically if you put one BindingResult as a method parameter immediately after the model attribute (see ref ed answer) 它发生自动的,如果你把一个BindingResult方法参数模型的属性后(见参考文献编辑答案)

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. 不幸的是,当您这样做时,Spring将验证每个字段,而这不是您想要的,而没有任何简单的方法来忽略某些字段上的错误。 So you could : 所以你可以:

  • put no automatic validator through an @InitBinder annotated method 通过@InitBinder注释的方法不放置自动验证器
  • and either 还有
    • explicitely reject fields by doing a manual validation (or depending of your violation ) - a bit manual, but robust method 通过手动验证(或根据您的violation )显式拒绝字段-有点手动但可靠的方法
    • 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. 在JSR-303验证器周围使用SpringValidatorAdapter ,因为它可以通过其SmartValidator接口将验证组用作验证提示,并在SmartValidator显式调用其方法void validate(Object target, Errors errors, Object... validationHints)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM