簡體   English   中英

獲取從JSP到Controller的日期(在Spring Mvc中)

[英]Get Date from JSP to Controller (in Spring Mvc)

我想將Date從我的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>

調節器

@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)

我認為您需要日期綁定。

嘗試將其放在您的控制器中

 @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將知道如何將String轉換為Date(記住Request對象只是String映射)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM