springMVC初始化绑定器
单日期
在处理器类中配置绑定方法 使用@InitBinder注解
在这里首先注册一个用户编辑器 参数一为目标类型 propertyEditor为属性编辑器,此处我们选用 CustomDateEditor属性编辑器,
参数一为想转换的日期格式,参数二表示是否允许为空

package cn.controller; import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
public class MyController{
//处理器方法
@RequestMapping(value="/first.do")
public String doFirst(Date birthday,int age) {
System.out.println("-----------------3333-----------------");
return "/welcome.jsp";
} //自定义一个方法
@InitBinder
public void initBinder(WebDataBinder binder){
DateFormat df=new SimpleDateFormat("yyyy-MM-dd");
binder.registerCustomEditor(Date.class, new CustomDateEditor(df, true));
} }

只需在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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
"> <!-- 包扫描器 -->
<context:component-scan base-package="cn.controller"></context:component-scan> </beans>

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head
<title></title>
</head> <body> <form action="${pageContext.request.contextPath }/first.do" method="post">
出生日期:<input name="birthday" value="${birthday }"/>${birthdayerror }
年龄:<input name="age" value="${age }"/>${ageerror }
<input type="submit" value="注册"/>
</form>
${ex.message }
</body> </html>

welcome.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>欢迎页面</title> </head> <body>
<h1>欢迎访问${uname },${param.uage }</h1>
</body>
</html>

多日期格式
配置步骤:
在处理器类中使用我们自定的属性编辑器
MyController.java

package cn.controller; import java.util.Date;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
public class MyController{
//处理器方法
@RequestMapping(value="/first.do")
public String doFirst(Date birthday,int age) {
System.out.println("--------------------3333--------------------");
return "/welcome.jsp";
} //自定义一个方法
@InitBinder
public void initBinder(WebDataBinder binder){
binder.registerCustomEditor(Date.class, new MyDateEditor());
} }

属性编辑器
此时我们需要考虑使用哪个属性编辑器,需要定义自己的属性编辑器
大致的配置方式如单日期相似,只需要更换属性编辑即可
自定义的属性编辑器,需要我们继承PropertiesEditor,重写里面的setAsText方法,使用setValue方法赋值
MyDateEditor.java

package cn.controller; import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Pattern; import org.springframework.beans.TypeMismatchException;
import org.springframework.beans.propertyeditors.PropertiesEditor; public class MyDateEditor extends PropertiesEditor{ @Override
public void setAsText(String str) throws Exception {
//一旦报错,自动调度到异常处理器
SimpleDateFormat sdf=getDateFormat(str); try {
Date date = sdf.parse(str); setValue(date);
} catch (ParseException e) {
e.printStackTrace();
}
} private SimpleDateFormat getDateFormat(String source){ SimpleDateFormat sdf=new SimpleDateFormat();
if (Pattern.matches("^\\d{4}-\\d{2}-\\d{2}$", source)) {
sdf=new SimpleDateFormat("yyyy-MM-dd");
}else if (Pattern.matches("^\\d{4}/\\d{2}/\\d{2}$", source)) {
sdf=new SimpleDateFormat("yyyy/MM/dd");
}else if (Pattern.matches("^\\d{4}\\d{2}\\d{2}$", source)) {
sdf=new SimpleDateFormat("yyyyMMdd");
}else {
throw new TypeMismatchException("",Date.class);
}
return sdf;
}
}

springMVC初始化绑定器的更多相关文章
- 类型转换器(InitBinder 初始化绑定器)
单日期格式 导入jar包 创建FirstController.java @Controller public class FirstController { /** * @param binder * ...
- 【工作篇】再次熟悉 SpringMVC 参数绑定
前言 主要现在项目中使用的参数绑定五花八门的,搞得很头大,例如有些用字符串接收日期,用字符串接受数组等等,完全没有利用好 SpringMVC 的优势,这里自己也总结一下,免得到时又要百度谷歌查找. 以 ...
- 一步一步自定义SpringMVC参数解析器
随心所欲,自定义参数解析器绑定数据. 题图:from Zoommy 干货 SpringMVC解析器用于解析request请求参数并绑定数据到Controller的入参上. 自定义一个参数解析器需要实现 ...
- SpringMVC【校验器、统一处理异常、RESTful、拦截器】
前言 本博文主要讲解的知识点如下: 校验器 统一处理异常 RESTful 拦截器 Validation 在我们的Struts2中,我们是继承ActionSupport来实现校验的...它有两种方式来实 ...
- Spark源码剖析 - SparkContext的初始化(八)_初始化管理器BlockManager
8.初始化管理器BlockManager 无论是Spark的初始化阶段还是任务提交.执行阶段,始终离不开存储体系.Spark为了避免Hadoop读写磁盘的I/O操作成为性能瓶颈,优先将配置信息.计算结 ...
- [转载]SpringBoot系列: SpringMVC 参数绑定注解解析
本文转载自 https://www.cnblogs.com/morethink/p/8028664.html, 作者写得非常好, 致谢! SpringMVC 参数绑定注解解析 本文介绍了用于参数绑 ...
- 第11课 std::bind和std::function(2)_std::bind绑定器
1. 温故知新:std::bind1st和std::bind2nd (1)bind1st.bind2nd首先它们都是函数模板,用于将参数绑定到可调用对象(如函数.仿函数等)的第1个或第2个参数上. ( ...
- 转 :关于springmvc使用拦截器
原博客: http://elim.iteye.com/blog/1750680 SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的 ...
- 值提供器 AND 模型绑定器
本章介绍了值提供器的作用,ASP MVC自带的5中值提供器.以及模型绑定器的作用,自定义模型绑定器并使用自定义的模型绑定器(类型上加上[ModelBinder(typeof(xx))]或者在全局模型绑 ...
随机推荐
- 论:开发者信仰之“天下IT是一家“(Java .NET篇)
比尔盖茨公认的IT界领军人物,打造了辉煌一时的PC时代. 2008年,史蒂夫鲍尔默接替了盖茨的工作,成为微软公司的总裁. 2013年他与微软做了最后的道别. 2013年以后,我才真正看到了微软的变化. ...
- DailyTick 开发实录 —— 开始
2009 年我读了李笑来老师的<把时间当朋友>,知识了柳比歇夫的时间记录法.当时激动坏了,马上动手实践起来.一开始的时候,是用一个小本子,走到哪儿都带着.完成一件事,就记录一下花费的时间. ...
- StringMVC 中如何做数据校验
步骤一:引入四个jar包 步骤二:注册类型转换器 <context:component-scan base-package="cn.happy.controller"> ...
- Java多线程
一:进程与线程 概述:几乎任何的操作系统都支持运行多个任务,通常一个任务就是一个程序,而一个程序就是一个进程.当一个进程运行时,内部可能包括多个顺序执行流,每个顺序执行流就是一个线程. 进程:进程 ...
- 继电器是如何成为CPU的(1)
继电器是如何成为CPU的(1) ——<穿越计算机的迷雾>整理和总结 究竟是如何设计的电路,具有计算和控制的智力? 这一点也不高深.本系列文章从初中学的最简单的电路图说起,看看能不能从最初的 ...
- CSS浮动、定位
这几天有空,整理了关于CSS浮动和定位的一些知识点,有什么欠缺的地方,欢迎大家批评指正. 一.文档流的概念指什么?有哪种方式可以让元素脱离文档流? 文档流,指的是元素排版布局过程中,元素会自动从左往右 ...
- mybatis_开发篇
一.使用mybatis的动态代理方式开发 需求:这里以crm系统中分页条件查询所有的客户信息的功能为例? 1.创建工程 2.引入所需的jar包 3.引入日志文件.数据库连接参数的配置文件等 4.创建m ...
- 设计模式之创建类模式大PK
创建类模式大PK 创建类模式包括工厂方法模式.建造者模式.抽象工厂模式.单例模式和原型模式,他们能够提供对象的创建和管理职责.其 ...
- 浅谈JSP中include指令与include动作标识的区别
JSP中主要包含三大指令,分别是page,include,taglib.本篇主要提及include指令. include指令使用格式:<%@ include file="文件的绝对路径 ...
- Java类访问权限修饰符
一.概要 通过了解Java4种修饰符访问权限,能够进一步完善程序类,合理规划权限的范围,这样才能减少漏洞.提高安全性.具备表达力便于使用. 二.权限表 修饰符 同一个类 同一个包 不同包的子类 不同包 ...