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

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

上篇博客不是说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类型转换之自定义日期类型转换器的更多相关文章

  1. Spring MVC__自定义日期类型转换器

    WEB层采用Spring MVC框架,将查询到的数据传递给APP端或客户端,这没啥,但是坑的是实体类中有日期类型的属性,但是你必须提前格式化好之后返回给它们.说真的,以前真没这样做过,之前都是一口气查 ...

  2. struts_自定义日期类型转换器

    1.问题:struts默认的日期类型是 xxxx-mm-dd,不能接收xxxx/mm//dd类型的日期 2.解决方案(继承DefaultTypeConverter,覆盖convertValue(Obj ...

  3. struts2自定义日期类型转换器

    在java web表单中提交的数据难免会有日期类型,struts2支持的日期类型是yyyy-MM-dd,如果是其他格式,就需要自己进行转换.比如yy-MM-dd 要完成自己定义的转换需要完成. 主要的 ...

  4. SpringMVC在使用Jackson2时关于日期类型格式化的问题

    SpringMVC在使用Jackson2时关于日期类型格式化的问题 如果无效,那么使用 @DateTimeFormat(pattern = "yyyy-MM-dd")

  5. Java String类型转换成Date日期类型

    插入数据库时,存入当前日期,需要格式转换 import java.text.SimpleDateFormat; formatter = new SimpleDateFormat( "yyyy ...

  6. spring boot 配置全局日期类型转换器

    1. 首先自定义一个类型转换器 import org.springframework.core.convert.converter.Converter; import org.springframew ...

  7. SpringMVC提交数据遭遇基础类型和日期类型报400错误解决方法

    使用SpringMVC开发的时候,页面如果有日期格式的数据,后台接受也是java.util.Date,则报告400错误 .下面是解决方案的演示示例: 这个是实体类,里面createDate就是java ...

  8. SpringBoot配置自定义日期参数转换器

    1.自定义参数转换器 自定义参数转换器必须实现Converter接口 /** * Created by IntelliJ IDEA. * * @Auther: ShaoHsiung * @Date: ...

  9. ssm整合快速入门程序(三)之Data类型转换器

    今天就写写springmvc配置Data类型转换器 首先在创建一个转换器的包cn.my.ssm.controller.converter,创建一个CustomDateConverter类实现Conve ...

随机推荐

  1. UML之对象图

    对象图对包含在类图中的事物的实例建模,对象图显示了在某一时间点上一组对象以及他们之间的关系.对象图用于对系统的静态设计视图或静态交互视图建模,这包括对某一时刻的系统快照建模,表示出对象集.对象的状态以 ...

  2. 对FMDB的封装JRDB

    在自己开发中,每次用到数据库都会纠结是使用CoreData还是FMDB.CoreData虽然Api简单,但是调用栈非常复杂,要初始化一个Context需要至少20行代码.显然,对于这种这么恶心的情况, ...

  3. boost pool_allocator 报错 'rebind'

    #include "stdafx.h" #include <vector> #include <boost/pool/pool.hpp> int _tmai ...

  4. C# DataTable,DataSet,IList,IEnumerable 互转扩展属性

    using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.R ...

  5. canvas的常见用法

    Canvas canvas是一种抽象概念,是2D图形系统中的重要部分,canvas一系列函数最终都是android 2D图形库Skia的一些列封装,对应在SKCanvas.cpp.canvas在系统中 ...

  6. mahout系列----minhash聚类

    Map: Vector featureVector = features.get(); if (featureVector.size() < minVectorSize) {       ret ...

  7. HBase 索引创建

    本文参考了文"mysql索引背后的数据结构及算法原理",之所以还要摘录,主要是为了形成hbase索引研究的开篇,弄明白什么索引的本质,如有版权问题,请及时通知. 索引的本质 索引是 ...

  8. 【.Net架构】BIM软件架构03:Web管控平台MVC架构

    一.前言        上一篇讲述的是将BIM平台后台架构CoreService.sln,该解决方案主要作用是对管控平台的核心业务进行封装,然后让前端的ApiController去调用该解决方案中的对 ...

  9. mysql 带条件的sum/count 使用技巧

    本测试只是简单测试,其用途不在于代替count函数配合where函数进行聚合计算,而是在复杂查询中在sum/count中加入条件,一次性求出不同条件下的聚合结果. 1.插入测试数据如图 2.sum计算 ...

  10. currval of sequence "follow_id_seq" is not yet defined in this session

    postgresql上使用 select currval('follow_id_seq'); 报错: currval of sequence "follow_id_seq" is  ...