栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

根据DefaultMessageCodesResolver

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

  • typeMismatch.user.age
  • typeMismatch.age
  • typeMismatch.int
  • 类型不匹配

因此,您应该得到(我想您的commandName被称为 command, 并且您的属性是 age )根据您的代码进行调整

typeMismatch.command.agetypeMismatch.agetypeMismatch.java.lang.IntegertypeMismatch

注意第三条代码

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.agetypeMismatch.age=typeMismatch.agetypeMismatch.java.lang.Integer=typeMismatch.java.lang.IntegertypeMismatch=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 {                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");          System.out.println(messageSource.getMessage((FieldError) object, null)); }        }    }}

如果要第二个,则 必须摆脱 typeMismatch.command.age密钥资源包

typeMismatch.age=typeMismatch.agetypeMismatch.java.lang.Integer=typeMismatch.java.lang.IntegertypeMismatch=typeMismatch

或编写您自己的MessageCodesResolver实现

public class MyCustomMessageCodesResolver implements MessageCodesResolver {    private DefaultMessageCodesResolver defaultMessageCodesResolver = new DefaultMessageCodesResolver();    public String [] resolveMessageCodes(String errorCode, String objectName) {        if(errorCode.equals("age"))  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"))  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"); }        });    }


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/615829.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号