SSM-SpringMVC-28:SpringMVC类型转换之自定义日期类型转换器
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------
例子很简易,要明白的是思路,话不多说,开讲
上篇博客不是说springmvc默认的日期转换格式是yyyy/MM/dd吗?如果我们要别的格式怎么办?(例如yyyyMMdd,yyyy-MM-dd,yyyy年MM月dd日)就用到了自定义日期类型转换器
案例:
1.自定义类型转换器,实现泛型接口Converter
package cn.dawn.day20selfconverter.converter; import org.springframework.core.convert.converter.Converter; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.regex.Pattern; /** * Created by Dawn on 2018/3/30. */ /*实现泛型接口,String是传进来的字符串,Date是要返回的日期类型*/ public class MyDateConverter implements Converter<String,Date> { public Date convert(String str) { /*获取下面方法返回的SimoleDateFormat*/ SimpleDateFormat sdf=getSimpleDate(str); try { /*转换Date类型后返回*/ return sdf.parse(str); } catch (ParseException e) { e.printStackTrace(); } return null; } /*根据字符串的格式自定义转换的格式*/ 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.day20selfconverter; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import java.util.Date; /** * Created by Dawn on 2018/3/28. */ @Controller public class TypeConverterController { /*作用于这俩个*/ @ExceptionHandler public ModelAndView resolveException(Exception ex, HttpServletRequest request) { ModelAndView modelAndView=new ModelAndView(); /*给username回显*/ modelAndView.addObject("username",request.getParameter("username")); /*返回的异常对象*/ modelAndView.addObject("ex",ex); /*发生异常返回登陆页*/ modelAndView.setViewName("login"); return modelAndView; } /*做日期类型自定义*/ @RequestMapping("/dateconverter2") public String dateconverter1(String username, Integer userage, Date birthday) throws Exception { 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.day20selfconverter"></context:component-scan> <!--视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/day20/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
4.修改web.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}/dateconverter2" 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