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 ...
随机推荐
- Linux 套接字编程中的 5 个隐患(转)
本文转自IBM博文Linux 套接字编程中的 5 个隐患. “在异构环境中开发可靠的网络应用程序”. Socket API 是网络应用程序开发中实际应用的标准 API.尽管该 API 简单,但是开发新 ...
- *** non-numeric second argument to `wordlist' function: ''. Stop错误解决办法
PS: 解决办法搜集自:stackoverflow website:http://stackoverflow.com/questions/5677178/ndk-gdb-fails-with-mess ...
- Leetcode_228_Summary Ranges
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/46762039 Given a sorted integer ...
- Linux 系统应用编程——线程基础
传统多任务操作系统中一个可以独立调度的任务(或称之为顺序执行流)是一个进程.每个程序加载到内存后只可以唯一地对应创建一个顺序执行流,即传统意义的进程.每个进程的全部系统资源是私有的,如虚拟地址空间,文 ...
- 恶补web之八:jQuery(3)
jquery和其他js框架.jQuery使用$作为jQuery的简写,但是还有很多js框架,比如: MooTools,Backbone,Sammy,Cappuccino,Knockout,JavaSc ...
- Course4-Python ftp/ssh
1. ftp Python 自带有模块支持ftp. 可以参看一下代码. #!/usr/bin/env python import sys import os import time import ge ...
- CSS 文章链接
文本溢出显示为省略号 Ellipsis for text overflow in table cell?
- 升级Centos 7/6内核版本到4.12.4的方法
一.查看那系统内核版本 二.升级内核 三.修改grub中默认的内核版本 四.重启系统并查看系统内核 公司打算上Docker服务,目前需要安装运行环境,Docker新的功能除了需要Centos 7系统之 ...
- Getting Real内容浓缩
今天看完,想整理一下,可能会更好,也给别人提供一个快速学习的途径第一章 什么是 Getting Real?表达形式省略.精炼.精益.敏捷.用户体验.迭代改进.产品简化.第二章 建构从简做得比竟争对手少 ...
- mybatis源码解读(三)——数据源的配置
在mybatis-configuration.xml 文件中,我们进行了如下的配置: <!-- 可以配置多个运行环境,但是每个 SqlSessionFactory 实例只能选择一个运行环境常用: ...