實體里用的java.util.date,數(shù)據(jù)庫用的是datetime,頁面是字符串<input type="date">。將頁面標(biāo)簽<input type="date">的內(nèi)容添加到數(shù)據(jù)庫
實體
public class BaseInformation { //信息主鍵 private String id; //信息標(biāo)題 private String title; //信息類型id(需要在數(shù)據(jù)字典定義) private String typeCode; //屬性id(需要在數(shù)據(jù)字典定義) private String propertityId; //信息可視范圍,部門可見或者整個單位可見,值是組織結(jié)構(gòu)的id private String scope; //內(nèi)容 private String content; //發(fā)布人id private String releaseId; //是否發(fā)布,1發(fā)布,0保存 private Integer released; //接收人id,對應(yīng)tb_base_information_receiver的主鍵 private String receiver; //創(chuàng)建時間就是發(fā)布時間 private Date createDate; //更新時間 private Date updateDate; //開始時間 private Date beginDate; //結(jié)束時間 private Date endDate; //定時器,規(guī)定什么時候發(fā)布信息 private Date timer;
。。。。。省略getter和setter
表
CREATE TABLE `tb_base_information` ( `id` varchar(48) NOT NULL, `title` varchar(128) DEFAULT NULL COMMENT "標(biāo)題", `type_code` varchar(48) DEFAULT NULL COMMENT "類型id(需要在數(shù)據(jù)字典定義),公告、新聞等", `propertity_id` varchar(48) DEFAULT NULL COMMENT "屬性id(需要在數(shù)據(jù)字典定義)", `scope` varchar(255) DEFAULT NULL COMMENT "信息可視范圍,部門可見或者整個單位可見,值是組織結(jié)構(gòu)的id", `content` text COMMENT "內(nèi)容", `release_id` varchar(48) DEFAULT NULL COMMENT "發(fā)布人id", `released` int(11) DEFAULT NULL COMMENT "是否發(fā)送,1發(fā)送0保存為草稿", `create_date` datetime DEFAULT NULL COMMENT "創(chuàng)建時間", `update_date` datetime DEFAULT NULL COMMENT "更新時間", `begin_date` datetime DEFAULT NULL COMMENT "信息有效期的起始時間", `end_date` datetime DEFAULT NULL COMMENT "信息有效期的結(jié)束時間", `timer` datetime DEFAULT NULL COMMENT "定時器,指定發(fā)送信息的時間", `expiry_date` datetime DEFAULT NULL COMMENT "是否永久有效,1是0否", `to_top` int(1) DEFAULT NULL COMMENT "信息是否置頂,1置頂,0否", `mark_star` int(1) DEFAULT NULL COMMENT "做星標(biāo)標(biāo)記用,1在星標(biāo)公告里顯示,0否", `receiver` varchar(48) DEFAULT NULL COMMENT "接收人id,對應(yīng)tb_base_information_receiver的主鍵", PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT="信息發(fā)布表";
頁面
<form name="form" method="post" action="/information/test" enctype="multipart/form-data"> 類型:<input type="text" name="typeCode" ><br/> 標(biāo)題:<input type="text" name="title" ><br/> 內(nèi)容:<textarea name="content" cols="30" rows="10"></textarea><br/> 創(chuàng)建時間:<input type="date" name="createDate"/><br/> 更新時間:<input type="date" name="updateDate"/><br/> 有效期開始時間:<input type="date" name="beginDate"/><br/> 有效期結(jié)束時間:<input type="date" name="endDate"/><br/> 定時器:<input type="date" name="timer"/><br/> <input type="submit" value="提交"> </form>
controller
@RequestMapping("/test") public String test(BaseInformation baseInformation, HttpServletRequest request) throws Exception { String id = UUID.randomUUID().toString(); baseInformation.setId(id); //baseInformation.setId(UUID.randomUUID().toString().replaceAll("-","")); System.out.println("request.getParameter(createDate)" + request.getParameter("createDate")); Date createDate = new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("createDate")); Date updateDate = new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("createDate")); Date beginDate = new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("beginDate")); Date endDate = new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("endDate")); Date timer = new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("timer")); baseInformation.setCreateDate(createDate); baseInformation.setUpdateDate(updateDate); baseInformation.setBeginDate(beginDate); baseInformation.setEndDate(endDate); baseInformation.setTimer(timer); service.save(baseInformation); return "information/test"; }
運行結(jié)果
Field error in object "baseInformation" on field "createDate": rejected value [2018-11-08]; codes [typeMismatch.baseInformation.createDate,typeMismatch.createDate,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [baseInformation.createDate,createDate]; arguments []; default message [createDate]]; default message [Failed to convert property value of type "java.lang.String" to required type "java.util.Date" for property "createDate"; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.util.Date] for value "2018-11-08"; nested exception is java.lang.IllegalArgumentException] at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:117) at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121) at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:158) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:128) at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872) at javax.servlet.http.HttpServlet.service(HttpServlet.java:650) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
修改方法
修改實體
string和date的類型轉(zhuǎn)換失敗,此時在實體中,對需要進(jìn)行轉(zhuǎn)換的類型添加如下注解,實現(xiàn)java.lang.String和java.util.Date之間自動轉(zhuǎn)換
@DateTimeFormat(pattern="yyyy-MM-dd")//頁面寫入數(shù)據(jù)庫時格式化 @JSONField(format="yyyy-MM-dd")//數(shù)據(jù)庫導(dǎo)出頁面時json格式化
即
修改contoller
既然java.lang.String和java.util.Date之間可以自動轉(zhuǎn)換了,后臺就不需要通過request獲取參數(shù)來進(jìn)行轉(zhuǎn)換,可以將紅方格中的注釋掉
依賴添加
1.注解@JsonFormat
<!--JsonFormat--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.8.8</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.8.8</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency>
2.注解@DateTimeFormat
@DateTimeFormat的使用和@jsonFormat差不多,首先需要引入是spring還有jodatime,spring我就不貼了
<!-- joda-time --> <dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> <version>2.3</version> </dependency>
關(guān)于@DateTimeFormat和@JsonFormat還可以參考http://www.ythuaji.com.cn/article/203164.html
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/beidaol/article/details/84637984