zoukankan      html  css  js  c++  java
  • springmvc 接受特殊类型字段的处理方法

    springmvc接受前台传入的数据时如果该字段类型无法被封装(如Date),则会出现400 Bad Request错误,解决方法如下。

    1.在需要处理的字段前加上注解:

    @DateTimeFormat( pattern = "yyyy-MM-dd" )

    然后在项目中引入joda-time.jar包,最后在在 SpringMVC 配置 xml 文件中中加入配置: <mvc:annotation-driven /> 。这一句配置是一种简写,其实是给 spring 容 器中注入了两个 Bena ,分别是: DefaultAnnotationHandlerMapping 和 AnnotationMethodHandlerAdapter 。 @DateTimeFormat 注解的内部同样需要使用到前面注入的两 个 bean 去处理,所以缺少这个配置, Spring 容器中没有对应的 bean 去处理注解同样也会报错。至此,所有的步骤都完成了,可以跑了。、

    2.使用springmvc提供的@InitBinder标签:

    在控制层中加入一个编辑器

        /**
         * 设置控制层特殊字段转换规则(如 Date),可重载
         * @param binder
         */
        @InitBinder 
        protected void initBinder(WebDataBinder binder) {  
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");  
            dateFormat.setLenient(false);  
            binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
        }

    spring提供了大量的实现类,诸如CustomDateEditor ,CustomBooleanEditor,CustomNumberEditor等

        @InitBinder    
           protected void initBinder(WebDataBinder binder) {    
               binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true));    
        /        binder.registerCustomEditor(int.class, new CustomNumberEditor(int.class, true));    
               binder.registerCustomEditor(int.class, new IntegerEditor());    
        /        binder.registerCustomEditor(long.class, new CustomNumberEditor(long.class, true));  
               binder.registerCustomEditor(long.class, new LongEditor());    
               binder.registerCustomEditor(double.class, new DoubleEditor());    
               binder.registerCustomEditor(float.class, new FloatEditor());    
           }   

    也可以不使用自带这些编辑器类,通过继承PropertiesEditor实现自定义编辑类

    import org.springframework.beans.propertyeditors.PropertiesEditor;
    
    public class DoubleEditor extends PropertiesEditor {  
        @Override  
        public void setAsText(String text) throws IllegalArgumentException {  
            if (text == null || text.equals("")) {  
                text = "0";  
            }  
            setValue(Double.parseDouble(text));  
        }  
      
        @Override  
        public String getAsText() {  
            return getValue().toString();  
        }  
    } 
    import org.springframework.beans.propertyeditors.PropertiesEditor;
    
    public class IntegerEditor extends PropertiesEditor {  
        @Override  
        public void setAsText(String text) throws IllegalArgumentException {  
            if (text == null || text.equals("")) {  
                text = "0";  
            }  
            setValue(Integer.parseInt(text));  
        }  
      
        @Override  
        public String getAsText() {  
            return getValue().toString();  
        }  
    } 
    import org.springframework.beans.propertyeditors.PropertiesEditor;
    
    public class FloatEditor extends PropertiesEditor {  
        @Override  
        public void setAsText(String text) throws IllegalArgumentException {  
            if (text == null || text.equals("")) {  
                text = "0";  
            }  
            setValue(Float.parseFloat(text));  
        }  
      
        @Override  
        public String getAsText() {  
            return getValue().toString();  
        }  
    }  
    import org.springframework.beans.propertyeditors.PropertiesEditor;
    
    public class LongEditor extends PropertiesEditor {  
        @Override  
        public void setAsText(String text) throws IllegalArgumentException {  
            if (text == null || text.equals("")) {  
                text = "0";  
            }  
            setValue(Long.parseLong(text));  
        }  
      
        @Override  
        public String getAsText() {  
            return getValue().toString();  
        }  
    }  

    3:配置全局日期转换器。

     http://blog.csdn.net/chenleixing/article/details/45156617

  • 相关阅读:
    一些遇到的错误的总结:
    ThinkPHP add save delete的返回说明
    一些实用函数 :去除html标签//去除空白//截取汉字
    group_concat
    linux环境下使用mkdir()函数无法创建目录的问题
    报错:Namespace declaration statement has to be the very first statement in the script的解决方法
    ThinkPHP中,字段更新加1的几种写法
    小狼毫输入法安装与简单配置(windows系统)
    对win10和win11的吐嘈
    看死亡诗社时有的一点新想法
  • 原文地址:https://www.cnblogs.com/chreless2/p/5553833.html
Copyright © 2011-2022 走看看