Composite组件中的读/写属性

时间:2017-03-31 12:54:23

标签: jsf-2 composite

我正在编写一个由jquery-ui的datepicker支持的JSF datepicker复合组件。(我没有选择使用组件库)

我已经掌握了这项功能,但我现在正试图通过删除不必要的属性来简化组件的界面。

我的组件公开的其中一个属性称为" value"。我希望组件的用户在这里提供el参考,例如。     #{myCarBean.selectedCar}

我注意到的是,组件似乎只读取了属性的值。似乎永远不会调用setter。

我设法通过将此方法公开为clientBehaviour来解决这个问题。 但是现在我有一个actionListener,它通过这个clientBehaviour(设置值)以及"值"来调用。从#{cc.attrs.values}读取值的属性。

我怎样才能拥有一个可以读/写"值"的属性?属性到使用页面提供的底层managedBean?

以下是代码:

<ui:component xmlns="http://www.w3.org/1999/xhtml" xmlns:util="http://discovery.co.za/mytags" xmlns:c="http://java.sun.com/jsp/jstl/core" 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="modal">
    <cc:attribute name="value" type="java.util.Date" shortDescription="An El expression representing the managed bean field to which the selected date will be wired."></cc:attribute>
    <cc:attribute name="minYear" type="java.lang.Integer" shortDescription="An El expression representing a managed bean field which should return an Integer indicating the year component of the minDate"></cc:attribute>
    <cc:attribute name="minMonth" type="java.lang.Integer" shortDescription="An El expression representing a managed bean field which should return an Integer indicating the month component of the minDate.(January is 0)"></cc:attribute>
    <cc:attribute name="minDay" type="java.lang.Integer" shortDescription="An El expression representing a managed bean field which should return an Integer indicating the day component of the minDate."></cc:attribute>
    <cc:attribute name="maxYear" type="java.lang.Integer" shortDescription="An El expression representing a managed bean field which should return an Integer indicating the year component of the maxDate"></cc:attribute>
    <cc:attribute name="maxMonth" type="java.lang.Integer" shortDescription="An El expression representing a managed bean field which should return an Integer indicating the month component of the maxDate"></cc:attribute>
    <cc:attribute name="maxDay" type="java.lang.Integer" shortDescription="An El expression representing a managed bean field which should return an Integer indicating the day component of the maxDate"></cc:attribute>
    <cc:attribute name="render" type="java.lang.String" />

    <cc:clientBehavior name="change" targets="#{cc.clientId}:dateInput" event="change" />
</cc:interface>
<cc:implementation>
    <div id="#{cc.clientId}">
        <h:inputText id="dateInput" value="#{cc.attrs.value}" styleClass="jquery-datepicker"
            onclick="modifyDatePickerOptions('#{cc.attrs.clientId}', #{cc.attrs.minYear}, #{cc.attrs.minMonth}, #{cc.attrs.minDay}, #{cc.attrs.maxYear}, #{cc.attrs.maxMonth}, #{cc.attrs.maxDay} );">
            <f:ajax execute="@form"  event="change"  render="#{cc.attrs.render}" />
        </h:inputText>

    </div>
</cc:implementation>

使用页面:

<h:form id="datePickerForm">
            <my:datepicker  render=":datePickerForm:datePickerAjax" value="#{datePickerBean.selectedDate}" minYear="1999" minMonth="0" minDay="1" maxYear="2019" maxMonth="0" maxDay="1">
                <f:ajax  event="change" listener="#{datePickerBean.selectedDate}" />
            </my:datepicker>
            <h:panelGroup id="datePickerAjax" layout="block">
            <br />
                <b><h:outputText value="You selected : #{datePickerBean.selectedDate}" rendered="#{datePickerBean.selectedDate != null}" /></b>
            </h:panelGroup>
        </h:form>

1 个答案:

答案 0 :(得分:0)

您需要在inputText字段中添加转换器。您可以在<f:convertDateTime/>中嵌套inputText标记来实现此目的。有关所有可用属性,请参阅convertDateTime