我有一个带有日期字段的表单。我想知道如何使用Calendar属性绑定DTO。我正在尝试使用String,但转换为java.sql.Date非常复杂。
HTML
<input type="text" id="date" name="date" />
Spring MVC
@RequestMapping(value = "/newholiday", method=RequestMethod.POST)
public String cadastrar(ModelMap model, HolidayDTO dto) {
System.out.println(dto.getDate());
return "/newholiday";
}
DTO
public class HolidayDTO extends DTO<Holiday> {
private Calendar date;
}
如果直接,则返回异常:
HTTP ERROR 500
Problem accessing /cemiterio/feriados/cadastrar/. Reason:
org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'holidayDTO' on field 'date': rejected value [23/12/1988]; codes [typeMismatch.holidayDTO.date,typeMismatch.date,typeMismatch.java.util.Calendar,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [holidayDTO.date,date]; arguments []; default message [date]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Calendar' for property 'date'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [java.util.Calendar] for property 'date': no matching editors or conversion strategy found]
Caused by:
org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'holidayDTO' on field 'date': rejected value [23/12/1988]; codes [typeMismatch.holidayDTO.date,typeMismatch.date,typeMismatch.java.util.Calendar,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [holidayDTO.date,date]; arguments []; default message [date]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Calendar' for property 'date'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [java.util.Calendar] for property 'date': no matching editors or conversion strategy found]
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.doBind(HandlerMethodInvoker.java:810)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:359)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:171)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:401)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:766)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:450)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:326)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:945)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:756)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:218)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:410)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)
答案 0 :(得分:4)
我强烈建议您使用Joda Time,而不是使用Calendar
对象,因为您可以灵活地进行日期时间操作,最重要的是,它是不可变的。将Joda DateTime
转换为java.sql.Date
就像这样简单: -
DateTime dateTime = ...;
java.sql.Date sqlDate = new java.sql.Date(dateTime.getMillis());
以下是一个例子: -
Spring Form
public class MyForm {
@DateTimeFormat(pattern = "MM/dd/yyyy")
private DateTime dateTime;
public DateTime getDateTime() {
return dateTime;
}
public void setDateTime(DateTime dateTime) {
this.dateTime = dateTime;
}
}
<强>控制器强>
@Controller
@RequestMapping(value = "/")
public class HomeController {
@RequestMapping(method = RequestMethod.POST)
public String post(@ModelAttribute MyForm myForm) {
System.out.println("@@@@@@: " + myForm.getDateTime());
return "home";
}
}
<强> JSP 强>
<spring:form modelAttribute="myForm">
<spring:input path="dateTime"/>
<spring:button>Submit</spring:button>
</spring:form>