0
我woulde想從我的JSP得到日期到我的控制器,但我嘗試了所有,但我還沒有得到一個結果:獲得從JSP到控制器的日期(在Spring MVC)
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>
控制器
@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;
}
}
然後我得到這個例外
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)
嘿Sisifo我不知道我可以把這個方法請在哪裏? –
這適用於我,但問題是我得到的日期看起來像這樣星期五5月22 00:00:00 CEST 2015 Tue Oct 06 00:00:00 CEST 2015 - >>但是我想取得日期像這樣22-05-2015怎麼辦請?????我想在我的數據庫中查詢 –
您需要在JSP中解析日期。例如http://stackoverflow.com/questions/3457134/how-to-display-a-formatted-datetime-in-spring-mvc-3-0。 你可以在jQuery中使用Date選擇器https://jqueryui.com/datepicker/ – Sisifo