解决方法@H_404_4@
这取决于你想要声明资源的确切位置.通常,以编程方式声明它们的唯一原因是您有一个自定义
@L_403_0@或
Renderer
,它生成HTML代码,而HTML代码又需要这些JS和/或CSS资源.然后在
@ResourceDependency
或
@ResourceDependencies
宣布它们.
@ResourceDependency(library="mylibrary",name="foo.css")
public class FooComponentWithCSS extends UIComponentBase {
// ...
}
@ResourceDependencies({
@ResourceDependency(library="mylibrary",name="bar.css"),@ResourceDependency(library="mylibrary",name="bar.js")
})
public class BarComponentWithCSSandJS extends UIComponentBase {
// ...
}
但是如果你真的需要在其他地方声明它们,比如在渲染响应之前调用的支持bean方法(否则它太晚了),那么你可以在UIViewRoot#addComponentResource()
之前完成.组件资源必须创建为UIOutput
,具有javax.faces.resource.Script或javax.faces.resource.Stylesheet的渲染器类型,用于表示完整的< h:outputScript>或< h:outputStylesheet>分别.库和名称属性只能放在属性映射中.
UIoUtput css = new UIoUtput();
css.setRendererType("javax.faces.resource.Stylesheet");
css.getAttributes().put("library","mylibrary");
css.getAttributes().put("name","bar.css");
UIoUtput js = new UIoUtput();
js.setRendererType("javax.faces.resource.Script");
js.getAttributes().put("library","mylibrary");
js.getAttributes().put("name","bar.js");
FacesContext context = FacesContext.getCurrentInstance();
context.getViewRoot().addComponentResource(context,css,"head");
context.getViewRoot().addComponentResource(context,js,"head");
Renderer
,它生成HTML代码,而HTML代码又需要这些JS和/或CSS资源.然后在
@ResourceDependency
或
@ResourceDependencies
宣布它们.
@ResourceDependency(library="mylibrary",name="foo.css") public class FooComponentWithCSS extends UIComponentBase { // ... }
@ResourceDependencies({ @ResourceDependency(library="mylibrary",name="bar.css"),@ResourceDependency(library="mylibrary",name="bar.js") }) public class BarComponentWithCSSandJS extends UIComponentBase { // ... }
但是如果你真的需要在其他地方声明它们,比如在渲染响应之前调用的支持bean方法(否则它太晚了),那么你可以在UIViewRoot#addComponentResource()
之前完成.组件资源必须创建为UIOutput
,具有javax.faces.resource.Script或javax.faces.resource.Stylesheet的渲染器类型,用于表示完整的< h:outputScript>或< h:outputStylesheet>分别.库和名称属性只能放在属性映射中.
UIoUtput css = new UIoUtput(); css.setRendererType("javax.faces.resource.Stylesheet"); css.getAttributes().put("library","mylibrary"); css.getAttributes().put("name","bar.css"); UIoUtput js = new UIoUtput(); js.setRendererType("javax.faces.resource.Script"); js.getAttributes().put("library","mylibrary"); js.getAttributes().put("name","bar.js"); FacesContext context = FacesContext.getCurrentInstance(); context.getViewRoot().addComponentResource(context,css,"head"); context.getViewRoot().addComponentResource(context,js,"head");