我使用的是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;
}
}
静态内容表单与上面的示例表单具有相同的字段名称但是它的类型为Date而不是String。
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]
我尝试了几种不同的东西,而没有尝试重命名&#39; fieldName&#39;在ExampleForm中
error.rejectValue("exampleForm.fieldName", form.getFieldName(), "common.field.required");
@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) {
}
答案 0 :(得分:0)
似乎你需要:
@InitBinder("exampleForm")
protected void initBinder(WebDataBinder binder) {
binder.setValidator(new ExampleFormValidator());
}