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="data:image/1.jpg">--%>
<h2>Success!</h2>
</body>
</html>
6.启动tomcat,访问login.jsp
SSM-SpringMVC-28:SpringMVC类型转换之自定义日期类型转换器的更多相关文章
- Spring MVC__自定义日期类型转换器
WEB层采用Spring MVC框架,将查询到的数据传递给APP端或客户端,这没啥,但是坑的是实体类中有日期类型的属性,但是你必须提前格式化好之后返回给它们.说真的,以前真没这样做过,之前都是一口气查 ...
- struts_自定义日期类型转换器
1.问题:struts默认的日期类型是 xxxx-mm-dd,不能接收xxxx/mm//dd类型的日期 2.解决方案(继承DefaultTypeConverter,覆盖convertValue(Obj ...
- struts2自定义日期类型转换器
在java web表单中提交的数据难免会有日期类型,struts2支持的日期类型是yyyy-MM-dd,如果是其他格式,就需要自己进行转换.比如yy-MM-dd 要完成自己定义的转换需要完成. 主要的 ...
- SpringMVC在使用Jackson2时关于日期类型格式化的问题
SpringMVC在使用Jackson2时关于日期类型格式化的问题 如果无效,那么使用 @DateTimeFormat(pattern = "yyyy-MM-dd")
- Java String类型转换成Date日期类型
插入数据库时,存入当前日期,需要格式转换 import java.text.SimpleDateFormat; formatter = new SimpleDateFormat( "yyyy ...
- spring boot 配置全局日期类型转换器
1. 首先自定义一个类型转换器 import org.springframework.core.convert.converter.Converter; import org.springframew ...
- SpringMVC提交数据遭遇基础类型和日期类型报400错误解决方法
使用SpringMVC开发的时候,页面如果有日期格式的数据,后台接受也是java.util.Date,则报告400错误 .下面是解决方案的演示示例: 这个是实体类,里面createDate就是java ...
- SpringBoot配置自定义日期参数转换器
1.自定义参数转换器 自定义参数转换器必须实现Converter接口 /** * Created by IntelliJ IDEA. * * @Auther: ShaoHsiung * @Date: ...
- ssm整合快速入门程序(三)之Data类型转换器
今天就写写springmvc配置Data类型转换器 首先在创建一个转换器的包cn.my.ssm.controller.converter,创建一个CustomDateConverter类实现Conve ...
随机推荐
- WIN7电脑文件莫名其妙被删除后的恢复
今天早上打开电脑,发现电脑F盘下的WINCE600文件夹下有剩下一小部分文件,绝大部分文件都找不到了,但是我记得自己没有删除过,而且在回收站也没有找到这些被删除的文件,怎恢复呢,今天尝试使用Recov ...
- 手把手教你从头开始搭建友善之臂ARM-tiny4412开发环境(史上最详细!!)
创建一个ARM目录 mkdir /disk/A9 -p 接下来你需要准备以下的东西 1.arm-linux-gcc-4.5.1 交叉编译器 2.linux-3.5-tiny4412 ...
- HDFS APPEND性能测试
hbase在写入数据之前会先写hlog,hlog目前是sequencefile格式,采用append的方式往里追加数据.之前团队的同学测试关闭hlog会一定程序上提升写hbase的稳定性.而在我之前的 ...
- linux设备驱动--等待队列实现
#include <linux/module.h> #include <linux/fs.h> #include <linux/sched.h> #include ...
- Sharepoint 2010 自定义WebService 找不到网站应用程序
错误描述:Net 开发WebService调用Microsoft.SharePoint.dll的服务器端对象模型,出现找不到网站的应用程序,或者出现500错误. 错误截图: [Webservice调用 ...
- The 3rd tip of DB QueryAnalyzer
The 3rd tip of DB Query Analyzer Ma Genfeng (Guangdong Unitoll Services incorporated, Guangzhou 510 ...
- miniUI Grid添加汇总行,Grid绑定数据,IDEA免编译设置
坑1: 2017-6-5周二,上午解决了昨天摸索一下午的问题,使用miniui显示汇总行数据,要点有这么几个 在创建Grid div的时候一定要加上以下两个属性: //显示汇总行开关 showSumm ...
- TCP TIME WAIT
根据TCP协议定义的3次握手断开连接规定,发起socket主动关闭的一方 socket将进入TIME_WAIT状态,TIME_WAIT状态将持续2个MSL(Max Segment Lifetime) ...
- 单链表反转(Singly Linked Lists in Java)
单链表反转(Singly Linked Lists in Java) 博客分类: 数据结构及算法 package dsa.linkedlist; public class Node<E> ...
- 【读书笔记】C++Primer---第二章
1.C++基本数据类型:字符型.整型.浮点型等: 2.算术类型如下表,下表只是表示C++要求的最小储存空间,但是一般编译器使用的都比所要求的储存空间大,以前类型所能表示的最大值也因机器的不同而有不同的 ...