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

上篇博客利用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网络设置(第二版) --互联网寻址过程

    Linux网络设置 --互联网寻址过程 1.TCP/IP与OSI参考模型比较 TCP/IP OSI 物理层 网卡 数据链路层 * MAC地址 网络层 IP,ICMP,ARP协议 传输层 TCP,UDP ...

  2. 认识Zygote

    概述 在java中不同的虚拟机实例会为不同的应用分配不同内存,为了使Android系统尽快启动,使用了Zygote来预加载核心类库和一些加载时间长的类(超过1250ms),让Dalvik虚拟机共享代码 ...

  3. HBase Compaction

    当 client 向 hregion 端 put() 数据时, HRegion 会判断当前的 memstore 的大小是否大于参数hbase.hregion.memstore.flush.size 值 ...

  4. 在GitHub上创建代码仓库

    目前在GitHub上管理托管带代码的人越来越多了,今天也尝试了一次,顺便记下来,备用. 首先是在GitHub上创建一个代码仓库,创建完之后,GitHub上会有提示,这时进入项目目录执行下面的命令,顺便 ...

  5. leetcode之旅(7)-Move Zeroes

    Move Zeroes 题目描述: Given an array nums, write a function to move all 0's to the end of it while maint ...

  6. BCD码转十进制C语言实现

    #include <stdio.h> #include <stdlib.h> #define uchar unsigned char uchar BCD_Decimal(uch ...

  7. palindrome number(回文数)

    Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negativ ...

  8. 关于MVC的特性(AuthorizeAttribute)的一些理解

    许多 Web 应用程序要求在用户登录之后才授予其对受限制内容的访问权限. 在某些应用程序中,即使是登录的用户,也会限制他们可以查看的内容或可以编辑的字段. 要限制对 ASP.NET MVC 视图的访问 ...

  9. 菜鸟级Git GitHub创建仓库

    菜鸟标准:知道pwd ,rm 命令是什么. 一.Git 是什么. git 是目前世界上最先进的分布式版本控制系统 二.SVN与Git 1.版本控制系统 SVN 是集中式版本控制系统,版本库是集中放在中 ...

  10. 前端Mahsup异步依赖方式不能做业务数据依赖

    很久之前流行mashup方式做内容集成,之前为了IP定位的方便,引用了第三方的IP定位JS,然后根据其内容与服务器同步地址数据并写入Cookie,可是这种方式一旦,第三方的库反应缓慢时,就会出现大问题 ...