springmvc的初始化参数绑定
一、springmvc的初始化参数绑定
此种和我们之前说的类型转换非常相似,可以看作是一种类型转换
在初始化参数绑定时 重要的是参数类型
-------------------单日期的绑定
二、 配置步骤:
步骤一:在applicationcontext.xml中只需要配置一个包扫描器即可
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd "> <!--让spring扫描包下所有的类,让标注spring注解的类生效 -->
<context:component-scan base-package="cn.yxj.controller"/> </beans>
步骤二:在处理器类中配置绑定方法 使用@InitBinder注解
在这里首先注册一个用户编辑器 参数一为目标类型 propertyEditor为属性编辑器,此处我们选用 CustomDateEditor属性编辑器,
参数一为想转换的日期格式,参数二表示是否允许为空


@InitBinder
public void databinder(WebDataBinder binder){
System.out.println("11111");
DateFormat df=new SimpleDateFormat("yyyy-MM-dd");
binder.registerCustomEditor(Date.class,new CustomDateEditor(df, true) );
}
单个日期的参数绑定配置完毕
-------------------多日期的绑定
配置步骤:
1.属性编辑器
此时我们需要考虑使用哪个属性编辑器,需要定义自己的属性编辑器
大致的配置方式如单日期相似,只需要更换属性编辑即可
自定义的属性编辑器,需要我们继承PropertiesEditor,重写里面的setAsText方法,使用setValue方法赋值
package cn.yxj.propertyEdit; import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Pattern; import org.springframework.beans.propertyeditors.PropertiesEditor; public class MyDataEdit extends PropertiesEditor{ @Override
public void setAsText(String text) throws IllegalArgumentException {
SimpleDateFormat sdf=getDateFormat(text);
try {
Date date = sdf.parse(text);
setValue(date);
} catch (ParseException e) {
e.printStackTrace();
}
} private SimpleDateFormat getDateFormat(String text) {
if(Pattern.matches("^\\d{4}-\\d{2}-\\d{2}$", text)){
return new SimpleDateFormat("yyyy-MM-dd");
}else if(Pattern.matches("^\\d{4}/\\d{2}/\\d{2}$", text)){
return new SimpleDateFormat("yyyy/MM/dd");
}else if(Pattern.matches("^\\d{4}\\d{2}\\d{2}$", text)){
return new SimpleDateFormat("yyyyMMdd");
}else if(Pattern.matches("^\\d{4}年\\d{2}月\\d{2}日$", text)){
return new SimpleDateFormat("yyyy年MM月dd日");
}
return null;
} }
步骤二:在处理器类中使用我们自定的属性编辑器
@InitBinder
public void databinder(WebDataBinder binder){
System.out.println("11111");
DateFormat df=new SimpleDateFormat("yyyy-MM-dd");
binder.registerCustomEditor(Date.class,new MyDataEdit() );
}
springmvc的初始化参数绑定的更多相关文章
- SpringMvc中初始化参数绑定
初始化参数绑定与类型转换很类似,初始化绑定时,主要是参数类型 ---单日期 在处理器类中配置绑定方法 使用@InitBinder注解 在这里首先注册一个用户编辑器 参数一为目标类型 proper ...
- SpringMVC 的初始化参数绑定
初始化参数绑定:日期格式 一:首先我们先做一种日期格式的绑定,配置初始化参数绑定和自定义类型转换有着异曲同工之妙 配置步骤如下: 1.我们首先配置applicationContext.xml,进行扫描 ...
- SpringMVC初始化参数绑定--日期格式
一.初始化参数绑定[一种日期格式] 配置步骤: ①:在applicationcontext.xml中只需要配置一个包扫描器即可 <!-- 包扫描器 --> <context:comp ...
- Spring MVC初始化参数绑定
初始化参数绑定与类型转换很类似,初始化绑定时,主要是参数类型 ---单日期 在处理器类中配置绑定方法 使用@InitBinder注解 在这里首先注册一个用户编辑器 参数一为目标类型 proper ...
- [Spring MVC] - SpringMVC的各种参数绑定方式
SpringMVC的各种参数绑定方式 1. 基本数据类型(以int为例,其他类似):Controller代码: @RequestMapping("saysth.do") publi ...
- SpringMVC的各种参数绑定方式
1. 基本数据类型(以int为例,其他类似):2. 包装类型(以Integer为例,其他类似):3. 自定义对象类型:4. 自定义复合对象类型:5. List绑定:6. Set绑定:7. Map绑定: ...
- [转载]SpringMVC的Model参数绑定方式
SpringMVC的各种参数绑定方式 http://www.cnblogs.com/HD/p/4107674.html springMVC中复杂嵌套对象.List等集合类型数据绑定 http://ww ...
- SpringMVC介绍及参数绑定
本节内容: SpringMVC介绍 入门程序 SpringMVC架构 SpringMVC整合MyBatis 参数绑定 SpringMVC和Struts2的区别 一.SpringMVC介绍 1. 什么是 ...
- SpringMVC中的参数绑定总结
众所周知,springmvc是用来处理页面的一些请求,然后将数据再通过视图返回给用户的,前面的几篇博文中使用的都是静态数据,为了能快速入门springmvc,在这一篇博文中,我将总结一下springm ...
随机推荐
- C# BS消息推送 SignalR Hubs环境搭建与开发(二)
1. 前言 本文是根据网上前人的总结得出的. 环境: SignalR2.x,VS2015,Win10 2. 开始开发 1)新建一个MVC项目,叫做SignalRDemo 2)安装SignalR包 In ...
- 逻辑回归(LR)总结复习
摘要: 1.算法概述 2.算法推导 3.算法特性及优缺点 4.注意事项 5.实现和具体例子 6.适用场合 内容: 1.算法概述 最基本的LR分类器适合于对两分类(类0,类1)目标进行分类:这个模型以样 ...
- AngularJS 源码分析2
上一篇地址 本文主要分析RootScopeProvider和ParseProvider RootScopeProvider简介 今天这个rootscope可是angularjs里面比较活跃的一个pro ...
- Java基础-多线程编程-1.随便选择两个城市作为预选旅游目标。实现两个独立的线程分别显示10次城市名,每次显示后休眠一段随机时间(1000ms以内),哪个先显示完毕,就决定去哪个城市。分别用Runnable接口和Thread类实现。
1.随便选择两个城市作为预选旅游目标.实现两个独立的线程分别显示10次城市名,每次显示后休眠一段随机时间(1000ms以内),哪个先显示完毕,就决定去哪个城市.分别用Runnable接口和Thread ...
- 关于ie11 的开发者工具
win7旗舰系统64为,更新ie11: 新安装了ie11浏览器,安装以后发现原来可以正常使用的开发者工具不能使用,提示 Imposible use F12 Developer Tools (Excep ...
- 利用扩展事件进行调优和Troubleshooting PPT分享
本篇主题是我在2015年中国数据库大会(DTCC)上的分享,扩展事件从2008版本出来到现在已经有6-7年,国内却很少有相关资料和使用,现在分享一下PPT,希望对大家有所帮助. 可 ...
- 给Easyui combobox设定默认值
今天做到那个北理工二期的项目,里面刚好有几个dialog需要弄一个默认值,一般是选择启用与否,但是,为了方便用户,最好有一个默认值,所以,增加一个默认值的属性.代码入下: JS代码: ...
- 尽量使用translate而不是改变top/left进行动画(翻译)
前言 本文翻译自 Why Moving Elements With Translate() Is Better Than Pos:abs Top/left,本文有改动,添加了一些作者自己的理解,不当之 ...
- Solr学习总结(三)Solr web 管理后台
前面讲到了Solr的安装,按道理,这次应该讲讲.net与数据库的内容,C#如何操作Solr索引等.不过我还是想先讲一些基础的内容,比如solr查询参数如何使用,各个参数都代表什么意思? 还有solr ...
- 【LeetCode】House Robber III(337)
1. Description The thief has found himself a new place for his thievery again. There is only one ent ...