2011-01-28 55 views
0

如何在由自定义jsf组件生成的html头标签中导入javascript和css文件?我发现了一些关于为此使用资源的文章。在自定义jsf组件的头标签中导入javascript和css

如(来源:http://technology.amis.nl/blog/6047/creating-a-custom-jsf-12-component-with-facets-resource-handling-events-and-listeners-valueexpression-and-methodexpression-attributes):

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.

回答

0

我不知道是否可以使用JSF 2一样,但复合组件更容易包括使用^ h资源:H outputStylesheet和:outputScript。当页面呈现时,资源将在html的头部。