2015-11-24 62 views
0

我試圖在Spring的<form:input>元素中顯示Date,即Db上保存的POJO的字段。在Spring窗體標記中顯示Date(java.sql.Date)時發生錯誤

這是我的POJO

import java.sql.Date; 
import org.springframework.format.annotation.DateTimeFormat; 

@Component 
@MappedSuperclass 
public class Persona implements Serializable{   
    .... 
    @Column(name="data_di_nascita", nullable=false, updatable=true) 
    @DateTimeFormat(pattern="yyyy/MM/dd") 
    private Date dataDiNascita; 
    .... 
} 

這是我的jsp頁面

<form:form modelAttribute="persona"> 
... 
    Data di nascista<form:input path="dataDiNascita"/><br/> 
... 
</form:form> 

這是錯誤我得到

 org.springframework.core.convert.ConversionFailedException: Failed to convert from type @javax.persistence.Column @org.springframework.format.annotation.DateTimeFormat java.sql.Date to type java.lang.String for value '1978-07-01'; nested exception is java.lang.IllegalStateException: JodaTime library not available - @DateTimeFormat not supported 
     org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:41) 
     org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:171) 
     org.springframework.validation.AbstractPropertyBindingResult.formatFieldValue(AbstractPropertyBindingResult.java:125) 
     org.springframework.validation.AbstractBindingResult.getFieldValue(AbstractBindingResult.java:222) 
     org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:120) 
     org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:178) 
     org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:198) 
     org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:164) 
     org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.autogenerateId(AbstractDataBoundFormElementTag.java:151) 
     org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.resolveId(AbstractDataBoundFormElementTag.java:142) 
     org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.writeDefaultAttributes(AbstractDataBoundFormElementTag.java:126) 
     org.springframework.web.servlet.tags.form.AbstractHtmlElementTag.writeDefaultAttributes(AbstractHtmlElementTag.java:421) 
     org.springframework.web.servlet.tags.form.InputTag.writeTagContent(InputTag.java:142) 
     org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:102) 
     org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:79) 
     org.apache.jsp.WEB_002dINF.views.persona_jsp._jspx_meth_form_005finput_005f5(persona_jsp.java:317) 
     org.apache.jsp.WEB_002dINF.views.persona_jsp._jspx_meth_form_005fform_005f0(persona_jsp.java:135) 
     org.apache.jsp.WEB_002dINF.views.persona_jsp._jspService(persona_jsp.java:76) 
     org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) 
     javax.servlet.http.HttpServlet.service(HttpServlet.java:847) 
     org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439) 
     org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395) 
     org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339) 
     javax.servlet.http.HttpServlet.service(HttpServlet.java:847) 
     org.apache.jasper.runtime.JspRuntimeLibrary.include(JspRuntimeLibrary.java:954) 
     org.apache.jasper.runtime.PageContextImpl.doInclude(PageContextImpl.java:688) 
     org.apache.jasper.runtime.PageContextImpl.include(PageContextImpl.java:682) 
     org.apache.tiles.jsp.context.JspTilesRequestContext.include(JspTilesRequestContext.java:103) 
     org.apache.tiles.jsp.context.JspTilesRequestContext.dispatch(JspTilesRequestContext.java:96) 
     org.apache.tiles.renderer.impl.UntypedAttributeRenderer.write(UntypedAttributeRenderer.java:61) 
     org.apache.tiles.renderer.impl.AbstractBaseAttributeRenderer.render(AbstractBaseAttributeRenderer.java:103) 
     org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:669) 
     org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:337) 
     org.apache.tiles.jsp.taglib.InsertAttributeTag.render(InsertAttributeTag.java:234) 
     org.apache.tiles.jsp.taglib.InsertAttributeTag.render(InsertAttributeTag.java:211) 
     org.apache.tiles.jsp.taglib.RenderTag.doEndTag(RenderTag.java:220) 
     org.apache.jsp.WEB_002dINF.template.default_.template1_jsp._jspx_meth_tiles_005finsertAttribute_005f2(template1_jsp.java:284) 
     org.apache.jsp.WEB_002dINF.template.default_.template1_jsp._jspService(template1_jsp.java:115) 
     org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) 
     javax.servlet.http.HttpServlet.service(HttpServlet.java:847) 
     org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439) 
     org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395) 
     org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339) 
     javax.servlet.http.HttpServlet.service(HttpServlet.java:847) 
     org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 
     org.apache.tiles.servlet.context.ServletTilesRequestContext.forward(ServletTilesRequestContext.java:241) 
     org.apache.tiles.servlet.context.ServletTilesRequestContext.dispatch(ServletTilesRequestContext.java:222) 
     org.apache.tiles.renderer.impl.TemplateAttributeRenderer.write(TemplateAttributeRenderer.java:44) 
     org.apache.tiles.renderer.impl.AbstractBaseAttributeRenderer.render(AbstractBaseAttributeRenderer.java:103) 
     org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:669) 
     org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:689) 
     org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:643) 
     org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:626) 
     org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:322) 
     org.springframework.web.servlet.view.tiles2.TilesView.renderMergedOutputModel(TilesView.java:124) 
     org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:262) 
     org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1180) 
     org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:950) 
     org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852) 
     org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882) 
     org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778) 
     javax.servlet.http.HttpServlet.service(HttpServlet.java:734) 
     javax.servlet.http.HttpServlet.service(HttpServlet.java:847) 
     org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 
     org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) 
     org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118) 
     org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84) 
     org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
     org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113) 
     org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
     org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103) 
     org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
     org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113) 
     org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
     org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54) 
     org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
     org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45) 
     org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
     org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:183) 
     org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
     org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105) 
     org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
     org.springframework.security.web.session.ConcurrentSessionFilter.doFilter(ConcurrentSessionFilter.java:125) 
     org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
     org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87) 
     org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
     org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192) 
     org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160) 
     org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346) 
     org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259) 



    root cause 
    java.lang.IllegalStateException: JodaTime library not available - @DateTimeFormat not supported 
     org.springframework.format.support.DefaultFormattingConversionService$NoJodaDateTimeFormatAnnotationFormatterFactory.getPrinter(DefaultFormattingConversionService.java:127) 
     org.springframework.format.support.DefaultFormattingConversionService$NoJodaDateTimeFormatAnnotationFormatterFactory.getPrinter(DefaultFormattingConversionService.java:1) 
     org.springframework.format.support.FormattingConversionService$AnnotationPrinterConverter.convert(FormattingConversionService.java:221) 
     org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:35) 
     org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:171) 
     org.springframework.validation.AbstractPropertyBindingResult.formatFieldValue(AbstractPropertyBindingResult.java:125) 
     org.springframework.validation.AbstractBindingResult.getFieldValue(AbstractBindingResult.java:222) 
     org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:120) 
     org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:178) 
     org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:198) 
     org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:164) 
     org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.autogenerateId(AbstractDataBoundFormElementTag.java:151) 
     org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.resolveId(AbstractDataBoundFormElementTag.java:142) 
     org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.writeDefaultAttributes(AbstractDataBoundFormElementTag.java:126) 
     org.springframework.web.servlet.tags.form.AbstractHtmlElementTag.writeDefaultAttributes(AbstractHtmlElementTag.java:421) 
     org.springframework.web.servlet.tags.form.InputTag.writeTagContent(InputTag.java:142) 
     org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:102) 
     org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:79) 
     org.apache.jsp.WEB_002dINF.views.persona_jsp._jspx_meth_form_005finput_005f5(persona_jsp.java:317) 
     org.apache.jsp.WEB_002dINF.views.persona_jsp._jspx_meth_form_005fform_005f0(persona_jsp.java:135) 
     org.apache.jsp.WEB_002dINF.views.persona_jsp._jspService(persona_jsp.java:76) 
     org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) 
     javax.servlet.http.HttpServlet.service(HttpServlet.java:847) 
     org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439) 
     org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395) 
     org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339) 
     javax.servlet.http.HttpServlet.service(HttpServlet.java:847) 
     org.apache.jasper.runtime.JspRuntimeLibrary.include(JspRuntimeLibrary.java:954) 
     org.apache.jasper.runtime.PageContextImpl.doInclude(PageContextImpl.java:688) 
     org.apache.jasper.runtime.PageContextImpl.include(PageContextImpl.java:682) 
     org.apache.tiles.jsp.context.JspTilesRequestContext.include(JspTilesRequestContext.java:103) 
     org.apache.tiles.jsp.context.JspTilesRequestContext.dispatch(JspTilesRequestContext.java:96) 
     org.apache.tiles.renderer.impl.UntypedAttributeRenderer.write(UntypedAttributeRenderer.java:61) 
     org.apache.tiles.renderer.impl.AbstractBaseAttributeRenderer.render(AbstractBaseAttributeRenderer.java:103) 
     org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:669) 
     org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:337) 
     org.apache.tiles.jsp.taglib.InsertAttributeTag.render(InsertAttributeTag.java:234) 
     org.apache.tiles.jsp.taglib.InsertAttributeTag.render(InsertAttributeTag.java:211) 
     org.apache.tiles.jsp.taglib.RenderTag.doEndTag(RenderTag.java:220) 
     org.apache.jsp.WEB_002dINF.template.default_.template1_jsp._jspx_meth_tiles_005finsertAttribute_005f2(template1_jsp.java:284) 
     org.apache.jsp.WEB_002dINF.template.default_.template1_jsp._jspService(template1_jsp.java:115) 
     org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) 
     javax.servlet.http.HttpServlet.service(HttpServlet.java:847) 
     org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439) 
     org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395) 
     org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339) 
     javax.servlet.http.HttpServlet.service(HttpServlet.java:847) 
     org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 
     org.apache.tiles.servlet.context.ServletTilesRequestContext.forward(ServletTilesRequestContext.java:241) 
     org.apache.tiles.servlet.context.ServletTilesRequestContext.dispatch(ServletTilesRequestContext.java:222) 
     org.apache.tiles.renderer.impl.TemplateAttributeRenderer.write(TemplateAttributeRenderer.java:44) 
     org.apache.tiles.renderer.impl.AbstractBaseAttributeRenderer.render(AbstractBaseAttributeRenderer.java:103) 
     org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:669) 
     org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:689) 
     org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:643) 
     org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:626) 
     org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:322) 
     org.springframework.web.servlet.view.tiles2.TilesView.renderMergedOutputModel(TilesView.java:124) 
     org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:262) 
     org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1180) 
     org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:950) 
     org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852) 
     org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882) 
     org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778) 
     javax.servlet.http.HttpServlet.service(HttpServlet.java:734) 
     javax.servlet.http.HttpServlet.service(HttpServlet.java:847) 
     org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 
     org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) 
     org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118) 
     org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84) 
     org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
     org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113) 
     org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
     org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103) 
     org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
     org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113) 
     org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
     org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54) 
     org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
     org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45) 
     org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
     org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:183) 
     org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
     org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105) 
     org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
     org.springframework.security.web.session.ConcurrentSessionFilter.doFilter(ConcurrentSessionFilter.java:125) 
     org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
     org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87) 
     org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
     org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192) 
     org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160) 


org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346) 
    org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259) 
+0

是的,我正在使用Spring MVC。我沒有放置控制器的代碼,因爲我沒有做任何事情;我只是「添加」我的pojo到「模型」 – MDP

+0

請檢查我的答案。您可以投票並接受,如果它達到目的! – SyntaX

回答

2

從異常JodaTime library not available - @DateTimeFormat not supported,很明顯的是, joda-time在構建路徑中缺少jar。

請,如果你正在使用maven添加Joda-time的lib到構建路徑或你的依賴 列表。

API doc明確提到@DateTimeFormat支持風格模式,ISO日期時間模式,或自定義格式模式字符串格式化。可應用於java.util.Datejava.util.Calendar,java.long.Long,Joda-Time值類型;並且從Spring 4和​​,到JSR-310 java.time類型。

請不要使用@DateTimeFormat和java.sql.Date,因爲它不支持 。

+1

downvoter評論請嗎? – soorapadman

+0

這不是我! :| – MDP

+0

我添加了joda-tima-2.2.jar並將java.sql.Date轉換爲java.util.Date,但我一直在獲取「JodaTime庫不可用」。自從我使用Spring和Hibernate以來,我需要特定版本的Joda時間嗎? – MDP