2012-08-25 5 views
16

JSF 2.0には、別のコンポーネントのクライアントIDを見つけるための組み込みメソッドがありますか?クライアントID関連の質問は約1,000件あり、これを行うための多くの方法がありますが、JSF 2.0が私には分かりません。JSF 2.0で他のコンポーネントのクライアントIDを取得する

#{component.clientId}は、特定のコンポーネントの独自のクライアントIDを評価しますが、別のコンポーネントのIDを参照したいと考えています。

Thisブログ投稿はcomponent.clientIdと書かれており、#{someComponent.clientId}とも言いますが、わからないからです。私はJSF 2.0のリファレンス実装がなくなる前に彼が書いたと思うので、彼はJSRに目を向けるだけで、その機能が変更された可能性があります。よく分かりません。

私はPrimeFacesとRichFacesの両方にクライアントIDを返す独自の関数があることは知っていますが、このための組み込みのJSF 2.0メソッドがあるかどうかは不思議です。ここではいくつかの例を示します。これは、のoutputTextのIDを返すために働く

`<h:outputText value="My client ID : #{component.clientId}" />` 

上記のブログ記事によると、これは動作するはずですが、それはできません。私はただ出力を得ません。

`<h:button id="sampleButton" value="Sample" />` 

`<h:outputText value="sampleButton's client ID : #{sampleButton.clientId}" />` 

これはPrimeFacesで動作します。RichFacesの中

`<h:outputText value="PrimeFaces : sampleButton's client ID : #{p:component('sampleButton')}" />` 

作品:また

`<h:outputText value="RichFaces : sampleButton's client ID : #{rich:clientId('sampleButton')}" />` 

、すべての可能で、私は「勝ったソリューションを探していた場合javax.faces.SEPARATOR_CHARの値を変更した場合、または参照先以外のコンテナを追加または削除した場合は破損します。コンポーネント。ハードコードされたIDパスに起因する問題を追跡するのに多くの時間を費やしました。

答えて

28

binding属性でコンポーネントをビュースコープ内に割り当てる必要があります。

<h:button id="sampleButton" binding="#{sampleButton}" value="Sample" /> 
<h:outputText value="sampleButton's client ID : #{sampleButton.clientId}" /> 
+3

これは素晴らしいことです。私は 'binding'属性が、コンポーネントをバッキングビーンに公開するためだけであると仮定しました。あなたがスコープ上にコンポーネントを公開することはできないと思いました。どうもありがとう。 – cutchin

+0

ようこそ。 – BalusC

+0

私もあなたがそれをすることができたか分からなかった!私は、バインディング/ ELの使用についてのドキュメントでは何も参照していませんでした。これはJSF 2.xの新機能ですか? (すなわち、1.2でも利用可能ですか?) –

1

これは私に役立ちました。私はこのような応答を書くことが幸いであるかどうかを知ることに興味があります。

client.html

<h:outputText value="#{UIHelper.clientId('look-up-address-panel-id')}" /> 

UIHelper.java

@ManagedBean(name = "UIHelper", eager = true) 
@ApplicationScoped 
public class UIHelper 
{ 

public String clientId(final String id) 
{ 
    FacesContext context = FacesContext.getCurrentInstance(); 
    UIViewRoot root = context.getViewRoot(); 
    final UIComponent[] found = new UIComponent[1]; 
    root.visitTree(new FullVisitContext(context), new VisitCallback() 
    { 
    @Override 
    public VisitResult visit(VisitContext context, UIComponent component) 
    { 
     if (component.getId().equals(id)) 
     { 
     found[0] = component; 
     return VisitResult.COMPLETE; 
     } 
     return VisitResult.ACCEPT; 
    } 
    }); 
    return found[0] == null ? "" : "#" + found[0].getClientId().replace(":", "\\\\:"); 
} 

} 
0

これは私のGoogle検索の最初の結果のうちだったと私は

のjavaxを得た理由は、私は疑問に思いましたので。 el.PropertyNotFoundException( 'itemId'プロパティが見つかりません[...])

解決策を受け入れる際には、私のソリューションをJSF 1と共有したいと思います。2:

UIComponentの方法getClientIdは、FacesContextパラメータ(UIComponent documentation参照)を必要とします。

XHTML:

<h:button id="sampleButton" binding="#{backingBean.sampleButton}" value="Sample" /> 
<h:outputText value="sampleButton's client ID : #{backingBean.sampleButtonClientId}" /> 

豆:豆はあなたがすべきか、あなたのコンポーネントを結合していることを

private UIComponent sampleButton; 

public UIComponent getSampleButton() { 
    return sampleButton; 
} 

public void setSampleButton(final UIComponent sampleButton) { 
    this.sampleButton = sampleButton; 
} 

public String getSampleButtonClientId() { 
    final FacesContext context = FacesContext.getCurrentInstance(); 
    return sampleButton.getClientId(context); 
} 

お知らせだから、バッキングBeanとものclientIdを返す別のメソッドへの結合を追加範囲が要求されている場合はjava.lang.IllegalStateException (duplicate Id for a component)this topicと比較して)になる可能性があります。

関連する問題