假设我们有一个绑定到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);
}
@BalusC - 这不是ADF具体的,如果你看过他的问题 - 这是JDeveloper的具体。 JDeveloper有超过3000页的手册是有原因的。 – GrrLex 2013-08-19 18:30:22