2

我刚开始使用 spring MVC 并且无法在验证失败时显示正确的错误消息。

控制器是:

     @RequestMapping(value = "/MobiusDashboardWebsiteContent/csiBatchSubmission", method = RequestMethod.POST)
        @LogAction(actionPerformed = "submitCSIBatch")
        public String submitCSIBatch(ManualCSIBatch manualCSIBatch, BindingResult result, Model model, MobiusAuthenticationToken authToken) throws Throwable {

            ManualCSIBatch mBatch = new ManualCSIBatch();
            manualCSIBatch.setBatchUser(getUserName(authToken));
            csiBatchValidator.validate(manualCSIBatch, result);
            if (result.hasErrors()) {
                 LOG.error("Validation errors found for the batch " + manualCSIBatch.getBatchName() + " error code is " + manualCSIBatch.getErrorCode());  
model.addAttribute("localeList", localeList);
            model.addAttribute("evaluateAttributeList", evaluateAttibutes);
            model.addAttribute("csiBatchSubmission", manualCSIBatch);                
return "csiBatchSubmission";
            }
            mBatch.setErrorCode(StatusCode.SUCESS.getCode());
            model.addAttribute("csiBatchSubmission", mBatch);
            return "csiBatchSubmission";
        }

验证器是:

   @Override
    public void validate(Object target, Errors errors) {
        ManualCSIBatch manualCSIBatch = (ManualCSIBatch) target;
        boolean isDuplicate = getiMobiusService().checkDuplicateCSIBatchName(manualCSIBatch.getBatchName(), manualCSIBatch.getBatchUser());
        if (isDuplicate) {
            logger.debug("This is a duplicate batch " + manualCSIBatch.getBatchName() + " for this user " + manualCSIBatch.getBatchUser());
            errors.rejectValue("batchName", "errors.csi.duplicateBatch", new Object[] { manualCSIBatch.getBatchName() }, null);
        }

当验证失败时,一切正常,表单不提交,但错误值未在 UI 上填充。

JSP 是这样的:

<div class="row">

    <label for="batch-name"><spring:message code="csi.label.batchname"/></label><form:input path="batchName" onkeyup="checkDuplciateBatch(this.value);" onblur="checkDuplciateBatch(this.value);"/> 
    <span id="availmsg" style="display:none"></span>
    <form:errors path="batchName" cssClass="error"/>
    </div>

在我看到的所有例子中,这就是方式。这里可能有什么问题?

谢谢,

4

1 回答 1

1

所以我们开始吧。

我开始知道 BindingResult 映射到每个模型属性。

根据http://blog.nigelsim.org/2011/09/07/spring-mvc-validation-bindingresult/在模型对象解决问题之前添加@ModelAttribute !!惊人的!:)

于 2012-12-26T12:33:58.640 回答