SSM-SpringMVC-30:SpringMVC中InitBinder的駭客級優化
阿新 • • 發佈:2018-03-31
void body str end .text pin efi edate mda
------------吾亦無他,唯手熟爾,謙卑若愚,好學若饑-------------
上篇博客利用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
SSM-SpringMVC-30:SpringMVC中InitBinder的駭客級優化