gpt4 book ai didi

java - 从 JSP 获取日期到 Controller (在 Spring Mvc 中)

转载 作者:行者123 更新时间:2023-11-30 03:12:42 24 4
gpt4 key购买 nike

我想获取从 JSP 到 Controller 的日期,但我尝试了所有方法,但仍然没有得到结果:

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;
}
}

然后我得到这个异常

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)

最佳答案

我认为您需要一个日期绑定(bind)。

尝试将其放入您的 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));
}

这样spring就会知道如何将字符串转换为日期(记住Request对象只是一个字符串映射)

关于java - 从 JSP 获取日期到 Controller (在 Spring Mvc 中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33281031/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com