java bindingResult hasError函数

时间:2015-02-19 16:46:24

标签: java spring java-ee binding annotations

我有一个bean,它有一些带有某些字段注释的属性 我两次使用这个豆子 我第一次需要对所有属性使用bindingResult.hasError函数(没问题) 第二次我只想对四个属性使用bindingResult.hasError函数(对应于我的JSP显示的四个字段)但是它不起作用导致函数bindingResult.hasError检测到bean中其他字段的错误

我的豆子:

private String region;
@NotBlank
private String zone;
@NotBlank
private String country;
@NotBlank
private String site;
private Long siteId;
@NotNull
private Long statusId;
private String status;
@NotBlank
private String linkName;
@NotNull
private Long linkTypeId;
private String linkType;
private Integer downloadLineBw;
private Integer downloadPortBw;
private String technology;
@ValidDate
private String installDate; 
private String maintenanceInfo;
private String internetConfiguration;
@NotNull
private Long linkRoleId;
private String linkRole;
private Long contractOwnerId;
private String contractOwner;
private Integer uploadPortBw;
private Integer uploadLineBw;
private Long yearlyCost;
@ValidDate
private String endDate;
private String supplier;
private Long ismCost;
private Long oneTimeCost;
private String linkedService;

我的控制器:

@RequestMapping(method = RequestMethod.POST)
public String findLinksByCriteria(@Valid LinkForm link, BindingResult bindingResult, Model uiModel) {
    if (isCriteriaEmpty(link)) {
        uiModel.addAttribute("criteriaEmptyWarning", "error_search_criteria_empty");
        return ViewConstants.LINK_SEARCH_VIEW;
    }
    if (bindingResult.hasErrors()) {
        return ViewConstants.LINK_SEARCH_VIEW;
    }
    ...
}

我的JSP的一部分:

        <tr>
            <td class="label"><spring:message code="link.site" /></td> 
            <td class="value"><form:input path="site" cssClass="x-form-text" /></td> 

            <td class="label"><spring:message code="link.type" /></td>
            <td class="value"><form:input path="linkType" cssClass="x-form-text" /></td> 
        </tr> 
        <tr>
            <td class="label"><spring:message code="link.installDate" /></td>
            <td class="value">
                <form:input path="installDate"  cssClass="datepickerMe" />
                <form:errors path="installDate" cssClass="errormsg" />
            </td>


            <td class="label"><spring:message code="link.EndDate" /></td>
            <td class="value">
                <form:input  path="endDate" cssClass="datepickerMe" />
                <form:errors path="endDate" cssClass="errormsg" />
            </td>
        </tr>

2 个答案:

答案 0 :(得分:0)

可以创建自定义弹簧验证器,以便您可以确定应该验证女巫属性:

@Controller
public class MyController {

    @Autowired
    private MyFormValidador formValidador;

    @InitBinder
    private void initBinder(WebDataBinder binder) {
        binder.setValidator(formValidador);
    }

    @RequestMapping(method = RequestMethod.POST)
    public String execute(@Valid MyForm form, BindingResult result, Model model) {
        if (result.hasErrors()) {
            return "errorview";
        }

        return "successview";
    }

}

@Component
public class MyFormValidador implements Validator {

    @Override
    public boolean supports(Class<?> clazz) {
        return MyForm.class.isAssignableFrom(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "linkName", "linkName.required", "Link name must be informed");
    }

}

答案 1 :(得分:0)

在Spring中,BindingResult会查找完整的对象错误,您无法检查部分对象的错误(对象中的几个字段)。

在对象中,如果任何单个验证失败,则bindingResult.hasError()将返回true,因此对象验证失败。仅当满足对象中的所有验证时,bindingResult.hasError()才会为false

创建一个包含NOT NULL列属性的数据库表并尝试为这些列插入NULL值。