SSM-SpringMVC-30:SpringMVC中InitBinder的骇客级优化

释放双眼,带上耳机,听听看~!

 

 

 

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

 

 

 

 

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

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

 

案例开始:

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

 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
1package cn.dawn.day22initbinder.editor;
2
3import org.springframework.beans.propertyeditors.PropertiesEditor;
4
5import java.text.ParseException;
6import java.text.SimpleDateFormat;
7import java.util.Date;
8import java.util.regex.Pattern;
9
10/**
11 * Created by Dawn on 2018/3/31.
12 */
13public class MyDateEdlitor extends PropertiesEditor {
14    @Override
15    /*网线打过来的长的像日期的字符串str*/
16    public void setAsText(String str) throws IllegalArgumentException {
17        SimpleDateFormat sdf=getSimpleDate(str);
18        Date date=null;
19        try {
20            date = sdf.parse(str);
21        } catch (ParseException e) {
22            e.printStackTrace();
23        }
24        setValue(date);
25    }
26
27
28    private SimpleDateFormat getSimpleDate(String str) {
29        SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日");
30        if(Pattern.matches("^\\d{4}-\\d{1,2}-\\d{2}$",str)){
31            sdf=new SimpleDateFormat("yyyy-MM-dd");
32        }
33        if(Pattern.matches("^\\d{4}/\\d{1,2}/\\d{2}$",str)){
34            sdf=new SimpleDateFormat("yyyy/MM/dd");
35        }
36        if(Pattern.matches("^\\d{4}\\d{1,2}\\d{2}$",str)){
37            sdf=new SimpleDateFormat("yyyyMMdd");
38        }
39        return sdf;
40    }
41}
42

 

2.自定义处理器和方法

 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
1package cn.dawn.day22initbinder;
2
3import cn.dawn.day22initbinder.editor.MyDateEdlitor;
4import org.springframework.beans.propertyeditors.CustomDateEditor;
5import org.springframework.stereotype.Controller;
6import org.springframework.web.bind.WebDataBinder;
7import org.springframework.web.bind.annotation.InitBinder;
8import org.springframework.web.bind.annotation.RequestMapping;
9
10import java.text.SimpleDateFormat;
11import java.util.Date;
12
13/**
14 * Created by Dawn on 2018/3/31.
15 */
16
17@Controller
18public class MultiController {
19    /*@InitBinder*/
20    @InitBinder("birthday")
21    public void initBinder(WebDataBinder binder){
22        binder.registerCustomEditor(Date.class,new MyDateEdlitor());
23    }
24
25
26    /*initbinder*/
27    @RequestMapping("/multiinitbinderFirst")
28    public String multiinitbinderFirst(String username, Integer userage, Date birthday) throws Exception {
29        System.out.println("2222222222");
30        System.out.println(username);
31        System.out.println(userage);
32        System.out.println(birthday);
33        return "success";
34    }
35}
36

 

3.和上篇博客一样的大配置xml文件,不过我也放上来

 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
1<?xml version="1.0" encoding="UTF-8"?>
2<beans xmlns="http://www.springframework.org/schema/beans"
3       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4       xmlns:mvc="http://www.springframework.org/schema/mvc"
5       xmlns:context="http://www.springframework.org/schema/context"
6       xsi:schemaLocation="http://www.springframework.org/schema/beans
7        http://www.springframework.org/schema/beans/spring-beans.xsd
8        http://www.springframework.org/schema/mvc
9        http://www.springframework.org/schema/mvc/spring-mvc.xsd
10         http://www.springframework.org/schema/context
11         http://www.springframework.org/schema/context/spring-context.xsd">
12
13    <!--包扫描器-->
14    <context:component-scan base-package="cn.dawn.day22initbinder"></context:component-scan>
15    <!--视图解析器-->
16    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
17        <property name="prefix" value="/day22/"></property>
18        <property name="suffix" value=".jsp"></property>
19    </bean>
20
21
22</beans>
23

 

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

5.jsp页面:

5.1login.jsp

 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
1<%@ page  pageEncoding="UTF-8" contentType="text/html;charset=UTF-8" language="java" isELIgnored="false"  %>
2<html>
3<head>
4    <title>Title</title>
5</head>
6<body>
7<h2>登录</h2>
8<form action="${pageContext.request.contextPath}/multiinitbinderFirst" method="post">
9
10    用户名:<input name="username" value="${username}">
11    年龄:<input name="userage">
12    生日:<input name="birthday">
13
14    <input type="submit" value="登录"/>
15</form>
16</body>
17</html>
18

 

5.2success.jsp

 


1
2
3
4
5
6
7
8
1<%@ page language="java" pageEncoding="utf-8" isELIgnored="false" %>
2<html>
3<body>
4<%--<img src="image/1.jpg">--%>
5<h2>Success!</h2>
6</body>
7</html>
8

 

6.启动tomcat,访问login.jsp

 

给TA打赏
共{{data.count}}人
人已打赏
安全技术

Ubuntu下安装Mysql 以及mysql-query-browser

2022-1-11 12:36:11

安全经验

JUnit + Mockito 单元测试(二)

2021-10-11 16:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索