Announcement Announcement Module
Collapse
No announcement yet.
Converter Trouble with JodaTime - Please Help Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Converter Trouble with JodaTime - Please Help

    I am a bit confused about Conversion. I have read the documentation and unfortunately am either not getting it or not understanding the process the application goes through on submit.

    The goal is that the DateTime objects in my Domain model will get passed from a webpage via a format, converted, and then added to the domain model to be persisted elsewhere. From the blog I read on springsource.org it appears that using the @DateTimeFormat(format="blah") should allow for conversion. However, I have all the parts correctly setup but yet I receive the following on POST:
    Code:
    Failed to convert property value of type java.lang.String to required type org.joda.time.DateTime 
    for property startDate; 
    nested exception is org.springframework.core.convert.ConversionFailedException: 
        Failed to convert from type java.lang.String to type @javax.persistence.Column 
        @org.springframework.format.annotation.DateTimeFormat org.joda.time.DateTime for 
        value 12/31/2011 18:14 ; nested exception is java.lang.IllegalArgumentException: 
            Invalid format: "12/31/2011 18:14 " is malformed at " "
    Eventually I have to convert an "Integer" to another object (Webform passes it back as String from a Select List). I am NOT using Webflow. Any help would be appreciated. Below is all I think you might need to help me resolve this.

    Thanks in advance.

    I have a Domain Model of
    Code:
    @Entity
    @Table(name = "RESERVATION")
    public class Reservation {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name = "id")
        private int id;
    
        @ManyToOne(targetEntity = Account.class, fetch = FetchType.EAGER)
        @JoinColumn(name = "account_id")
        private Account owner;
    
        @Column(nullable = false)
        private String purpose;
    
        @Column(nullable = false)
        @DateTimeFormat(pattern="MM/dd/yyyy HH:mm")
        private DateTime startDate;
    ......
    My context.xml has the following:
    Code:
    	<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean"/>
    	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    		<property name="webBindingInitializer">
    			<bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
    				<property name="conversionService" ref="conversionService" />
    			</bean>
    		</property>
    	</bean>
    My Controller for Posts has:
    Code:
       @RequestMapping(value = "add", method = RequestMethod.POST)
        public String addReservation(@ModelAttribute("reservation") Reservation reservation, BindingResult result,
    	    SessionStatus status) {
    	log.debug("RESERVATION: {}",reservation);
    	reservationValidator.validate(reservation, result);
    	log.debug("RESERVATION AFTER VALIDATION: {}",reservation);
    	if (result.hasErrors()) {
    	    return "addReservation";
    	}
    	try {
    	    log.info("Creating Reservation {}",reservation);
    	    reservationService.createReservation(reservation);
    	} catch (Exception e) {
    	    e.printStackTrace();
    	}
    	return "redirect:reservation";
        }
    The JSP has the following:
    Code:
    <form:form method="POST" modelAttribute="reservation">
    
    	<form:label path="purpose" cssClass="label">Reservation Purpose</form:label>
    	<form:input path="purpose" />
    	<form:errors path="purpose" cssClass="error" />
    
    	<form:label path="frame.id" cssClass="label">Select Frame:</form:label>
           <form:select path="frame.id">
    		<form:option value="0" label="Select" />
    		<form:options items="${frames}" itemValue="id" itemLabel="label" />
    	</form:select>
    	<form:errors path="frame.id" cssClass="error" />
    
    	<spring:bind path="reservation.startDate">
    	<form:label path="startDate">Start Date/Time</form:label>
    	<form:input path="startDate" cssClass="startDate" />
    	<form:errors path="startDate" />
    	</spring:bind>
    				
    	<spring:bind path="reservation.stopDate">
    	<form:label path="stopDate">Start Date/Time</form:label>
    	<form:input path="stopDate" cssClass="stopDate" />
    	<form:errors path="stopDate" />
    	</spring:bind>
    
    	<br>
    
    	<input type="submit" value="Add" />
        </form:form>
Working...
X