一.单日期格式

因为是用注解完完成的后台访问,所以必须在大配置中配置包扫描器;

1.applicactionContext.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.happy.controller"></context:component-scan>
</beans>

2.配置类

package cn.happy.controller;

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 FirstController {
/**
* @param binder
*/ @InitBinder
public void initBinder(WebDataBinder binder){
//PropertyEditor
//类 implements PropertyEditor
binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
}
@RequestMapping("/first.do")
//类型转化工作一定是在真正的handler方法执行前执行的。
public String doFirst(Date birthday,int age) throws Exception{
System.out.println(birthday+"===============");
System.out.println(age+"===============");
return "/WELCOME.jsp";
} }

3.前台

    <form action="${pageContext.request.contextPath }/first.do" method="post">
出生日期:<input name="birthday"/><br/><br/>
年龄:<input name="age"/><br/><br/>
<input type="submit" value="注册"/>
</form>

二:多日期格式

1.因为并不是一种格式,所以我们可以把能进转换的格式提到一个类中

package cn.happy.controller;

import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
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 source) throws IllegalArgumentException { SimpleDateFormat sdf=getDate(source);
//做一个操作
try {
setValue(sdf.parseObject(source));
} catch (ParseException e) {
e.printStackTrace();
} } private SimpleDateFormat getDate(String source) {
SimpleDateFormat sdf=null;
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;
} }

2、还是用@initBinder 注解处理类型

package cn.happy.controller;

import java.beans.PropertyEditor;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date; import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.PropertiesEditor;
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 FirstController {
/**
*
* @param binder
*/
@InitBinder
public void initBinder(WebDataBinder binder){
binder.registerCustomEditor(Date.class,new MyDateEditor());
System.out.println("===============11111"); } @RequestMapping("/first.do")
//类型转化工作一定是在真正的handler方法执行前执行的。
public String doFirst(Date birthday,int age) throws Exception{
System.out.println(birthday+"===============");
System.out.println(age+"===============");
return "/WELCOME.jsp";
} }

Spring mvc @initBinder 类型转化器的使用的更多相关文章

  1. SpringMVC09异常处理和类型转化器

    public class User { private String name; private Integer age; public String getName() { return name; ...

  2. jQuery源码分析系列(36) : Ajax - 类型转化器

    什么是类型转化器? jQuery支持不同格式的数据返回形式,比如dataType为 xml, json,jsonp,script, or html 但是浏览器的XMLHttpRequest对象对数据的 ...

  3. spring mvc中的拦截器小结 .

    在spring mvc中,拦截器其实比较简单了,下面简单小结并demo下. preHandle:预处理回调方法,实现处理器的预处理(如登录检查),第三个参数为响应的处理器(如我们上一章的Control ...

  4. spring mvc 形参类型

    spring mvc 形参类型 1 没有占位符号的,形参的名字为参数的名称 请求路径为:organtrans/t1/t5?a=1(a为形参的的名称必须一致) @RequestMapping(" ...

  5. struts2类型转化器详解(带例子)

    Struts2有两种类型转化器: 一种局部,一种全局. 如何实现: 第一步:定义转化器 第二部:注册转化器 下面做一个局部类型转化器的实例. 我们在上面一片日志说过有个变量date类型的.只有我们输入 ...

  6. Spring MVC中的拦截器/过滤器HandlerInterceptorAdapter的使用

    一般情况下,对来自浏览器的请求的拦截,是利用Filter实现的 而在Spring中,基于Filter这种方式可以实现Bean预处理.后处理. 比如注入FilterRegistrationBean,然后 ...

  7. Spring Mvc 的自定义拦截器

     spring mvc的拦截器 SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理.比如通过它来进行权限验证,或者是来判断用户 ...

  8. Spring MVC的视图解析器

    一.视图解析器简介 在Spring MVC中,当Controller将请求处理结果放入到ModelAndView中以后,DispatcherServlet会根据ModelAndView选择合适的视图进 ...

  9. [Spring MVC] - InitBinder验证

    Spring MVC使用InitBinder验证: 使用InitBinder做验证的情况一般会在此Controller中提交的数据需要有一些是业务性质的,也即比较复杂的验证情况下才会使用.大部份简单的 ...

随机推荐

  1. 理解CSS边框border

    前面的话   边框是CSS盒模型属性中默默无闻的一个普通属性,CSS3的到来,但得边框属性重新焕发了光彩.本文将详细介绍CSS边框 基础样式   边框是一条以空格分隔的集合样式,包括边框粗细(边框宽度 ...

  2. MVVM模式和在WPF中的实现(一)MVVM模式简介

    MVVM模式解析和在WPF中的实现(一) MVVM模式简介 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二)数据绑定 MVVM模式解析和在 ...

  3. 回首经典的SQL Server 2005

    原创文章转载请注明出处:@协思, http://zeeman.cnblogs.com SQL Server是我使用时间最长的数据库,算起来已经有10年了.上世纪90年代,微软在软件开发的所有领域高歌猛 ...

  4. JavaScript性能优化

    如今主流浏览器都在比拼JavaScript引擎的执行速度,但最终都会达到一个理论极限,即无限接近编译后程序执行速度. 这种情况下决定程序速度的另一个重要因素就是代码本身. 在这里我们会分门别类的介绍J ...

  5. C# 条形码操作【源码下载】

    本篇介绍通过C#生成和读取一维码.二维码的操作. 目录 1. 介绍:介绍条形码.条形码的分类以及ZXing.Net类库. 2. 一维码操作:包含对一维码的生成.读取操作. 3. 二维码操作:包含对二维 ...

  6. Electron使用与学习--(页面间的通信)

    目录结构: index.js是主进程js. const electron = require('electron') const app = electron.app const BrowserWin ...

  7. 构建通用的 React 和 Node 应用

    这是一篇非常优秀的 React 教程,这篇文章对 React 组件.React Router 以及 Node 做了很好的梳理.我是 9 月份读的该文章,当时跟着教程做了一遍,收获很大.但是由于时间原因 ...

  8. 主成分分析(PCA)原理总结

    主成分分析(Principal components analysis,以下简称PCA)是最重要的降维方法之一.在数据压缩消除冗余和数据噪音消除等领域都有广泛的应用.一般我们提到降维最容易想到的算法就 ...

  9. ASP.NET Core: You must add a reference to assembly mscorlib, version=4.0.0.0

    ASP.NET Core 引用外部程序包的时候,有时会出现下面的错误: The type 'Object' is defined in an assembly that is not referenc ...

  10. 【转】39个让你受益的HTML5教程

    闲话少说,本文作者为大家收集了网上学习HTML5的资源,期望它们可以帮助大家更好地学习HTML5. 好人啊! 不过,作者原来说的40个只有39个,因为第5个和第8个是重复的. 原文在此! 1. 五分钟 ...