gpt4 book ai didi

java - 如何使用 Spring MVC 在 DTO 上绑定(bind)日历

转载 作者:行者123 更新时间:2023-11-30 07:18:16 39 4
gpt4 key购买 nike

我有一个带有日期字段的表单。我想知道如何使用 Calendar 属性绑定(bind) 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";
}

数据传输

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)

最佳答案

与其使用 Calendar 对象,我强烈建议您使用 Joda Time因为您可以灵活地进行日期时间操作,而且最重要的是,它是不可变的。将 Joda DateTime 转换为 java.sql.Date 就这么简单:-

DateTime dateTime = ...;
java.sql.Date sqlDate = new java.sql.Date(dateTime.getMillis());

这是一个例子:-

Spring 表格

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

@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>

关于java - 如何使用 Spring MVC 在 DTO 上绑定(bind)日历,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15500409/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com