栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何以编程方式向其中添加JS和CSS资源 ?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何以编程方式向其中添加JS和CSS资源 ?

这取决于您到底想在哪里声明资源。 通常
,以编程方式声明它们的唯一原因是您拥有一个自定义

UIComponent
Renderer
生成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 {    // ...}

但是,如果您 确实 需要在其他地方声明它们,例如 渲染响应 之前 调用的backing
bean方法(否则为时已​​晚),则可以使用

UIViewRoot#addComponentResource()
。必须将组件资源创建为
UIOutput
呈现器类型为
javax.faces.resource.script
javax.faces.resource.Stylesheet
,以分别表示完全值
<h:outputscript>
<h:outputStylesheet>
。该
library
name
属性正好可以放在属性地图。

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");


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/647677.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号