SSM-SpringMVC-28:SpringMVC类型转换之自定义日期类型转换器

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

 

 

 

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

 

 

 

 

例子很简易,要明白的是思路,话不多说,开讲

上篇博客不是说springmvc默认的日期转换格式是yyyy/MM/dd吗?如果我们要别的格式怎么办?(例如yyyyMMdd,yyyy-MM-dd,yyyy年MM月dd日)就用到了自定义日期类型转换器

 

案例:

1.自定义类型转换器,实现泛型接口Converter

 


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
43
44
1package cn.dawn.day20selfconverter.converter;
2
3import org.springframework.core.convert.converter.Converter;
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/30.
12 */
13/*实现泛型接口,String是传进来的字符串,Date是要返回的日期类型*/
14public class MyDateConverter implements Converter<String,Date> {
15    public Date convert(String str) {
16        /*获取下面方法返回的SimoleDateFormat*/
17        SimpleDateFormat sdf=getSimpleDate(str);
18        try {
19            /*转换Date类型后返回*/
20            return sdf.parse(str);
21        } catch (ParseException e) {
22            e.printStackTrace();
23        }
24        return null;
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
43}
44

 

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
37
38
39
40
41
42
43
44
45
1package cn.dawn.day20selfconverter;
2
3import org.springframework.stereotype.Controller;
4import org.springframework.web.bind.annotation.ExceptionHandler;
5import org.springframework.web.bind.annotation.RequestMapping;
6import org.springframework.web.servlet.ModelAndView;
7
8import javax.servlet.http.HttpServletRequest;
9import java.util.Date;
10
11/**
12 * Created by Dawn on 2018/3/28.
13 */
14@Controller
15public class TypeConverterController {
16
17
18    /*作用于这俩个*/
19    @ExceptionHandler
20    public ModelAndView resolveException(Exception ex, HttpServletRequest request) {
21        ModelAndView modelAndView=new ModelAndView();
22        /*给username回显*/
23        modelAndView.addObject("username",request.getParameter("username"));
24        /*返回的异常对象*/
25        modelAndView.addObject("ex",ex);
26        /*发生异常返回登陆页*/
27        modelAndView.setViewName("login");
28
29
30        return modelAndView;
31    }
32
33
34
35
36    /*做日期类型自定义*/
37    @RequestMapping("/dateconverter2")
38    public String dateconverter1(String username, Integer userage, Date birthday) throws Exception {
39        System.out.println(username);
40        System.out.println(userage);
41        System.out.println(birthday);
42        return "success";
43    }
44}
45

 

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.day20selfconverter"></context:component-scan>
15    <!--视图解析器-->
16    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
17        <property name="prefix" value="/day20/"></property>
18        <property name="suffix" value=".jsp"></property>
19    </bean>
20
21
22</beans>
23

 

4.修改web.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}/dateconverter2" 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

安全经验

IDEA插件系列(14):AceJump插件——跳转到编辑器任意位置

2021-10-11 16:36:11

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