Spring - 禁用绑定异常(针对特定属性)

时间:2010-05-14 14:46:44

标签: java data-binding spring-mvc

在我正在使用Spring 2.5.6.SEC01的Web应用程序中,我基本上有一个Integer字段,它接受一个数字来确定要滚动到哪个页面。需求已更改,我们不再希望显示错误消息,但如果输入的数字无效,则忽略用户的输入,例如“adfadf”。

我读到你可以通过以下方式做到:

TypeMismatch.property =一些新的错误消息

然而,在尝试过之后,我们仍然收到原始错误消息: java.lang.Integer.TypeMismatch = ...

我只想为该给定属性禁用此消息。我怎样才能做到这一点?我仍然希望绑定自动发生,我现在不想听到它。

沃尔特

3 个答案:

答案 0 :(得分:4)

根据DefaultMessageCodesResolver

如果代码为“typeMismatch”,则对象名称为“user”,字段为“age”

  • typeMismatch.user.age
  • typeMismatch.age
  • typeMismatch.int
  • typeMismatch

所以你应该得到(我想你的commandName被称为命令,你的属性是年龄)根据你的代码进行调整

typeMismatch.command.age
typeMismatch.age
typeMismatch.java.lang.Integer
typeMismatch

注意第三个代码

typeMismatch.java.lang.Integer

它会解决你想要的问题

<强>更新

我创建了一个Person命令类

public class Person implements Serializable {

    private Integer age;

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

}

和一个人控制器

public class PersonController extends SimpleFormController {

    public PersonController() {
        setCommandClass(Person.class);
        setValidator(new Validator() {
            public boolean supports(Class clazz) {
                return clazz.isAssignableFrom(Person.class);
            }

            public void validate(Object command, Errors errors) {
                rejectIfEmpty(errors, "age", "Age is required");
            }
        });
    }

    @Override
    protected ModelAndView onSubmit(Object command) throws Exception {
        return new ModelAndView();
    }

}    

这是我的myMessages.properties(类路径的根目录)

typeMismatch.command.age=typeMismatch.command.age
typeMismatch.age=typeMismatch.age
typeMismatch.java.lang.Integer=typeMismatch.java.lang.Integer
typeMismatch=typeMismatch

所以,我做了以下测试

public class PersonControllerTest {

    private PersonController personController;
    private MockHttpServletRequest request;

    private MessageSource messageSource;

    @Before
    public void setUp() {
        request = new MockHttpServletRequest();
        request.setMethod("POST");

        personController = new PersonController();

        messageSource = new ResourceBundleMessageSource();
        ((ResourceBundleMessageSource) messageSource).setBasename("myMessages");
    }

    @Test
    public void failureSubmission() throws Exception {
        /**
         * Ops... a bindException
         * 
         * Age can not be a plain String, It must be a plain Integer
         */
        request.addParameter("age", "not a meaningful age");

        ModelAndView mav = personController.handleRequest(request, new MockHttpServletResponse());

        BindingResult bindException = (BindingResult) mav.getModel().get(BindingResult.MODEL_KEY_PREFIX + "command");
        for (Object object : bindException.getAllErrors()) {
            if(object instanceof FieldError) {
                FieldError fieldError = (FieldError) object;

                assertEquals(fieldError.getField(), "age");

                /**
                  * outputs typeMismatch.command.age
                  */
                System.out.println(messageSource.getMessage((FieldError) object, null));
            }
        }
    }

}

如果你想要第二个,你必须摆脱类型的Mismatch.command.age密钥资源包

typeMismatch.age=typeMismatch.age
typeMismatch.java.lang.Integer=typeMismatch.java.lang.Integer
typeMismatch=typeMismatch

或编写自己的MessageCodesResolver实现

public class MyCustomMessageCodesResolver implements MessageCodesResolver {

    private DefaultMessageCodesResolver defaultMessageCodesResolver = new DefaultMessageCodesResolver();

    public String [] resolveMessageCodes(String errorCode, String objectName) {
        if(errorCode.equals("age"))
            /**
              * Set up your custom message right here
              */
            return new String[] {"typeMismatch.age"};

        return defaultMessageCodesResolver.resolveMessageCodes(String errorCode, String objectName);
    }

    public void String[] resolveMessageCodes(String errorCode, String objectName, String field, Class fieldType) {
        if(errorCode.equals("age"))
            /**
              * Set up your custom message right here
              */
            return new String[] {"typeMismatch.age"};

        return defaultMessageCodesResolver.resolveMessageCodes(String errorCode, String objectName, String field, Class fieldType);
    }
}

设置你的PersonController

public class PersonController extends SimpleFormController {

    public PersonController() {
        setMessageCodesResolver(new MyCustomMessageCodesResolver());
        setCommandClass(Person.class);
        setValidator(new Validator() {
            public boolean supports(Class clazz) {
                return clazz.isAssignableFrom(Person.class);
            }

            public void validate(Object command, Errors errors) {
                rejectIfEmpty(errors, "age", "Age is required");
            }
        });
    }

答案 1 :(得分:0)

您可以为该字段注册自定义PropertyEditor,但在类型不匹配时不会失败。

答案 2 :(得分:0)

由于这是一个Spring MVC应用程序,并假设它是一个简单的表单,您可以通过多种方式进行设置。你能指定你的控制器设置吗?对于发布请求,您可以在调用验证程序之前record a suppressed field(假设您已指定一个)或在调用验证程序之后。{3}}如果您想在验证之前执行此操作,可以调用[this] [2]。验证后,您可以调用[this] [3]

[2]:http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/web/servlet/mvc/BaseCommandController.html#onBind(javax.servlet.http.HttpServletRequest,java.lang.Object,org.springframework.validation.BindException)   [3]:http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/web/servlet/mvc/BaseCommandController.html#onBindAndValidate(javax.servlet.http.HttpServletRequest,java.lang.Object,org.springframework.validation.BindException)