UIViewRoot有一个方法将资源组件添加到
头
视图目标:
public void addComponentResource(FacesContext上下文,UIComponent componentResource):
将假定表示资源实例的componentResource添加到当前视图中。资源实例由
资源呈现器(如ScriptRenderer、StylesheetRenderer)为
在标准的HTML RenderKit中描述。这种方法将导致
要在视图的head元素中呈现的资源。
对于您的情况,组件是一个带有属性的UIOutput
名称
以及渲染类型
javax.faces.resource.Stylesheet
.
将自定义组件添加到视图后,可以添加样式表资源。这样做是为了注册它来监听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);
}
}
我想知道对于您正在尝试的工作来说,使用复合组件是否并不容易。