您现在的位置是:首页 > 文章详情

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

日期:2018-03-30点击:385

 

 

 

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

 

 

 

 

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

上篇博客不是说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

 

原文链接:https://yq.aliyun.com/articles/619631
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章