2012-03-29 80 views
0

我是JDeveloper和ADF的新手,我在從selectOneChoice組件獲取選定值時遇到了一些問題。這是valuChangeListener:從ADF中的selectOneChoice獲取選定值,而不是索引

public void versionValueChangeListener(ValueChangeEvent valueChangeEvent) { 
    System.out.println(valueChangeEvent.getOldValue().toString()); 
    System.out.println(valueChangeEvent.getNewValue().toString()); 

} 

這是給被選擇的選擇的指標,而不是文字本身。我如何獲得文本而不是索引? 這是selectOneChoice代碼:

<af:selectOneChoice value="#{bindings.Version.inputValue}" 
             label="#{bindings.Version.label}" 
             required="#{bindings.Version.hints.mandatory}" 
             shortDesc="#{bindings.Version.hints.tooltip}" 
             id="soc3" autoSubmit="true" 
             valueChangeListener="#{savesBean.versionValueChangeListener}"> 
        <f:selectItems value="#{bindings.Version.items}" id="si3"/> 
        </af:selectOneChoice> 

感謝名單:)

回答

1

這是ORCLE的傢伙是怎麼做的

How-to get the selected af:selectOneChoice Label雖然在我看來,它可以在其他的方式來完成.. 。

我想你最好建立一個map其中指數將是關鍵和值是標籤

比versionValueChangeListener則可以直接進入地圖是這樣的:

myMap.get(valueChangeEvent.getNewValue().toString());

0

這不是ADF具體。這是HTML特定的。僅發送了HTML <input type="radio">元素的值,而不是HTML <label>元素的值。對於每個其他HTML <input>,<select><textarea>元素都是如此。

對「問題」的解決很簡單:您的背景bean中的所有標籤都位於#{bindings.Version.items}後面的集合中。只需根據所選值從該處獲取標籤即可。

或者,將整個複雜對象(包含值和標籤)設置爲項目值而不是其值。您只需要一個Converter即可在複雜對象和字符串之間進行轉換。

0

jsp/jsf頁面上有一個用於選擇一個選項的屬性,允許您傳遞實際對象值或從列表中傳遞索引值。單擊jsp/jsf頁面中的選擇項,然後點擊底部的綁定選項卡,轉到頁面定義(您將在綁定頁面的頂部看到它),並且選中的一個選項將突出顯示在現在打開的頁面定義文件中。如果您從這裏查看屬性檢查器 - 默認情況下有一個名爲「SelectItemValueMode」的屬性,它將設置爲ListIndex值,您可以從此處將其更改爲ListObject。它是在頁面定義文件的屬性窗口中爲選擇一個選擇列表對象列出的最後一個屬性。

+0

@BalusC - 這不是ADF具體的,如果你看過他的問題 - 這是JDeveloper的具體。 JDeveloper有超過3000頁的手冊是有原因的。 – GrrLex 2013-08-19 18:30:22

-1

你可以找到以下網址這種情況的解決方案:https://blogs.oracle.com/adf/entry/getting_selected_value_from_selectonechoice

假設我們有一個綁定到DEPTNO屬性 在DNAME和selectOneChoice的 顯示值DEPTNO屬性模型驅動的列表JSPX頁面

<af:selectOneChoice value="#{bindings.Deptno.inputValue}" label="Select Department" 
          required="true" shortDesc="#{bindings.Deptno.hints.tooltip}" 
          id="soc1" autoSubmit="true"> 
      <f:selectItems value="#{bindings.Deptno.items}" id="si1"/> 
</af:selectOneChoice> 

當我們要選擇的值,我們做的常見錯誤是使用 相同的EL勢必selectOneChoice組件的value屬性, 但是使用這個,我們得到的是所選項目的索引,而不是 。這是因爲當我們將屬性 SelectOneChoice拖放到頁面上時,SelectItems會以 索引作爲值生成。

顯示到頁面JSPX

在本節中選擇的值,我們可以看到如何讓選擇的值,而無需編寫Java代碼 一行。創建一個outputText組件,其 value屬性綁定到#{bindings.Deptno.attributeValue},而不是 #{bindings.Deptno.inputValue},並根據列表選擇通過添加partialTriggers屬性來使其可刷新。

<af:outputText value = "Selected Value: #{bindings.Deptno.attributeValue}" id="ot1" partialTriggers="soc1"/> 

上面的代碼給出了所選項目的Deptno值。如果'SALES'的 Deptno是30,則將在outputText上顯示30,其中 從列表中選擇'SALES'。

如果我們希望顯示「SALES」本身則下面EL應 可以使用假設DNAME是第二屬性DeptView

<af:outputText value = "Display Value: #{bindings.Deptno.selectedValue ne ' ' ? bindings.Deptno.selectedValue.attributeValues[1] : ''}" id="ot2" partialTriggers="soc1"/> 

內部值變化監聽

上述EL表達式評估ValueChangeListener內部不會給出當前選定值 ,而是給出先前選擇的 值,因爲所選值不會被更新爲模型調用時間的ValueChangeListener。

在這種情況下,在訪問選定的值之前,我們需要先更新 模型。

下面是示例代碼:

public void valueChanged(ValueChangeEvent valueChangeEvent) { 
    this.setValueToEL("#{bindings.Deptno.inputValue}", valueChangeEvent.getNewValue()); //Updates the model 
    System.out.println("\n******** Selected Value: "+resolveExpression("#{bindings.Deptno.attributeValue}")); 
    System.out.println("\n******** Display Value: "+resolveExpression("#{bindings.Deptno.selectedValue ne ' ' ? bindings.Deptno.selectedValue.attributeValues[1] : ''}")); 
} 

public Object resolveExpression(String el) {  
    FacesContext facesContext = FacesContext.getCurrentInstance(); 
    ELContext elContext = facesContext.getELContext(); 
    ExpressionFactory expressionFactory = facesContext.getApplication().getExpressionFactory();   
    ValueExpression valueExp = expressionFactory.createValueExpression(elContext,el,Object.class); 
    return valueExp.getValue(elContext); 
} 

public void setValueToEL(String el, Object val) { 
    FacesContext facesContext = FacesContext.getCurrentInstance(); 
    ELContext elContext = facesContext.getELContext(); 
    ExpressionFactory expressionFactory = facesContext.getApplication().getExpressionFactory(); 
    ValueExpression exp = expressionFactory.createValueExpression(elContext, el, Object.class); 
    exp.setValue(elContext, val); 
} 
+0

儘管這個鏈接可能回答這個問題,但最好在這裏包含答案的基本部分,並提供參考鏈接。如果鏈接頁面更改,則僅鏈接答案可能會失效。 - [來自評論](/ review/low-quality-posts/11362471) – 2016-02-22 15:39:40

+0

@AdamMichalik做到了。謝謝你的評論。 – XpiritO 2016-02-22 16:09:29

相關問題