自定义JSF组件可将新的子项添加到页面"head"页面中.方面 [英] Custom JSF component which adds new child to page "head" facet

查看:98
本文介绍了自定义JSF组件可将新的子项添加到页面"head"页面中.方面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建自定义组件,将新的子项添加到页面头部"构面中.

I want to create custom component which adds new child to page "head" facet.

此自定义组件基于h:selectOneMenu.在jsf页面上使用时,用户可以简单地更改当前主题.我需要该组件执行的操作是将样式表子项添加到头部.

This custom component is based on h:selectOneMenu. When used on jsf page, user can simply change the current theme. What I need this component to do is to add stylesheet child to head facet.

我的组件具有Java支持.我试图在encodeBegins()方法中修改"head",但是我的孩子根本没有被渲染.看一下实现:

My component has backing java. I tried to modify "head" in encodeBegins() method, but my child is not rendered at all. Take a look at the implementation:



@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"构面?如果是这样,我想念什么?
问候

Is it possible to modify "head" facet directly from backing java of my custom component? If so, what am I missing?
Regards

推荐答案

UIViewRoot具有一种将资源组件添加到视图的 head 目标的方法:

UIViewRoot has a method to add a resource component to the head target of the view:

公共无效的addComponentResource(FacesContext上下文,UIComponent componentResource): 将被假定代表资源实例的componentResource添加到当前视图.资源实例由 资源渲染器(例如ScriptRenderer,StylesheetRenderer)为 在标准HTML RenderKit中进行了介绍.此方法将导致 在视图的"head"元素中呈现的资源.

public void addComponentResource(FacesContext context, UIComponent componentResource): Add componentResource, that is assumed to represent a resource instance, to the current view. A resource instance is rendered by a resource Renderer (such as ScriptRenderer, StylesheetRenderer) as described in the Standard HTML RenderKit. This method will cause the resource to be rendered in the "head" element of the view.

对于您而言,该组件是一个UIOutput,具有属性 name 和呈现类型为 javax.faces.resource.Stylesheet .

For your case, the component is an UIOutput with an attribute name and a render type of javax.faces.resource.Stylesheet.

您可以在将自定义组件添加到视图之后添加样式表资源.您进行此操作,将其注册以侦听PostAddToViewEvent. UIInput已经实现了ComponentSystemEventListener,因此您必须重写processEvent.

You can add the stylesheet resource after your custom component is added to the view. You make this, registering it for listening to PostAddToViewEvent's. The UIInput already implements ComponentSystemEventListener so you have to override processEvent.

这是添加样式表的组件的有效示例.

This is a working example of a component that adds a stylesheet.

@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);
    }

}

我想知道使用复合组件是否对您尝试的操作不容易.

I wonder if using a composite component is not easier for what you are trying to do.

这篇关于自定义JSF组件可将新的子项添加到页面"head"页面中.方面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆