spring mvc绑定参数之类型转换有三种方式:

1.实体类中加日期格式化注解(上次做项目使用的这种。简单,但有缺点,是一种局部的处理方式,只能在本实体类中使用。方法三是全局的。)

@DateTimeFormat(pattern="yyyy-MM-dd HH:mm")
private Date creationTime;

2.属性编辑器(复杂)

spring3.1之前

在Controller类中通过@InitBinder完成

/**

  • 在controller层中加入一段数据绑定代码
    @param webDataBinder
    */
    @InitBinder
    public void initBinder(WebDataBinder webDataBinder) throws Exception{
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm");
    simpleDateFormat.setLenient(false);
    webDataBinder.registerCustomEditor(Date.class , new CustomDateEditor(simpleDateFormat , true));
    }
    备注:自定义类型转换器必须实现PropertyEditor接口或者继承PropertyEditorSupport类
    写一个类 extends propertyEditorSupport(implements PropertyEditor){
    public void setAsText(String text){
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy -MM-dd hh:mm");
    Date date = simpleDateFormat.parse(text);
    this.setValue(date);
    }
    public String getAsTest(){
    Date date = (Date)this.getValue();
    return this.dateFormat.format(date);
    }
    }

3. 类型转换器Converter

全局类型转换

参考前期课程代码

spring mvc绑定参数之 类型转换 有三种方式:的更多相关文章

  1. spring mvc绑定参数之日期类型转换

    问题复现步骤: 1.提交表单,参数为: 2.接收参数类型为product实体类,如下 3.错误日志如下: org.springframework.validation.BindException: o ...

  2. spring mvc 绑定参数据默认值,是否必传,(RequestParam(value="id",defaultValue="1",required=true) )

    @RequestMapping(value = "/detail", method = RequestMethod.GET) public String newDetail(@Re ...

  3. spring MVC 拦截有几种实现方式

    spring MVC 拦截有几种实现方式 实现HandelInterceptor接口方式        继承HandelInterceptor 的方式.一般有这两种方式 spring 如何走单元测式 ...

  4. Spring MVC处理异常的4种方式

    http://blog.csdn.net/ufo2910628/article/details/40399539 http://my.oschina.net/CandyDesire/blog/3333 ...

  5. Spring MVC 处理异常的3种方式

    使用Spring MVC开发的博客网站时,遇到了如何处理业务层抛出的异常的问题,查阅到了spring官方博客-spring MVC中异常的处理,以下将会以登录模块为示例. 愚蠢的处理方式 处理异常遵循 ...

  6. Spring MVC JSON自己定义类型转换(续)

    前面提到了两种转换类型的方法(Spring MVC JSON自己定义类型转换),这里针对Json转换提供一种更简便的方法. 通过配置全局的日期转换来避免使用麻烦的注解. 首先用到了一个简单的日期工具类 ...

  7. Spring MVC接收参数(Map,List,JSON,Date,2个Bean)(记录一次面试惨状)

    题目Spring MVC 接收参数 MapListDate2个BeanJSON Spring MVC接收参数 -Map Spring MVC接收参数 -List Spring MVC接收参数 -dat ...

  8. Spring MVC温故而知新 – 参数绑定、转发与重定向、异常处理、拦截器

    请求参数绑定 当用户发送请求时,根据Spring MVC的请求处理流程,前端控制器会请求处理器映射器返回一个处理器,然后请求处理器适配器之心相应的处理器,此时处理器映射器会调用Spring Mvc 提 ...

  9. Spring MVC请求参数绑定

    所谓请求参数绑定,就是在控制器方法中,将请求参数绑定到方法参数上 @RequestParam 绑定单个请求参数到方法参数上 @RequestParam("id") Integer ...

随机推荐

  1. Springmvc-crud-07(springmvc标签错误)

    错误:springmvc标签错误 原因:1.在springmvc中的form标签中没有绑定modelAttribute属性 2.必须要获取到参数(可以创建map对象,进行存储参数,再用modelAtt ...

  2. source insight 编译后出现停止工作解决方法

    解决方法: 工程的路径不要有中文,都用英文

  3. JAVA常量池、栈、堆的比较(转载)

    今天在学JAVA的数据存储位置的时候,看到了一篇博文感觉不错,特此转载: http://www.cnblogs.com/Eason-S/p/5658230.html JAVA中,有六个不同的地方可以存 ...

  4. Spring_第一个Spring入门案例IOC

    今天我们来写我们的第一个spring 第一步 建立一个java project 第二步 添加我们的五个jar文件 第三步 在项目中建立一个com.zk.spring包 第四步 建立我们的userser ...

  5. jQuery选择器的使用注意事项:

    1. 选择其中含有特殊符号 W3C规范规定属性值中不能含有某些特殊字符,但在实际开发过程中,常遇到表达式中含有“#”或“.”等特殊字符的情况,如果按照普通的方式去处理就会出错,解决此类问题的方法就是使 ...

  6. Centos7 将应用添加快捷方式到applications 中以pycham为例[ubuntu]适用

    安装版本pycharm-2019.1.3 安装路径:/opt/pycharm-2019.1.3/ vim /usr/share/applications/pycharm.desktop #!/usr/ ...

  7. MyBatis-Plus学习笔记(3):分页查询

    依赖配置可参考:MyBatis-Plus学习笔记(1):环境搭建以及基本的CRUD操作 分页配置 @Configuration public class PlusConfig { @Bean publ ...

  8. 使用 vant 的 v-lazy 实现图片 vue 在移动端的懒加载

    官方文档:https://youzan.github.io/vant/#/zh-CN/lazyload 引入 Lazyload 是 Vue 指令,使用前需要对指令进行注册 import Vue fro ...

  9. PB 数据窗口点击标题不能排序的一个原因

    标题必须和数据行名称一致,如 数据行列名为:num ,标题行必须为 num_t 才可以

  10. 2016-2017学年第三次测试赛 问题 F: 签到题

    问题 F: 签到题 时间限制: 1 Sec  内存限制: 128 MB提交: 80  解决: 28 提交统计讨论版 题目描述 在计算机网络考试中, 黑帅男神看到一个将IP网络分类的题, 精通C++的他 ...