SSM-SpringMVC-30:SpringMVC中InitBinder的骇客级优化
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------
上篇博客利用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("2222222222"); 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="image/1.jpg">--%> <h2>Success!</h2> </body> </html>
6.启动tomcat,访问login.jsp