------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------

上篇博客利用initbinder做了局部的日期类型转换,但是兼容性不要,只支持yyyy-MM-dd这种,所以我们这里进行进一步的优化

其实话说回来了,要想限定格式做最稳定的日期类型转换,就是用日期控件,让用户选,你通过js生成日期数据,这可以省好多麻烦

案例开始:

  1.定义一个自己的日期编辑类,继承PropertiesEditor

package cn.dawn.day22initbinder.editor;

import org.springframework.beans.propertyeditors.PropertiesEditor;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Pattern; /**
* Created by Dawn on 2018/3/31.
*/
public class MyDateEdlitor extends PropertiesEditor {
@Override
/*网线打过来的长的像日期的字符串str*/
public void setAsText(String str) throws IllegalArgumentException {
SimpleDateFormat sdf=getSimpleDate(str);
Date date=null;
try {
date = sdf.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}
setValue(date);
} private SimpleDateFormat getSimpleDate(String str) {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日");
if(Pattern.matches("^\\d{4}-\\d{1,2}-\\d{2}$",str)){
sdf=new SimpleDateFormat("yyyy-MM-dd");
}
if(Pattern.matches("^\\d{4}/\\d{1,2}/\\d{2}$",str)){
sdf=new SimpleDateFormat("yyyy/MM/dd");
}
if(Pattern.matches("^\\d{4}\\d{1,2}\\d{2}$",str)){
sdf=new SimpleDateFormat("yyyyMMdd");
}
return sdf;
}
}

  2.自定义处理器和方法

package cn.dawn.day22initbinder;

import cn.dawn.day22initbinder.editor.MyDateEdlitor;
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; import java.text.SimpleDateFormat;
import java.util.Date; /**
* Created by Dawn on 2018/3/31.
*/ @Controller
public class MultiController {
/*@InitBinder*/
@InitBinder("birthday")
public void initBinder(WebDataBinder binder){
binder.registerCustomEditor(Date.class,new MyDateEdlitor());
} /*initbinder*/
@RequestMapping("/multiinitbinderFirst")
public String multiinitbinderFirst(String username, Integer userage, Date birthday) throws Exception {
System.out.println("");
System.out.println(username);
System.out.println(userage);
System.out.println(birthday);
return "success";
}
}

  3.和上篇博客一样的大配置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: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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <!--包扫描器-->
<context:component-scan base-package="cn.dawn.day22initbinder"></context:component-scan>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/day22/"></property>
<property name="suffix" value=".jsp"></property>
</bean> </beans>

  4.修改web.xml的中央调度器的上下文配置位置为上面这个xml文件

  5.jsp页面:

    5.1login.jsp

<%@ page  pageEncoding="UTF-8" contentType="text/html;charset=UTF-8" language="java" isELIgnored="false"  %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h2>登录</h2>
<form action="${pageContext.request.contextPath}/multiinitbinderFirst" method="post"> 用户名:<input name="username" value="${username}">
年龄:<input name="userage">
生日:<input name="birthday"> <input type="submit" value="登录"/>
</form>
</body>
</html>

    5.2success.jsp

<%@ page language="java" pageEncoding="utf-8" isELIgnored="false" %>
<html>
<body>
<%--<img src="data:image/1.jpg">--%>
<h2>Success!</h2>
</body>
</html>

  6.启动tomcat,访问login.jsp

SSM-SpringMVC-30:SpringMVC中InitBinder的骇客级优化的更多相关文章

  1. Maven+SSM框架(Spring+SpringMVC+MyBatis) - Hello World(转发)

    [JSP]Maven+SSM框架(Spring+SpringMVC+MyBatis) - Hello World 来源:http://blog.csdn.net/zhshulin/article/de ...

  2. 简单易学的SSM(Spring+SpringMVC+MyBatis)整合

    SSM(Spring+SpringMVC+MyBatis)的整合: 具体执行过程:1.用户在页面向后台发送一个请求 2.请求由DispatcherServlet 前端控制器拦截交给SpringMVC管 ...

  3. 使用ssm(spring+springMVC+mybatis)创建一个简单的查询实例(三)(错误整理篇)

    使用ssm(spring+springMVC+mybatis)创建一个简单的查询实例(一) 使用ssm(spring+springMVC+mybatis)创建一个简单的查询实例(二) 以上两篇已经把流 ...

  4. 使用ssm(spring+springMVC+mybatis)创建一个简单的查询实例(二)(代码篇)

    这篇是上一篇的延续: 用ssm(spring+springMVC+mybatis)创建一个简单的查询实例(一) 源代码在github上可以下载,地址:https://github.com/guoxia ...

  5. 使用ssm(spring+springMVC+mybatis)创建一个简单的查询实例(一)

    梳理下使用spring+springMVC+mybatis 整合后的一个简单实例:输入用户的 ID,之后显示用户的信息(此次由于篇幅问题,会分几次进行说明,此次是工程的创建,逆向生成文件以及这个简单查 ...

  6. SpringMVC重定向路径中带中文参数

    SpringMVC重定向路径中带中文参数 springboot重定向到后端接口测试 package com.mozq.http.http_01.demo; import org.springframe ...

  7. SSM(Spring+SpringMVC+MyBatis)高并发优化思路

    SSM(Spring+SpringMVC+MyBatis)框架集由Spring.MyBatis两个开源框架整合而成(SpringMVC是Spring中的部分内容).常作为数据源较简单的web项目的框架 ...

  8. SpringMVC结合easyUI中datagird实现分页

    SpringMVC结合easyUI中datagird实现分页 DataGrid以表格形式展示数据,并提供了丰富的选择.排序.分组和编辑数据的功能支持.轻量级,单元格合并.多列标题.冻结列和页脚只是其中 ...

  9. 搭建SSM(Spring+SpringMVC+Mybatis)

    1.SpringMVC和Spring不需要什么特殊配置就可以结合 2.Mybatis和Spring (1)需要引入额外的jar包:mybatis-spring-1.2.2.jar (2)配置数据源 ( ...

随机推荐

  1. Linux的常用命令(2) - 关机

    关机命令 shutdown‑h now 立即进行关机 shutdown‑r now 现在重新启动计算机 -t sec : -t后面加秒数,即"过几秒后关机" -k      : 不 ...

  2. Android Studio Gradle Configuration Errors总结

    初次看到这个错误,我从下手Error:Configuration with name 'default' not found.  只知道这是由于android的grad项目构建的时候出现的错误,但是具 ...

  3. obj-c编程19:关联对象

    对于一些无法子类化的实例对象来说,如果希望将一个对象与其绑定该如何做呢? 以下示例虚构了一个HyConsoleAlert类,User类将会使用该类在控制台显示定制的告警.如果User中包括多个Aler ...

  4. masm的一些常用编译选项

    ml命令行选项: /Dsymbol[=value] 定义给定名称的文本宏 /Fl 生成lst文件 /Sn lst文件中关闭符号表 /I 设置include文件的路径 /link 发送给link的连接器 ...

  5. obj-c编程17:键值观察(KVO)

    说完了前面一篇KVC,不能不说说它的应用KVO(Key-Value Observing)喽.KVO类似于ruby里的hook功能,就是当一个对象属性发生变化时,观察者可以跟踪变化,进而观察或是修正这个 ...

  6. 从Windows角度看Mac OS X上的软件开发

    如果原来从事Windows软件开发,想跨足或转换至Mac OS X环境,需要知道那些东西?有什么知识技能可以快速运用在Mac OS X环境上的?这两个问题应该是Windows开发者进入Mac OS X ...

  7. jQuery插件之----缓冲运动

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. 2.3MySQL 自带工具使用介绍

    1.mysql 首先看看“-e, --execute=name”参数,这个参数是告诉mysql,我只要执行“-e”后面的某个命令,而不是要通过mysql 连接登录到MySQL Server 上面.此参 ...

  9. java安装及设置环境变量

    目录: java安装 (找不到或无法加载主类 com.sun.tools.javac.Main) 设置环境变量 maven安装及环境变量设置 1. java安装 情景:不使用默认安装 问题:cmd-j ...

  10. 线上Django项目python2到3升级日记

    这两天干了一个几斤疯狂的事情,花不到一个工作日的时间把一个线上Django项目语言版本从python2升级到Python31.字典的一个语法变化 Python2.7: if dict1.haskey( ...