2017-03-31 60 views
0

我寫一個JSF日期選擇器由jQuery的UI的日期選擇器支持的複合材料部件的屬性。(我沒有使用的元件庫中選擇),我已經得到了功能讀/寫在複合材料部件

工作,但我現在試圖通過刪除不必要的屬性來簡化組件的界面。

我的組件公開的屬性之一稱爲「值」。我期望組件的用戶在這裏提供一個el引用。 #{myCarBean.selectedCar}

我注意到的是,組件似乎只讀取了屬性的值。 setter從來不會被調用。

我設法通過將此方法公開爲clientBehaviour來解決此問題。 但現在我有一個actionListener通過此clientBehaviour(設置值)以及從值#{cc.attrs.values}中讀取值的「value」屬性進行調用。

我該如何擁有一個可以讀/寫「value」屬性到使用頁面提供的底層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> 
+0

出於好奇,爲什麼不這樣做:_「(我沒有選擇使用組件庫)」_ – Kukeltje

+0

@Kukeltje:項目的架構師覺得他們不想依賴組件庫,就像新的和更好的組件庫總是出來,他們不希望遷移開銷。所以我只限於mojarra 2.1。他們至少允許我使用Omnifaces,儘管 –

+0

因此即使使用諸如autoComplete之類的簡單組件,他們也不想使用例如PrimeFaces,但需要定製複雜性,如http://stackoverflow.com/questions/42834630/how-to-execute-client-behaviour-before-the-event-it-targets?好的...祝你好運他們與數據表,過濾,排序分頁,樹表和其他組件;-) – Kukeltje

回答

0

你需要一個轉換器添加到您的inputText領域。您可以在inputText中嵌入<f:convertDateTime/>標籤以實現此目的。有關所有可用屬性,請參閱convertDateTime

+0

謝謝是的,我應該這樣做。設置日期的managedBean實際上將Date聲明爲String(即使在組件的接口中,「value」屬性被聲明爲java.util.Date)..我不確定爲什麼這會起作用。 –