今天就写写springmvc配置Data类型转换器

首先在创建一个转换器的包cn.my.ssm.controller.converter,创建一个CustomDateConverter类实现Converter<String, Date>接口的public Date convert(String source)方法

package cn.my.ssm.controller.converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date; import org.springframework.core.convert.converter.Converter;
/**
* date类型转换
* @author Administrator
*
*/
public class CustomDateConverter implements Converter<String, Date> { @Override
public Date convert(String source) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
return simpleDateFormat.parse(source);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
} }

然后再到springmvc.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 扫描组件 -->
<context:component-scan base-package="cn.my.ssm.controller"></context:component-scan> <!-- 配置静态资源解析器 -->
<mvc:resources location="/js/" mapping="/js/**"/> <!-- 配置映射器和适配器 -->
<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 配置转换器(比如Date类型) -->
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="cn.my.ssm.controller.converter.CustomDateConverter"></bean>
</list>
</property>
</bean>
</beans>

然后修改itemsList.jsp的表格代码,代码如下

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>查询商品列表</title>
<script type="text/javascript">
function deleteItems(){
document.formSubmit.action = "${pageContext.request.contextPath }/items/deleteItems.action";
document.formSubmit.submit();
}
function queryItems(){
document.formSubmit.action = "${pageContext.request.contextPath}/items/queryItems.action";
document.formSubmit.submit();
}
function batchEditItems(){
document.formSubmit.action = "${pageContext.request.contextPath}/items/batchEditItems.action";
document.formSubmit.submit();
}
</script>
</head>
<body>
用户:${name }
<a href="${pageContext.request.contextPath}/logout.action">退出</a>
<form name="formSubmit" action="${pageContext.request.contextPath }/items/queryItems.action" method="post">
查询条件:
<table width="100%" border=1>
<tr>
<td><input name="itemsCustom.name" type="text" value=""/></td>
<td>
<input type="button" value="查询" onclick="queryItems()"/>
<input type="button" value="批量删除" onclick="deleteItems()"/>
<input type="button" value="批量修改" onclick="batchEditItems()"/>
</td>
</tr>
</table>
商品列表:
<table width="100%" border=1>
<tr>
<td>选择</td>
<td>商品名称</td>
<td>商品价格</td>
<td>生产日期</td>
<td>商品描述</td>
<td>操作</td>
</tr>
<c:forEach items="${itemsList }" var="item">
<tr>
<td><input type="checkbox" name="items_id" value="${item.id}"/></td>
<td>${item.name }</td>
<td>${item.price }</td> <!--使用jstl标签返回时间格式-->
<td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
<td>${item.detail }</td> <td><a href="${pageContext.request.contextPath }/items/editItems.action?id=${item.id}">修改</a></td> </tr>
</c:forEach> </table>
</form>
</body> </html>

这样就完成date类型转换了

ssm整合快速入门程序(三)之Data类型转换器的更多相关文章

  1. ssm整合快速入门程序(一)

    整合基础说明 spring 是一个开放源代码的设计层面框架,他解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用.Spring是于2003 年兴起的一个轻量级的Jav ...

  2. ssm整合快速入门程序(二)

    下面我们配置serivce层到项目中 1.service包中创建ItemsService.java接口,和service.imp包中创建一个service实现类ItemsServiceImpl.jav ...

  3. IDEA工具下Mybaties框架快速入门程序

    本篇文章介绍在IDEA工具下mybatis快速入门程序分为以下五步 ​ 1 添加依赖包 ​ 2 编写pojo对象 ​ 3 编写映射文件 ​ 4 编写核心配置文件 ​ 5 测试框架 详细如下 建立Mod ...

  4. ssm框架整合快速入门

    写在前面: 公司一直都是使用ssh框架(Struts2,Spring,Hibernate)来开发,但是现在外面的公司大多数都是使用的ssm框架,所以也有必要多学习一下外面的新技术.这里就快速搭建一个小 ...

  5. ELK快速入门(三)logstash收集日志写入redis

    ELK快速入门三-logstash收集日志写入redis 用一台服务器部署redis服务,专门用于日志缓存使用,一般用于web服务器产生大量日志的场景. 这里是使用一台专门用于部署redis ,一台专 ...

  6. SuperSocket快速入门(三):实现你的AppServer和AppSession

    什么是AppSession? AppSession 代表一个和客户端的逻辑连接,基于连接的操作应该定义于在该类之中.你可以用该类的实例发送数据到客户端,接收客户端发送的数据或者关闭连接.同时可以保存客 ...

  7. 元素(WebElement)-----Selenium快速入门(三)

    上一篇<元素定位-----Selenium快速入门(二)>说了,如何定位元素,本篇说说找到的元素(WebElement)该怎么用. WebElement常用方法:  返回值  方法名  说 ...

  8. spring快速入门(三)

    一.在spring快速入门(二)的基础上,原先我们是采用构造方法完成对象的注入.这里还有其他的方法可以完成注入,通过set方法来完成. 修改UserActionImpl package com.mur ...

  9. HTML5快速入门(三)—— 标签综合运用

    前言: 1.HTML5的发展非常迅速,可以说已经是前端开发人员的标配,在电商类型的APP中更是运用广泛,这个系列的文章是本人自己整理,尽量将开发中不常用到的剔除,将经常使用的拿出来,使需要的朋友能够真 ...

随机推荐

  1. LeetCode - 661. Image Smoother

    Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoother t ...

  2. 超级简便的容器化部署工具(使用 ASP.NET Core 演示)

    Docker 改变了我们部署网站的方式,从原先的手动编译打包上传,到现在的构建镜像然后推送部署,让我们在配置环境上所花费的时间大大减少了.不仅如此,通过一系列相关的工具配合,可以很轻松的实现 CI.C ...

  3. [Python Study Notes]WdSaveFormat 枚举

    WdSaveFormat 枚举 指定要在保存文档时使用的格式. 版本信息 已添加版本: 名称 值 说明 wdFormatDocument 0 Microsoft Word 格式. wdFormatDO ...

  4. yum 安装 nfs,rpcbind 出现错误 libc.so.6(GLIBC_2.14)(64bit) is needed by

    错误信息: Running rpm_check_debugERROR with rpm_check_debug vs depsolve:libc.so.6(GLIBC_2.14)(64bit) is ...

  5. 在linux内核中修改TCP MSS值

    MTU: Maxitum Transmission Unit 最大传输单元 MSS: Maxitum Segment Size 最大分段大小 MSS最大传输大小的缩写,是TCP协议里面的一个概念.MS ...

  6. PHP系统左侧菜单栏的管理与实现

    在日常的开发工作中,面对后台的日益增长的业务,以及后期业务的迭代开发,通常会选择添加菜单栏的形式来扩充业务功能,同样日益增长的后台菜单选项也为我们后期的维护,产生了一定的困难性.为此我总结出自己关于左 ...

  7. Angular2 ^ 资源链接

     Angular2 资源链接 Material Desgin 2 githubhttps://github.com/Promact/md2 DEMOhttp://code.promactinfo.co ...

  8. JQuery坑,说说哪些大家都踩过的坑

    1 乱用选择器 坑人指数:200 JQuery选择器调用代价很大,反复调用效率更低.应采用缓存对象的方法或采用链式调用的方式. //错误的写法 $("#button").click ...

  9. CodeForces 820C

    题意略. 这道题目的出题者竟然被hack了!? 我的思路是:在游戏开始时,为了尽量少地用字母,我应该尽量选取计算机输出的前a个字母中已经使用过的字母.但是为了使电脑也尽量少用字母,我添加的这b个字母应 ...

  10. 记录一则fsck的简单案例

    环境:RHEL 6.5 + ext4文件系统 我个人实验环境的一个虚拟机,开机时在Checking filesystems时,有报错: /dev/mapper/vg_linuxbase-lv_root ...