3

ページの「head」ファセットに新しい子を追加するカスタム コンポーネントを作成したいと考えています。

このカスタム コンポーネントは、h:selectOneMenu に基づいています。jsf ページで使用すると、ユーザーは現在のテーマを簡単に変更できます。このコンポーネントで行う必要があるのは、スタイルシートの子を head ファセットに追加することです。

私のコンポーネントにはバッキング Java があります。encodeBegins() メソッドで「頭」を変更しようとしましたが、子供がまったくレンダリングされません。実装を見てみましょう:



@FacesComponent(value = "com.ramps.util.ThemeSelector")
public class ThemeSelector extends UIInput implements NamingContainer {

 public void encodeBegin(FacesContext context) throws IOException {

   UIComponent headFacet = context.getViewRoot().getFacet("javax_faces_location_HEAD");     
   Resource res = new Resource();       
   res.setName("...");
   ...
   List <UIComponent> headChildren = headFacet.getChildren();
   headChildren.add(res);

   super.encodeBegin(context);
  }
 }

カスタム コンポーネントのバッキング Java から直接「head」ファセットを変更することは可能ですか? もしそうなら、私は何が欠けていますか?
よろしく

4

1 に答える 1

4

UIViewRoot には、リソース コンポーネントをビューのヘッドターゲットに追加するメソッドがあります。

public void addComponentResource(FacesContext context, UIComponent componentResource): リソース インスタンスを表すと想定される componentResource を現在のビューに追加します。リソース インスタンスは、標準 HTML RenderKit で説明されているように、リソース レンダラー (ScriptRenderer、StylesheetRenderer など) によってレンダリングされます。このメソッドにより、リソースがビューの「head」要素にレンダリングされます。

あなたの場合、コンポーネントは属性名とjavax.faces.resource.Stylesheetのレンダリングタイプを持つ UIOutputです。

カスタム コンポーネントをビューに追加した後で、スタイルシート リソースを追加できます。これを作成し、PostAddToViewEvent をリッスンするために登録します。UIInput はすでに ComponentSystemEventListener を実装しているため、processEvent をオーバーライドする必要があります。

これは、スタイルシートを追加するコンポーネントの実例です。

@FacesComponent("CustomComponent")
@ListenerFor(systemEventClass=PostAddToViewEvent.class)
public class CustomComponent extends UIInput{

    @Override
    public void processEvent(ComponentSystemEvent event) throws AbortProcessingException {
        if(event instanceof PostAddToViewEvent){
            UIOutput resource=new UIOutput();
            resource.getAttributes().put("name", "theme.css");
            resource.setRendererType("javax.faces.resource.Stylesheet");
            FacesContext.getCurrentInstance().getViewRoot().addComponentResource(FacesContext.getCurrentInstance(), resource);
        }
        super.processEvent(event);
    }

}

あなたがやろうとしていることに対して、複合コンポーネントを使用するのは簡単ではないのでしょうか。

于 2011-02-07T09:46:47.577 に答える