由于日期数据有很多种格式,所以springmvc没办法把字符串转换成日期类型。所以需要自定义参数绑定。前端控制器接收到请求后,找到注解形式的处理器适配器,对RequestMapping标记的方法进行适配,并对方法中的形参进行参数绑定。在springmvc这可以在处理器适配器上自定义Converter进行参数绑定。如果使用<mvc:annotation-driven/>可以在此标签上进行扩展。

  步骤:自定义Converter

  

public class DateConverter implements Converter<String, Date> {

//String (原数据的类型),Date(转换后的数据类型)

@Override

public Date convert(String source) {

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

try {

return simpleDateFormat.parse(source);

} catch (ParseException e) {

e.printStackTrace();

}

return null;

}

}

配置Converter(在springmvc的主配置文件中配置)

      

<!-- 加载注解驱动 -->

<mvc:annotation-driven conversion-service="conversionService"/>

<!-- 转换器配置 -->

<bean id="conversionService"

class="org.springframework.format.support.FormattingConversionServiceFactoryBean">

<property name="converters">

<set>  

<bean class="cn.kingdee.springmvc.convert.DateConverter"/>

</set>

</property>

</bean>

配置方式2(了解)

    

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd

        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd

        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

<!-- 扫描带Controller注解的类 -->

<context:component-scan base-package="cn.itcast.springmvc.controller" />

<!-- 转换器配置 -->

<bean id="conversionService"

class="org.springframework.format.support.FormattingConversionServiceFactoryBean">

<property name="converters">

<set>

<bean class="cn.kingdee.springmvc.convert.DateConverter"/>

</set>

</property>

</bean>

<!-- 自定义webBinder -->

<bean id="customBinder" class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">

<property name="conversionService" ref="conversionService" />

</bean>

<!--处理器适配器 -->

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">

<property name="webBindingInitializer" ref="customBinder"></property>

</bean>

<!-- 注解处理器映射器 -->

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>

<!-- 加载注解驱动 -->

<!-- <mvc:annotation-driven/> -->

<!-- 视图解析器 -->

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="viewClass"

value="org.springframework.web.servlet.view.JstlView" />

<!-- jsp前缀 -->

<property name="prefix" value="/WEB-INF/jsp/" />

<!-- jsp后缀 -->

<property name="suffix" value=".jsp" />

</bean>

</beans>

SpringMVC中的自定义参数绑定案例的更多相关文章

  1. SpringMVC由浅入深day01_12.4 pojo绑定_12.5自定义参数绑定实现日期类型绑定_12.6集合类

    12.4 pojo绑定 页面中input的name和controller的pojo形参中的属性名称一致,将页面中数据绑定到pojo. 页面定义: controller的pojo形参的定义: 打断点测试 ...

  2. SSM框架之SpringMVC(2)参数绑定及自定义类型转换

    SpringMVC(2)参数绑定及自定义类型转换 1.请求参数的绑定 1.1. 请求参数的绑定说明 1.1.1.绑定机制 表单提交的数据都是k=v格式的 username=haha&passw ...

  3. SpringMVC 自定义参数绑定实现日期类型绑定

    package cn.itcast.ssm.controller.converter; import java.text.ParseException; import java.text.Simple ...

  4. SpringMVC框架笔记02_参数绑定返回值文件上传异常处理器JSON数据交互_拦截器

    目录 第1章 高级参数的绑定 1.1 参数的分类 1.2 数组类型的参数的绑定 1.3 集合类型的参数的绑定 第2章 @RequestMapping的用法 2.1 URL路径映射 2.2 请求方法限定 ...

  5. JAVAEE——SpringMVC第一天:介绍、入门程序、架构讲解、SpringMVC整合MyBatis、参数绑定、SpringMVC和Struts2的区别

    1. 学习计划   第一天 1.SpringMVC介绍 2.入门程序 3.SpringMVC架构讲解 a) 框架结构 b) 组件说明 4.SpringMVC整合MyBatis 5.参数绑定 a) Sp ...

  6. SpringMVC之Controller和参数绑定

    在上一篇Spring+SpringMVC+Mybatis整合中说到了SSM的整合,并且在其中添加了一个简单的查询功能,目的只是将整个整合的流程进行一个梳理,下面在上一篇中工程的基础上再说一些关于Spr ...

  7. Springmvc入门基础(四) ---参数绑定

    1.默认支持的参数类型 处理器形参中添加如下类型的参数处理适配器会默认识别并进行赋值. 除了ModelAndView以外,还可以使用Model来向页面传递数据, Model是一个接口,在参数里直接声明 ...

  8. SpringMVC中post请求参数注解@requestBody使用问题

    一.httpClient发送Post 原文https://www.cnblogs.com/Vdiao/p/5339487.html public static String httpPostWithJ ...

  9. Spring boot中普通工具类不能使用@Value注入yml文件中的自定义参数的问题

    在写一个工具类的时候,因为要用到yml中的自定义参数,使用@Value发现值不能正常注入,都显示为null: yml文件中的自定义格式 调用工具类的时候不能new的方式 要使用@Autowired的方 ...

随机推荐

  1. NPM常用命令install 淘宝镜像 update等

    NPM是随同NodeJS一起安装的包管理工具,允许用户从NPM服务器上传下载安装第三方包或命令行程序,能解决NodeJS代码部署上的很多问题,非常方便.下面我们一起来看看常用的npm命令有哪些 使用方 ...

  2. 帝国cms内容关键字自动加链接且设置内容关键字只替换一次

    网站上线前先设置一些内部链接对后期的优化排名很有帮助,帝国cms也可以设置文章中的关键字自动加链接,但是要注意一下关键词替换次数,最好是1次. 怎么操作呢?分两步完成 1.帝国cms文章关键字自动加链 ...

  3. 数据库 - Navicat与pymysql模块

    一.Nabicat 在生产环境中操作MySQL数据库还是推荐使用命令行工具mysql,但在我们自己开发测试时, 可以使用可视化工具Navicat,以图形界面的形式操作MySQL数据库 官网下载:htt ...

  4. testrem

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...

  5. sap 下载程序

    1: 左下角,直接输入 software centre.  搜索相应程序下载.

  6. [LeetCode] 613. Shortest Distance in a Line_Easy tag: SQL

    Table point holds the x coordinate of some points on x-axis in a plane, which are all integers. Writ ...

  7. iOS 开发笔记-加载/初始化

    ViewDidLoad 一般我们会在这里做界面上的初始化操作,比如往view中添加一些子视图.从数据库或者网络加载模型数据装配到子视图中 在自定义控制里 initWithFrame:一般用于添加控件, ...

  8. vue中定时器的使用方式

    就这么搞定 no no no  离开页面的时候还必须清楚定时器

  9. MVC 中url-pattern配置为"/"和"/*"的区别

    首先大家都知道"/*"可以匹配所有url,包括带扩展名的,一般只用在过滤器上. 而"/"很多人理解成不能拦截带扩展名的,这种理解是错误的!它其实也能拦截“.js ...

  10. shell基础:输入输出重定向

    输出重定向将命令输出存入到文件,类似日志.便于查看.2和>>间没空格.但这种方法没用 ,命令执行时并不知道对错. /dev/null下的null就是一个垃圾箱,脚本中的一些命令并不需要保存 ...