JavaEE_Mybatis_SpringMVC_自定义的参数绑定,页面的日期类型传递到数据库中datetime

来源:互联网 发布:石家庄seo建站 编辑:程序博客网 时间:2024/06/06 04:59

项目代码:

http://pan.baidu.com/s/1borjAur


在JavaEE的 Mybatis_SpringMVC 框架中

Springmvc支持对一般pojo类型的映射,只需要页面上的name属性与pojo中的属性相对应,则可完成映射。

但对于java中的 import java.util.Date; Date 类型并不支持。前端的数据并不能直接映射到pojo中,此时需要我们自己定义一个转换器


一般pojo属性映射:整套流程

addItem.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"><script type="text/javascript" src="<%=request.getContextPath() %>/resources/js/My97DatePicker/WdatePicker.js"></script><title>修改商品信息</title></head><body> <form id="itemForm" action="${pageContext.request.contextPath }/items/addItemsSubmit.action" method="post" ><input type="hidden" name="id" value="${itemsEx.id }"/>新增商品信息:<table width="100%" border=1><tr><td>商品名称</td><td><input type="text" name="name" value="${itemsEx.name }"/></td></tr><tr><td>商品价格</td><td><input type="text" name="price" value="${itemsEx.price }"/></td></tr><tr><td>商品生产日期</td><td><input type="text" name="createtime" value="${itemsEx.createtime}" onfocus="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})"/></td><!--<td><input type="text" name="createtime" value="<fmt:formatDate value="${itemsEx.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/>"/></td>  --></tr><%-- <tr><td>商品图片</td><td><c:if test="${item.pic !=null}"><img src="/pic/${item.pic}" width=100 height=100/><br/></c:if><input type="file"  name="pictureFile"/> </td></tr> --%><tr><td>商品简介</td><td><textarea rows="3" cols="30" name="detail">${itemsEx.detail }</textarea></td></tr><tr><td colspan="2" align="center"><input type="submit" value="提交"/></td></tr></table></form></body></html>


controller

@RequestMapping("/addItemsSubmit")public String editItemsSubmit(ItemsEx itemsEx) {try {itemsService.insertItems(itemsEx);} catch (Exception e) {e.printStackTrace();}return "redirect:queryItems.action";}


pojo

package cn.itcast.ssm.po;import java.util.Date;public class Items {    private Integer id;    private String name;    private Float price;    private String pic;    private Date createtime;    private String detail;    public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name == null ? null : name.trim();    }    public Float getPrice() {        return price;    }    public void setPrice(Float price) {        this.price = price;    }    public String getPic() {        return pic;    }    public void setPic(String pic) {        this.pic = pic == null ? null : pic.trim();    }    public Date getCreatetime() {        return createtime;    }    public void setCreatetime(Date createtime) {        this.createtime = createtime;    }    public String getDetail() {        return detail;    }    public void setDetail(String detail) {        this.detail = detail == null ? null : detail.trim();    }}


对于特殊的Date 的定义转换器;

配置文件(springmvc.xml)进行如下配置:

<!--注解映射器 --><!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> --><!--注解适配器 --><!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> --><!-- 使用 mvc:annotation-driven代替上边注解映射器和注解适配器配置mvc:annotation-driven默认加载很多的参数绑定方法,比如json转换解析器就默认加载了,如果使用mvc:annotation-driven不用配置上边的RequestMappingHandlerMapping和RequestMappingHandlerAdapter实际开发时使用mvc:annotation-driven --><mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>

<!-- 自定义参数绑定 --><bean id="conversionService"  class="org.springframework.format.support.FormattingConversionServiceFactoryBean"><property name="converters"><list><!-- 进行日期的转换 --><bean class="cn.itcast.ssm.controller.converter.CustomConverter"/></list></property></bean>

需要实现 org.springframework.format.support.FormattingConversionServiceFactoryBean 接口(spring-context-3.2.0 RELEASE.jar)

实现类

package cn.itcast.ssm.controller.converter;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import org.springframework.core.convert.converter.Converter;public class CustomConverter implements Converter<String, Date> {// 实现将日期串转换为日期类型@Overridepublic Date convert(String source) {SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");try {return simpleDateFormat.parse(source);} catch (ParseException e) {e.printStackTrace();}// 如果参数绑定失败,返回nullreturn null;}}



0 0
原创粉丝点击