java中出现绑定异常,java – Spring – 禁用绑定异常(针对特定属性)

根据

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

它会解决你想要的

UPDATE

我创建了一个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));

}

}

}

}

如果你想要第二个,你必须摆脱typeMismatch.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");

}

});

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值