一.单日期格式

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

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. python核心编程第二版练习题答案

    2-5 #写一个while循环,输出整型为0~10 a=0while a<11: print a a+=1 #写一个for循环重复以上操作 for i in range(11): print i ...

  2. Phoenix综述(史上最全Phoenix中文文档)

    个人主页:http://www.linbingdong.com 简书地址:http://www.jianshu.com/users/6cb45a00b49c/latest_articles 网上关于P ...

  3. IOS FMDB 获取数据库表和表中的数据

    ios开发中,经常会用到数据库sqlite的知识,除了增,删,改,查之外,我们说说如何获取数据库中有多少表和表相关的内容. 前言 跟数据库使用相关的一般的增删改查的语句,这里就不做解释了.在网上有很多 ...

  4. EF6 对多个数据库,多个DBContext的情况 进行迁移的方法。

    参见: http://stackoverflow.com/questions/21537558/multiple-db-contexts-in-the-same-db-and-application- ...

  5. 【知识必备】ezSQL,最好用的数据库操作类,让php操作sql更简单~

    最近用php做了点小东东,用上了ezSQL,感觉真的很ez,所以拿来跟大家分享一下~ ezSQL是一个非常好用的PHP数据库操作类.著名的开源博客WordPress的数据库操作就使用了ezSQL的My ...

  6. Python(九) Python 操作 MySQL 之 pysql 与 SQLAchemy

    本文针对 Python 操作 MySQL 主要使用的两种方式讲解: 原生模块 pymsql ORM框架 SQLAchemy 本章内容: pymsql 执行 sql 增\删\改\查 语句 pymsql ...

  7. ASP.NET Core 中文文档 第四章 MVC(3.7 )局部视图(partial)

    原文:Partial Views 作者:Steve Smith 翻译:张海龙(jiechen).刘怡(AlexLEWIS) 校对:许登洋(Seay).何镇汐.魏美娟(初见) ASP.NET Core ...

  8. C#关于分页显示

    ---<PS:本人菜鸟,大手子还请高台贵手> 以下是我今天在做分页时所遇到的一个分页显示问题,使用拼写SQL的方式写的,同类型可参考哦~ ------------------------- ...

  9. javascript函数

    array.sort(function(a, b){ return a -b ; } )   把数组 array 按照从小到大排序. [11, 22, 586, 10, -58, 86].sort(f ...

  10. 我理解的MVC

    前言 前一阶段对MVC模式及其衍生模式做了一番比较深入的研究和实践,这篇文章也算是一个阶段性的回顾和总结. 经典MVC模式 经典MVC模式中,M是指业务模型,V是指用户界面,C则是控制器,使用MVC的 ...