大佬教程收集整理的这篇文章主要介绍了java – Spring – 禁用绑定异常(针对特定属性),大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
在我正在使用Spring 2.5.6.SEc01的Web应用程序中,我基本上有一个Integer字段,它接受一个数字来确定要滚动到哪个页面.需求已更改,我们不再希望显示错误消息,但如果用户输入的数字无效,则会忽略用户的输入,例如“adfadf”.
我正在读你可以通过以下方式做到:
TypeMismatch.property =一些新的错误消息
但是,在尝试过之后,我们仍然收到原始错误消息:
java.lang.Integer.TypeMismatch = …
我只想为该给定属性禁用此消息.我怎样才能做到这一点?我仍然希望绑定自动发生,我现在不想听到它.
沃尔特
在代码“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
所以,@R_324_10673@下测试
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 @R_53_4895@ubmission() 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,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,"Age is required");
}
});
}
以上是大佬教程为你收集整理的java – Spring – 禁用绑定异常(针对特定属性)全部内容,希望文章能够帮你解决java – Spring – 禁用绑定异常(针对特定属性)所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。