2013-04-29 56 views
0

如何創建ofer_has_location對象(加入從位置和奧弗對象)使用當前奧弗以及從H所選項目:selectManyCheckBox如何在jsf中實現foreach?

<h:selectOneMenu id="companyidCompany" 
    value="#{oferController.selected.companyidCompany}" 
    title="#{bundle.CreateOferTitle_companyidCompany}" 
    required="true" 
    requiredMessage="#{bundle.CreateOferRequiredMessage_companyidCompany}"> 
    <f:ajax event="valueChange" execute="companyidCompany" 
     render="locationCollection" /> 
    <f:selectItems value="#{companyController.itemsAvailableSelectOne}"/> 
</h:selectOneMenu> 

<h:outputLabel value="#{bundle.CreateOferLabel_locationCollection}" 
    for="locationCollection" />  
<h:selectManyListbox id="locationCollection" value="locations" 
    title="#{bundle.CreateOferTitle_locationCollection}">      
    <c:forEach items="locations">  
     <f:selectItems var="locations" 
      value="#{oferController.selected.companyidCompany.locationCollection}" /> 
    </c:forEach> 
</h:selectManyListbox> 
+1

沒有理由使用''。只要擺脫它,提供轉換器,在列表框中使用正確的值綁定,並在AJAX監聽器方法中準備必要的數據,就是這樣。 – skuntsel 2013-04-30 02:13:12

+0

@skuntsel,你需要打破所有的下降 – kolossus 2013-04-30 02:22:24

+0

@kolossus然後,我會離開OP沒有他的個人經驗,這可能會對他的寫真實世界的應用程序的能力產生負面影響:) – skuntsel 2013-04-30 06:47:47

回答

1

需要,以實現「連接要素」的功能做什麼:

  1. 有兩個元件(<h:selectOneMenu><h:selectManyLisBox>你的情況),其中,第二個將依賴於所述第一個的選定選項(多個)。第二個元素必須有一個id才能在之後被重新渲染。
  2. 每個HTML select元素(由您選擇的兩個JSF標記呈現)將具有一組option s,它們不應通過像<c:forEach>這樣的迭代元素創建(儘管它實際上可能),而是通過<f:selectItem>/<f:selectItems>標籤(因此,在註釋中刪除您的迭代標籤)。
  3. 當組件中的值不是普通的String或原始包裝(Integer等),而是作爲模型對象(YourClass對象等)綁定時,那麼您需要告訴JSF兩件事情:如何能夠它會從您的類中打印optionvalue以及如何從請求參數(字符串)重建對象。爲此,您需要實現Converter,即解釋JSF如何執行上述轉換。作爲參考點使用this answerBalusC's blog。請注意0​​here的適當語法。
  4. 由這兩個組件綁定的模型值也需要表示您的類,就像選定項目的值一樣。對於<h:selectOneMenu>它是value="#{}";對於<h:selectManyListbox>,它分別是value="#{}"YourClass selectOneMenuValueList<YourClass> selectManyListboxValuesYourClass[] selectManyListboxValues bean屬性。
  5. 第二個select的人口將通過<f:ajax>標籤處理。由於內容需要「即時」計算,因此要使其處於其listener屬性(即具有List<YourClass> contentsOfSecondListbox = createListboxValues(YourClass oneMenuSelectedOption);)的正確位置。因爲您希望重新渲染第二個元素,請在render屬性<f:ajax>中指定其客戶端ID。示例here

如果您有約束力,例如對String/String[]值,您將不需要轉換器部件。

試着一步一步找出你的錯誤並糾正錯誤。

+0

我不能讓它工作,值我的自定義轉換器getAsObject方法返回「開」,我不知道爲什麼 – 2013-05-01 22:14:00

+0

得到它的工作!!!!!!非常感謝你!!! jpa甜美地詮釋了連接表,它像一個魅力一樣工作!非常感謝你!!!! – 2013-05-02 01:51:08