2015-05-21 81 views
3

我需要以一种方式自定义ui:include渲染器,当它生成HTML输出时还会添加一条注释,说明包含文件的开始和结尾。自定义UI:包括渲染以添加前缀/后缀

例如,假设一个空白file.xhtml

输入

<ui:include src="file.xhtml" /> 

输出

<!-- START file.xhtml --> 
<!-- END file.xhtml --> 

目前我使用的是MyFaces的JSF2.2,任何想法我该如何做到这一点?

+0

产品/重复:http://stackoverflow.com/q/33263233 – BalusC

回答

1

我建议定义以下Facelet标记:

<?xml version='1.0' encoding='UTF-8' ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
     xmlns:c="http://java.sun.com/jsp/jstl/core" 
     xmlns:ui="http://java.sun.com/jsf/facelets" 
     xmlns:h="http://java.sun.com/jsf/html" 
      > 
<ui:composition> 
    <h:outputText value="&lt;START FILE!--#{source}--&gt;"/> 
    <ui:include src="#{source}"> 
    <ui:insert name="includeParams"/> 
    </ui:include> 
    <h:outputText value="&lt;!--END FILE!--#{source}--&gt;"/> 
</ui:composition> 
</html> 

,并使用这个标签,而不是用户界面:在代码中包括:

<my:include source="file.xhtml"> 
    <ui:define name="includeParams"> 
    <ui:param name="param1" value="value1"/>  
    <ui:param name="param2" value="value2"/>  
    </ui:define> 
</my:include> 
+0

我已经试过了,但似乎我不能将它用于内部UI:param,PARAMS被忽略。 – Fabio

2

ui:include不是UiComponent,并且没有渲染器。它是一个Facelet TagHandler,因此在构建(或恢复)视图时执行。您必须修改该TagHandler以将其他ELInstruction实例与期望的注释一起包含到组件树中。

我不认为JSF提供任何漂亮的扩展点来覆盖现有标记库的标记处理程序。您可以在自己的标签库中定义一个新标签。您可以尝试替换现有的标记库定义,但我不确定这是否可以用于内置库。或者,您可以通过为该类提供自己的定义(您可以通过复制和修改原始源代码获取该定义)来隐藏类路径中原始标记处理程序的类定义。所有这些方法都需要重复框架代码,因此在维护时会变得很脆弱。

0

定制,也可以通过TagDecorator元素,但这是实现你的目标的一些努力。

但要小心:该解决方案仅适用于<ui:include>内部没有<ui:param>的情况。

以下四个待办事项是必需的:

  1. 定义您taglib.xml

<namespace>http://my-namespace.com/tags/my-tags</namespace> 

<tag> 
    <tag-name>includeWithComment</tag-name> 
    <source>my/package/includeWithComment.xhtml</source> 

    <attribute> 
     <name>src</name> 
    </attribute> 
</tag> 
内部的包装标签
  • 为您的包装创建xhtml includeWithComment。XHTML
  • <?xml version="1.0" encoding="UTF-8"?> 
    <!DOCTYPE html> 
    <html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets"> 
    <ui:composition> 
    
        <!-- START ${src} --> 
        <ui:include src="${src}" /> 
        <!-- END ${src} --> 
    
    </ui:composition> 
    </html> 
    
  • 定义您TagDecorator在的web.xml这样的:
  • <context-param> 
        <param-name>javax.faces.FACELETS_DECORATORS</param-name> 
        <param-value>my.package.CommentTagDecorator</param-value> 
    </context-param> 
    
  • 创建您的TagDecorator并填入生活。 (来自this blogpost两者)​​
  • package my.package; 
    
    public class CommentTagDecorator implements TagDecorator { 
    
        @Override 
        public Tag decorate(Tag tag) { 
         if (!tag.getNamespace().equals("http://xmlns.jcp.org/jsf/facelets") 
          || !tag.getLocalName().equals("include") 
         ) { 
          return null; 
         } 
    
         return new Tag(tag.getLocation(), "http://my-namespace.com/tags/my-tags", 
          "includeWithComment", "t:includeWithComment", tag.getAttributes()); 
        } 
    } 
    

    最后的输出会像

    <!-- START /include/popup.xhtml --> 
    [...] 
    <!-- END /include/popup.xhtml -->