将java.util.Date分割为两个h:使用f:convertDateTime表示小时和分钟的inputText字段 [英] Split java.util.Date over two h:inputText fields representing hour and minute with f:convertDateTime

查看:128
本文介绍了将java.util.Date分割为两个h:使用f:convertDateTime表示小时和分钟的inputText字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在这个页面上设置一个日期字段

I would like to set up a Date field in my page like this

|hours| h |minutes|

其中小时和分钟分开的inputText。

where hours and minutes are in separated inputText.

bean有这个日期

import java.util.Date;
...
private Date myDate;
...

,页面是

<h:form>
    ...
    <h:inputText id="myDateHours" maxlength="2" value="#{myBean.myDate}"
        <f:convertDateTime pattern="HH" />
    </h:inputText>

    <h:outputText value=" h " />

    <h:inputText id="myDateMinutes" maxlength="2" value="#{myBean.myDate}"
        <f:convertDateTime pattern="mm" />
    </h:inputText>
    ...
</h:form>

但问题是当我提交表单时,最后一个元素被保存。
例如,如果我键入小时,然后输入分钟,则会覆盖小时数,结果为

But the problem is that when I submit the form only the last element is saved. For instance if I type the hours and then the minutes, the hours are overwritten and the result is

| 00 | h | minutes |

我试图设置

<h:inputText id="myDateHours" value="#{myBean.myDate.hours}></h:inputText>

<h:inputText id="myDateMinutes" value="#{myBean.myDate.minutes}></h:inputText>

但我得到一个

Cannot convert 01/01/70 01:00 of type class java.util.Date to int

我不想用两个int字段替换我的日期字段(小时和分钟...)
你有想法吗?

I don't want to replace my date field with two int field (hours and minutes...) Do you have an idea?

谢谢

推荐答案

如果您想使用单个模型值,这种情况是不可能的。

This particular case is not possible if you want to use a single model value.

然而,这是复合组件的完美候选人。它允许您将单个模型值绑定到一组紧密相关的现有组件,并在后台组件中执行处理/转换,完全与视图和后备bean分离。本文中可以找到一个示例:复合组件具有多个输入字段。这个例子可以根据你的具体情况改变如下:

This is however a perfect candidate for a composite component. It allows you to bind a single model value to a group of closely related existing components and perform the processing/conversion in the backing component, fully decoupled from the view and backing bean. One of the examples can be found in this article: composite component with multiple input fields. This example can in for your specific case be altered as follows:

/resources/components/inputTime.xhtml

<ui:component
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:cc="http://java.sun.com/jsf/composite"
>
    <cc:interface componentType="inputTime">
        <cc:attribute name="value" type="java.util.Date" shortDescription="The selected time. Defaults to now." />
    </cc:interface>
    <cc:implementation>
        <span id="#{cc.clientId}" style="white-space:nowrap">
            <h:inputText id="hour" binding="#{cc.hour}" maxlength="2" converter="javax.faces.Integer" />h
            <h:inputText id="minute" binding="#{cc.minute}" maxlength="2" converter="javax.faces.Integer" />
        </span>
    </cc:implementation>
</ui:component>

com.example.InputTime


com.example.InputTime

@FacesComponent("inputTime")
public class InputTime extends UIInput implements NamingContainer {

    private UIInput hour;
    private UIInput minute;

    /**
     * As required by <cc:interface>.
     */
    @Override
    public String getFamily() {
        return UINamingContainer.COMPONENT_FAMILY;
    }

    /**
     * Set initial hour and minute based on model.
     */
    @Override
    public void encodeBegin(FacesContext context) throws IOException {
        Calendar calendar = Calendar.getInstance();
        Date date = (Date) getValue();

        if (date != null) {
            calendar.setTime(date);
        }

        hour.setValue(calendar.get(Calendar.HOUR_OF_DAY));
        minute.setValue(calendar.get(Calendar.MINUTE));
        super.encodeBegin(context);
    }

    /**
     * Returns the submitted value in HH-mm format.
     */
    @Override
    public Object getSubmittedValue() {
        return hour.getSubmittedValue() + "-" + minute.getSubmittedValue();
    }

    /**
     * Converts the submitted value to concrete {@link Date} instance.
     */
    @Override
    protected Object getConvertedValue(FacesContext context, Object submittedValue) {
        try {
            return new SimpleDateFormat("HH-mm").parse((String) submittedValue);
        }
        catch (ParseException e) {
            throw new ConverterException(e);
        }
    }

    public UIInput getHour() {
        return hour;
    }

    public void setHour(UIInput hour) {
        this.hour = hour;
    }

    public UIInput getMinute() {
        return minute;
    }

    public void setMinute(UIInput minute) {
        this.minute = minute;
    }

}

用法:

<html ... xmlns:my="http://java.sun.com/jsf/composite/components">
...
<my:inputTime value="#{bean.date}" />



另请参见:



查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆