簡體   English   中英

Spring 3.x驗證錯誤綁定到fieldName失敗

[英]Spring 3.x validation error binding on fieldName fails

我正在使用Spring 3.2.8-RELEASE版本。 這可能是一個錯誤,但需要確認。

我的控制器:

@SessionAttributes( { "exampleForm", "testForm", "staticContent" } )
@Controller
@RequestMapping( value = "/action" )
public class ExampleController extends BaseController {

    @Autowired
    private ExampleFormValidator validator;

    @RequestMapping(value = "/subAction", method = RequestMethod.GET)
    public String doGet(@ModelAttribute ExampleForm exampleForm,
    @ModelAttribute TestForm testForm, @ModelAttribute StaticContent staticContent) {
        return "example.tile";
    }

    @ModelAttribute("exampleForm")
    public ExampleForm exampleForm() {
        return new ExampleForm();
    }

    @InitBinder("exampleForm")
    public void initBinder(WebDataBinder binder) {
        binder.setValidator(this.validator);
    }

    @RequestMapping(value = "/subAction", method = RequestMethod.POST, params = "submitAction=ok")
    public String doAction(@Validated @ModelAttribute ExampleForm exampleForm,
            BindingResult formBinding, @ModelAttribute TestForm testForm,
            @ModelAttribute StaticContent staticContent, SessionStatus sessionStatus,
            RedirectAttributes ra, Model model) {
            if (formBinding.hasErrors()) {
                return "example.tile";
            }
        }

}

我的驗證者:

@Component
public class ExampleFormValidator implements Validator {

    public boolean supports( Class<?> clazz ) {
        return ExampleForm.class.equals( clazz );
    }

    public void validate( Object target, Errors errors ) {
        ExampleForm form = (ExampleForm) target;
        if (form.getFieldName() == null) {
            error.rejectValue("fieldName", form.getFieldName(), "common.field.required");
        }
    }
}

為簡潔起見,一個字段的示例表格:

public class ExampleForm {
    private String fieldName;

    public String getFieldName() {
        return fieldName;
    }

    public void setFieldName(String fieldName) {
        this.fieldName = fieldName;
    }
}

靜態內容表單的字段名稱與上面示例示例中的字段名稱相同,但其類型為日期而不是字符串。

public class StaticContent {
    private Date fieldName;

    public Date getFieldName() {
        return fieldName;
    }

    public void setFieldName(Date fieldName) {
        this.fieldName = fieldName;
    }
}

StaticContent具有靜態內容,該靜態內容顯示在JSP的頂部。 表單元素示例包含表單元素。

GET方法可以正常工作並顯示JSP。 當我在不提供任何輸入的情況下提交表單時,它將調用示例驗證程序,添加拒絕的值,但失敗並出現以下錯誤。 如您所見,它正在嘗試將拒絕的值綁定到staticContent對象,而不是exampleForm。 我究竟做錯了什么?

14:39:04,605 INFO [STDOUT] [13 Jan 2015 14:39:04,602] TRACE [ServletInvocableHandlerMethod] Error resolving argument [3] [type=com.personal.example.bo.StaticContent] HandlerMethod details: Controller [com.personal.example.controller.ExampleController] Method [public java.lang.String com.personal.example.controller.ExampleController.doAction(com.personal.example.form.ExampleForm,org.springframework.validation.BindingResult,com.personal.example.form.TestForm,com.personal.example.bo.StaticContent,org.springframework.web.bind.support.SessionStatus,org.springframework.web.servlet.mvc.support.RedirectAttributes,org.springframework.ui.Model) throws com.personal.example.exception.ApplicationException]

org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors Field error in object 'staticContent' on field 'fieldName': rejected value []; codes [typeMismatch.staticContent.fieldName,typeMismatch.fieldName,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [staticContent.fieldName,fieldName]; arguments []; default message [fieldName]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'fieldName'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type java.util.Date for value ''; nested exception is java.lang.IllegalArgumentException]

我嘗試了幾種不同的方法,而沒有嘗試重命名ExampleForm中的“ fieldName”

  1. 試圖使用exampleForm對error.rejectValue方法調用中的字段名稱進行資格預審,顯然它不喜歡它,並拋出org.springframework.beans.NotReadablePropertyException
error.rejectValue("exampleForm.fieldName", form.getFieldName(), "common.field.required");
  1. 我試圖在控制器的POST方法中對@ModelAttribute進行資格預審,但這無濟於事
@RequestMapping(value = "/subAction", method = RequestMethod.POST, params = "submitAction=ok")
    public String doAction(@Validated @ModelAttribute ("exampleForm") ExampleForm exampleForm,
            BindingResult formBinding, @ModelAttribute TestForm testForm,
            @ModelAttribute StaticContent staticContent, SessionStatus sessionStatus,
            RedirectAttributes ra, Model model) {
}

似乎您需要:

@InitBinder("exampleForm")
protected void initBinder(WebDataBinder binder) {
    binder.setValidator(new ExampleFormValidator());
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM