【发布时间】:2011-01-28 13:58:34
【问题描述】:
如何在自定义jsf组件生成的html的head标签中导入javascript和css文件?我发现了一些关于为此目的使用资源的文章。
protected void writeScriptResource(
FacesContext context,
String resourcePath) throws IOException
{
Set scriptResources = _getScriptResourcesAlreadyWritten(context);
// Set.add() returns true only if item was added to the set
// and returns false if item was already present in the set
if (scriptResources.add(resourcePath))
{
ViewHandler handler = context.getApplication().getViewHandler();
String resourceURL = handler.getResourceURL(context, SCRIPT_PATH +resourcePath);
ResponseWriter out = context.getResponseWriter();
out.startElement("script", null);
out.writeAttribute("type", "text/javascript", null);
out.writeAttribute("src", resourceURL, null);
out.endElement("script");
}
}
private Set _getScriptResourcesAlreadyWritten(
FacesContext context)
{
ExternalContext external = context.getExternalContext();
Map requestScope = external.getRequestMap();
Set written = (Set)requestScope.get(_SCRIPT_RESOURCES_KEY);
if (written == null)
{
written = new HashSet();
requestScope.put(_SCRIPT_RESOURCES_KEY, written);
}
return written;
}
static private final String _SCRIPT_RESOURCES_KEY =
ShufflerRenderer.class.getName() + ".SCRIPTS_WRITTEN";
但是除了写在头标签之外,导入链接也写在我的组件代码中。 writeScriptResource() 方法在 encodeBegin 方法的开头被调用。 有谁知道如何做到这一点以避免在我的页面中注入内联脚本和样式?
提前致谢, 保罗·S.
【问题讨论】:
标签: jsf resources custom-component