————吾亦无他,唯手熟尔,谦卑若愚,好学若饥————-
本案例是上面的异常和日期类型转换结合的一个小小的Demo
案例开始
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
40
41
42
43
44
45 1package cn.dawn.day19typeconverter;
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("/dateconverter1")
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
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.day19typeconverter"></context:component-scan>
15 <!--视图解析器-->
16 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
17 <property name="prefix" value="/day19/"></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}/dateconverter1" 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
结论:什么时候跳到成功页面?就是日期格式不出错,日期格式必须为yyyy/MM/dd,SpringMVC默认的自动转换日期格式必须传入的是这个
下篇博客就做自定义类型转换器