6

これを含むインターフェースを持つ複合コンポーネントがあります:

<cc:attribute name="model"
                  shortDescription="Bean that contains Location" >
        <cc:attribute name="location" type="pkg.Location"
                      required="true" />
    </cc:attribute>
</cc:interface>

したがって、 #{cc.attrs.model.location}を使用して、マークアップ内のLocationオブジェクトにアクセスできます。

また、次のように、複合コンポーネントのバッキング Bean からそのオブジェクトにアクセスします。

    FacesContext fc = FacesContext.getCurrentInstance();
    Object obj = fc.getApplication().evaluateExpressionGet(fc, 
            "#{cc.attrs.model.location}", Location.class);

これで、複合コンポーネントの作業が完了しました。バッキング Bean からモデルのセッター メソッドを呼び出すにはどうすればよいでしょうか。(つまり、model.setLocation(someValue) ?

4

2 に答える 2

8

を使用しValueExpression#setValue()ます。

FacesContext facesContext = FacesContext.getCurrentInstance();
ELContext elContext = facesContext.getELContext();
ValueExpression valueExpression = facesContext.getApplication().getExpressionFactory()
    .createValueExpression(elContext, "#{cc.attrs.model.location}", Location.class);

valueExpression.setValue(elContext, newLocation);

ちなみに、 javadocで説明されているとおりに、カバーの下で呼び出しApplication#evaluateExpressionGet()ます(これまでに読んだことがある場合は...)。ValueExpression#getValue()


具体UIComponent的な問題とは関係ありませんが、複合コンポーネントのバッキング クラスを作成する可能性についてご存知ですか? ValueExpressionこれは、このように sをいじるよりもずっと簡単だと思います。その後、継承getAttributes()されたメソッドを使用してmodel.

Model model = (Model) getAttributes().get("model);
// ...

複合コンポーネントの wiki ページで例を見つけることができます。

于 2011-08-23T15:18:13.043 に答える
1

属性「デフォルト」はどうですか?バッキングコンポーネントの実装を使用する場合、実装されていないことがわかります。

xhtml :

<composite:interface>
    <composite:attribute name="test" 
                         type="java.lang.Boolean" 
                         default="#{false}"/>
</composite:interface>
<composite:implementation >
    TEST : #{cc.attrs.test}
</composite:implementation >

Java バッキングの実装:

 testValue = (Boolean) getAttributes().get("test");

test 属性がメインの xhtml に設定されている場合は問題ありません。xhtml と Java の両方のバッキングが同じ値を持ちます。ただし、設定されていない場合、デフォルト値は xhtml のみです: html には次が含まれます

TEST : false 

ただし、バッキングでは testValue は null です

于 2013-08-01T12:51:39.173 に答える