【问题标题】:how to populate attribute via managed bean?如何通过托管 bean 填充属性?
【发布时间】:2019-06-20 12:34:46
【问题描述】:

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

【问题讨论】:

    标签: oracle-adf


    【解决方案1】:

    有多种方法可以在 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-03
      • 2013-07-27
      • 1970-01-01
      相关资源
      最近更新 更多