更新作为复合组件中的参数的值

时间:2012-12-26 16:46:30

标签: jsf-2 richfaces composite-component

我使用this个问题来创建一个具有时间选择器行为的复合组件。

这是我的复合xhtml

<ui:composition 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"
xmlns:rich="http://richfaces.org/rich">
<cc:interface componentType="customTimeBean">
    <cc:attribute name="date" type="java.util.Date" required="true" />
</cc:interface>
<cc:implementation>
    <rich:inputNumberSpinner value="#{cc.hours}" minValue="0"
        maxValue="23" />
    <h:outputLabel value=":" />
    <rich:inputNumberSpinner value="#{cc.minutes}" minValue="0"
        maxValue="59" />
</cc:implementation>
</ui:composition>

这是我的Faces组件

@FacesComponent(value = "customTimeBean")
public class CustomTimeBean extends UINamingContainer {

    private Date getDate() {

        Date d = (Date) getAttributes().get("date");
        if (d == null) {
            //throw new RuntimeException("Date no debe ser nulo");
            d = new date();
        }
        return d;
    }

    public void setMinutes(int value) {

        getDate().setMinutes(value);
    }

    public void setHours(int value) {

        getDate().setHours(value);
    }

    public int getMinutes() {

        return getDate().getMinutes();
    }

    public int getHours() {

        return getDate().getHours();
    }

    public void setSeconds(int value) {

        getDate().setHours(value);
    }

    public int getSeconds() {

        return getDate().getSeconds();
    }

}

用法

<sigh:time date="#{bean.date}" hasSeconds="false"/>

我的测试用例:

<h:outputLabel value="#{controller.date}" id="date" />
<sigh:time date="#{controller.date}" />
<a4j:commandButton render="date" />

“控制器”:

@ManagedBean
public class Controller  {

    Date date;

    public Date getDate() {

        return date;
    }

    public void setDate(Date date) {

        this.date = date;
    }
}     

这很好用,但是,当我将null Date作为属性传递时,这不起作用,如何更新Bean中的值(controller.date)?

在我的测试用例中,当我按下a4j:commandButton并且控制器中的日期为null时,outputlabel不显示任何内容(date为null),当日期不为null时,每次单击Button时日期都会更新

抱歉我的英语不好。

谢谢!

1 个答案:

答案 0 :(得分:0)

只需替换

throw new RuntimeException("Date no debe ser nulo");

通过

d = new Date();

对于具体问题

无关,您在其中使用的Date方法已弃用。另请参阅javadoc。主要原因是它没有正确处理时区/ DST。您应该在组合内部使用java.util.Calendar代替。