1

I woulde like to get Date from my JSP to my Controller but I tried all but I still not get a result:

JSP

<div>
    <f:form modelAttribute="banqueForm2" method="post"  action="testero33">
        <table>
            <tr>
                <td>Date1 :</td>
                <td><f:input path="date1" type="date" name="date" /> 
                </td>
                <td><f:errors path="date1" cssClass="error">
                    </f:errors></td>

                    <td>Date2 :</td>
                <td><f:input path="date2" type="date"  name="date" /> 
                </td>
                <td><f:errors path="date2" cssClass="error">
                    </f:errors></td>

                <td><input type="submit" value="OK" /></td>
            </tr>
        </table>
    </f:form>
</div>

Controller

@RequestMapping(value="/testero33")
public String testero3(@Valid BanqueForm2  bf, Model model, Date date1, Date date2){ 
    System.out.println("-------------date JSP---------------:  ");
    System.out.println("*** Cosluter Strin ***:  " + date1);
    System.out.println("*** Cosluter Strin ***:  " + date2);

    System.out.println("------------End JSP---------------:  ");

    return "DateTest";
}

BanqueForm2.class

public class BanqueForm2 {

    private Date date1;
    private Date date2;


    public Date getDate1() {
        return date1;
    }
    public void setDate1(Date date1) {
        this.date1 = date1;
    }
    public Date getDate2() {
        return date2;
    }
    public void setDate2(Date date2) {
        this.date2 = date2;
    }
}

Then I get this exception

Field error in object 'banqueForm2' on field 'date1': rejected value [2015-10-01]; codes [typeMismatch.banqueForm2.date1,typeMismatch.date1,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [banqueForm2.date1,date1]; arguments []; default message [date1]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'date1'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type java.util.Date for value '2015-10-01'; nested exception is java.lang.IllegalArgumentException]
Field error in object 'banqueForm2' on field 'date2': rejected value [2015-02-12]; codes [typeMismatch.banqueForm2.date2,typeMismatch.date2,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [banqueForm2.date2,date2]; arguments []; default message [date2]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'date2'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type java.util.Date for value '2015-02-12'; nested exception is java.lang.IllegalArgumentException]org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:111)
Bnrdo
  • 5,325
  • 3
  • 35
  • 63

1 Answers1

2

I think that you need a date binding.

Try to put this in your controller

 @InitBinder
public final void initBinderUsuariosFormValidator(final WebDataBinder binder, final Locale locale) {
    final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", locale);
    binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));    
}

In this way spring will know how to convert a String to a Date (remember that a Request object is just a String map)

Sisifo
  • 91
  • 3
  • This worked for Me but the problem is that I'm getting date who look like this Fri May 22 00:00:00 CEST 2015 Tue Oct 06 00:00:00 CEST 2015 -->> but I would like to get date like this 22-05-2015 how to do Please ????? i would like to query in my data base – Bienfait Rukundo Oct 22 '15 at 13:11
  • You need to parse the date in the JSP. For example http://stackoverflow.com/questions/3457134/how-to-display-a-formatted-datetime-in-spring-mvc-3-0. You can use Date picker with jQuery https://jqueryui.com/datepicker/ – Sisifo Oct 22 '15 at 14:50
  • Date Binding using custom property editor is perfectly works, Thanks. – Sathish Jul 29 '19 at 11:38