1

While adding Date from jsp to database through Hibernate i got the error. please let me know if anyone knows the solution for this. appreciate for your help. below is my code snippet.

register.jsp

<form:input path="date" id="datepicker"/>

Registration.java

import java.sql.Timestamp;
public class Registration {
private Timestamp date;
    public Timestamp getDate() {
        return date;
    }
    public void setDate(Timestamp date) {
        this.date = date;
    }
}

HelloController.java

@Controller
@RequestMapping("/library/*")
public class HelloController {
    @RequestMapping(value="success", method = RequestMethod.POST)
    public String addUser(@ModelAttribute("register") Registration register, ModelMap model) {
        System.out.println(register.getFirstName());
        registerService.addUsers(register);
        return "register";
    }
}

while adding date from jsp to database i got the below error

org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'register' on field 'date': rejected value [25/11/2014]; codes [typeMismatch.register.date,typeMismatch.date,typeMismatch.java.sql.Timestamp,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [register.date,date]; arguments []; default message [date]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.sql.Timestamp' for property 'date'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [java.sql.Timestamp] for property 'date': no matching editors or conversion strategy found]
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.doBind(HandlerMethodInvoker.java:818)
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:367)
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:171)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:436)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:643)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:659)
1
  • Your date variable in Registration class is of type TimeStamp for which valid data looks like yyyy-mm-dd hh:mm:ss[.fffffffff] where as your input is dd/mm/yyyy Commented Nov 1, 2014 at 19:39

2 Answers 2

3

If you can use java.util.Date object instead of TimeStamp object. Following code will solve your problem,

At your model class, Registration.java

import java.util.Date;
public class Registration {
private Date date;
    public Date getDate() {
        return date;
    }
    public void setDate(Date date) {
        this.date = date;
    }
}

At your controller class, HelloController.java

@Controller
@RequestMapping("/library/*")
public class HelloController {

@Override
@InitBinder
public void initBinder(WebDataBinder binder) {

   SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
   dateFormat.setLenient(false);
   binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}

@RequestMapping(value="success", method = RequestMethod.POST)
public String addUser(@ModelAttribute("register") Registration register, ModelMap model) {
    System.out.println(register.getFirstName());
    registerService.addUsers(register);
    return "register";
}
}

After submitting your jsp form, all the parameters in the request by default will be of type String. In Spring-MVC, it's the duty initBinder to convert all those String values into respective type and bind it with Model class variables.

Sign up to request clarification or add additional context in comments.

Comments

0

The problem seems to be mapping between timestamp object and date string that is received from JSP.

You can solve this problem any of below ways.

  1. Either change your Register class date variable type to String type and later on in your service or DAO layer you can convert it to the TimeStamp Object like below.

    Date date = new Date(date_received_as_string);

    Timestamp timestamp = new Timestamp(date.getTime());

  2. Or you can write your JSON Serializer.First annotate the date variable with JSONSerialize.

    @JsonSerialize(using = CustomSerializer.class)

    private Timestamp date;

After you provide implementation of CustomSerializer class,like below.

public class CustomSerializer extends JsonSerializer<Timestamp> {
    @Override
    public void serialize(Timestamp date, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {

        //Now write you code for converting string to TimeStamp Object.

    }
}

In the same you can also write JsonDeserializer for TimeStamp Object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.