**** ————吾亦无他,唯手熟尔,谦卑若愚,好学若饥————-****
之前博客的配置日期类型转换器,他是全局的,如果只是一个处理器中使用怎么办?
引出@InitBinder注解
案例:
1.在处理器中:
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 1package cn.dawn.day22initbinder;
2
3import org.springframework.beans.propertyeditors.CustomDateEditor;
4import org.springframework.stereotype.Controller;
5import org.springframework.web.bind.WebDataBinder;
6import org.springframework.web.bind.annotation.InitBinder;
7import org.springframework.web.bind.annotation.RequestMapping;
8
9import java.text.SimpleDateFormat;
10import java.util.Date;
11
12/**
13 * Created by Dawn on 2018/3/31.
14 */
15
16@Controller
17public class FirstController {
18 /*第一种*/
19 /*@InitBinder*/
20 /*第二种*/
21 @InitBinder("birthday")
22 public void initBinder(WebDataBinder binder){
23 System.out.println("1111111111");
24 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
25 binder.registerCustomEditor(Date.class,new CustomDateEditor(sdf,true));
26 }
27
28
29 /*initbinder*/
30 @RequestMapping("/initbinderFirst")
31 public String initbinderFirst(String username, Integer userage, Date birthday) throws Exception {
32 System.out.println("2222222222");
33 System.out.println(username);
34 System.out.println(userage);
35 System.out.println(birthday);
36 return "success";
37 }
38}
39
此处需要重点解释一波,这样就可以日期类型转换器变成局部的了,俩个方法执行顺序正如输出的那样一个是1,一个是2,@InitBinder注解它加参数是说只匹配birthday,如果不加,下面那个方法有三个参数,上面的方法就执行三次,System.out.print("11111111")三次
2.自己的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
3.修改web.xml配置文件的中央调度器的上下问配置位置为上面的那个xml
4.jsp页面:
4.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}/initbinderFirst" 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
4.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
5.启动tomcat,访问login.jsp,发现只有yyyy-MM-dd可以转换成功,说明它已经和springmvc默认的不同了,同样的问题,兼容性不好,所以,下一篇博客为大家做进一步的优化