如何通过托管bean填充属性?

时间:2019-06-20 12:34:46

标签: oracle-adf

我有一个带有表单的jsf页面,我需要通过一个受管bean(通过按钮)更新表单的属性(显然是当前的)。 所讨论的受管Bean已经存在并执行其他代码,即将文件上传到服务器并获取完整的文件路径(它返回一个字符串,例如file_name)。 我希望表单的属性(名为path的输入文本)在每次上传文件时都获得file_name值

1 个答案:

答案 0 :(得分:0)

在Oracle ADF中可以通过多种方式以编程方式设置视图属性的值,这是其中两种:

  1. 使用强烈推荐的JSFUtils.java库函数的JSF方法:
/**
 * Method for setting a new object into a JSF managed bean
 * Note: will fail silently if the supplied object does
 * not match the type of the managed bean.
 * @param expression EL expression
 * @param newValue new value to set
 */
public static void setExpressionValue(String expression, Object newValue) {
    FacesContext facesContext = getFacesContext();
    Application app = facesContext.getApplication();
    ExpressionFactory elFactory = app.getExpressionFactory();
    ELContext elContext = facesContext.getELContext();
    ValueExpression valueExp = elFactory.createValueExpression(elContext, expression, Object.class);

    //Check that the input newValue can be cast to the property type
    //expected by the managed bean.
    //If the managed Bean expects a primitive we rely on Auto-Unboxing
    Class bindClass = valueExp.getType(elContext);
    if (bindClass.isPrimitive() || bindClass.isInstance(newValue)) {
        valueExp.setValue(elContext, newValue);
    }
}

JSFUtils.setExpressionValue("#{bindings.YOUR_VO_ATTRIBUTE.inputValue}","YOUR VALUE");
  1. 通过将jsf组件绑定到您的ADF Bean

转到组件>打开属性检查器>将绑定属性设置为Bean(将创建以下getter和setter)

public void setMyInputText(RichInputText myInputText) {
    this.myInputText = myInputText;
}

public RichInputText getMyInputText() {
    return myInputText;
}

//then in your action you can just set and refresh component 
this.setMyInputText(YourValue);
AdfFacesContext.getCurrentInstance().addPartialTarget(this.getMyInputText);

https://gist.github.com/CedricL46/6cc291ce80601f50b66973e1000690a9