编写自定义标记以实现“基于下拉菜单”的日期选择器

时间:2013-08-14 23:08:46

标签: java spring jsp tags datepicker

我想为包含3个下拉菜单(日,月和年)的日期选择器编写自定义标记。此日期选择器的动态参数只是“名称”和从当前年份显示的年数。在我的JSP中使用此标记时,它将类似于:

<library:dropdownDatePicker name="example" yearsBack="10"/>

我知道如何扩展BodyTagSupport类并为此编写HTML内容,方法是编写3个<form:select />元素并用选项填充它们。我不知道的是如何将选定的日,月和年绑定到单个日期字段。我知道如何在我的Java应用程序中执行此操作,但不知道如何在自定义标记定义中执行此操作。

理想情况下,我想将这个日期字段与Spring绑定到我的表单中Bean的日期字段:

<form:form commandName="formBean">
    <library:dropdownDatePicker name="example" yearsBack="10" path="someDateFieldInFormBean"/>
</form:form>

我希望这个解释清楚。有人可以帮我这个吗?

非常感谢。

1 个答案:

答案 0 :(得分:0)

在dropdownDatePicker中,您有$ {path}(在您的示例中为“someDateFieldInFormBean”)。 使用访问者和变换器扩展包含年,月和日字段的类型,然后在dropdownDatePicker标记中,将字段的路径设置为$ {path} .year,$ {path} .month和$ {path} .day

例如: 控制器:

@RequestMapping(value = BASE_URL + "/test", produces = TEXT_HTML) public String test(final Model uiModel) {

    Formstuff formstuff = new Formstuff();
    DatePicker datePicker = new DatePicker();
    datePicker.setYear(1234L);
    datePicker.setMonth(2345L);
    datePicker.setDay(3456L);
    formstuff.setDatePicker(datePicker);

    uiModel.addAttribute("formstuff", formstuff);

    return "test";
}

@RequestMapping(value = BASE_URL + "/test", produces = TEXT_HTML) public String test(final Model uiModel) { Formstuff formstuff = new Formstuff(); DatePicker datePicker = new DatePicker(); datePicker.setYear(1234L); datePicker.setMonth(2345L); datePicker.setDay(3456L); formstuff.setDatePicker(datePicker); uiModel.addAttribute("formstuff", formstuff); return "test"; }

Formstuff类:

class Formstuff {
    private DatePicker datePicker;

    public DatePicker getDatePicker() {
        return datePicker;
    }

    public void setDatePicker(final DatePicker datePicker) {
        this.datePicker = datePicker;
    }

    @Override
    public String toString() {
        return "Formstuff [datePicker=" + datePicker + "]";
    }

}

Datepicker类:​​

class Formstuff { private DatePicker datePicker; public DatePicker getDatePicker() { return datePicker; } public void setDatePicker(final DatePicker datePicker) { this.datePicker = datePicker; } @Override public String toString() { return "Formstuff [datePicker=" + datePicker + "]"; } }

相关的jsp:

public class DatePicker {
    private Long year;
    private Long month;
    private Long day;

    public Long getYear() {
        return year;
    }

    public void setYear(final Long year) {
        this.year = year;
    }

    public Long getMonth() {
        return month;
    }

    public void setMonth(final Long month) {
        this.month = month;
    }

    public Long getDay() {
        return day;
    }

    public void setDay(final Long day) {
        this.day = day;
    }

    @Override
    public String toString() {
        return "DatePicker [year=" + year + ", month=" + month + ", day=" + day + "]";
    }
}

public class DatePicker { private Long year; private Long month; private Long day; public Long getYear() { return year; } public void setYear(final Long year) { this.year = year; } public Long getMonth() { return month; } public void setMonth(final Long month) { this.month = month; } public Long getDay() { return day; } public void setDay(final Long day) { this.day = day; } @Override public String toString() { return "DatePicker [year=" + year + ", month=" + month + ", day=" + day + "]"; } }

标签:

<jsp:root xmlns:c="http://java.sun.com/jsp/jstl/core"
    xmlns:util="urn:jsptagdir:/WEB-INF/tags/util"
    xmlns:jsp="http://java.sun.com/JSP/Page"
    xmlns:form="http://www.springframework.org/tags/form" version="2.0">

    <jsp:directive.page contentType="text/html;charset=UTF-8" />
    <jsp:output omit-xml-declaration="yes" />

    <util:pageBody title="Datepicker test page" >
        <form:form commandName="formstuff">
            <util:datepicker name="example" yearsBack="10" path="datePicker"/>
        </form:form>
    </util:pageBody>
</jsp:root>

<jsp:root xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:util="urn:jsptagdir:/WEB-INF/tags/util" xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:form="http://www.springframework.org/tags/form" version="2.0"> <jsp:directive.page contentType="text/html;charset=UTF-8" /> <jsp:output omit-xml-declaration="yes" /> <util:pageBody title="Datepicker test page" > <form:form commandName="formstuff"> <util:datepicker name="example" yearsBack="10" path="datePicker"/> </form:form> </util:pageBody> </jsp:root>

我希望,我的回答很有帮助。