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

时间:2022-06-25 16:57:11

今天就写写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类型转换了