今天就写写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. 阿里云学习之IOT物联网套件(客户端与服务端的后台数据传输)

    设备端代码(mqttClient):https://help.aliyun.com/document_detail/42648.html?spm=5176.doc30579.6.569.ZEgA1g ...

  2. Halcon一日一练:CAD类型的相关操作

    大很多场合,需要在视觉程序中导入CAD文档,比如,在3C行业,需要对手机外壳进行CNC加工,或者点胶操作,此时,需要获取产品的各个点的数据.如果将CAD直接导入,就会大的减少编程工作量,同时也能达到很 ...

  3. CocosCreator游戏开发---菜鸟学习之路(一)

    PS(废话): 辞职后在家好久好久了,久到经济不允许了,接着就准备再次出去找工作了,然而工作哪有那么好找,特别是像我这种菜鸟.而且我还准备转行,准备去做游戏,技能等级接近于0,那工作就更难找了.既然如 ...

  4. 浅讲JUnit

    JUnit单元简介:      JUnit ----是一个开发源码的java测试框架,用于编写和运行可重复的测试,它是用于单元测试框架体系xUnit的一个实例, 用于java语言,主要用于白盒测试,回 ...

  5. CENTOS/RHEL 7 系统中设置SYSTEMD SERVICE的ULIMIT资源限制

    遇到的问题: golang程序一直出现 too many open files的报错, 尽管对 /etc/security/limits.conf 做了设置, 对最大文件打开数,最大进程数做了调优. ...

  6. 原生js总结(干货)

    1.js基本数据类型 number string boolean underfined null 2.查找文档中的特定元素 document.getElementById("id" ...

  7. ajax请求中contentType与dataType区别

    contentType: 告诉服务器,我要发什么类型的数据,默认值: "application/x-www-form-urlencoded". dataType:告诉服务器,我要想 ...

  8. os模块中关于文件/目录常用的函数使用方法

    os模块中关于文件/目录常用的函数使用方法 函数名 使用方法 getcwd() 返回当前工作目录 chdir(path) 改变工作目录 listdir(path='.') 列举指定目录中的文件名('. ...

  9. js中的写出想jquery中的函数一样调用

    1.IIFE: Immediately-Invoked function Expression 函数模块自调用 2.代码实现 <!DOCTYPE html> <html lang=& ...

  10. 剑指offer第八天

    32.把数组排成最小的数 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个.例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323 ...