2

日付フィールドを持つフォームがあります。Calendar 属性を持つ DTO にバインドする方法を知りたいです。String で試していましたが、java.sql.Date への変換が非常に複雑でした。

HTML

<input type="text" id="date" name="date" />

春の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)
4

1 に答える 1

4

Calendarオブジェクトを使用する代わりに、Joda Timeを使用することを強くお勧めします。これは、日時の操作を柔軟に行うことができ、最も重要なこととして、不変であるためです。JodaDateTimeをに変換するjava.sql.Dateのはこれと同じくらい簡単です:-

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

次に例を示します。-

春の形

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>
于 2013-03-19T13:23:19.647 に答える