Customize ui: enable rendering to add prefix / postfix

I need to set up ui: include rendering so that when generating HTML output, a comment is also added to indicate the start and end of the included file.

Example, suppose the space is file.xhtml

:

Input

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

      

Output

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

      

At the moment I am using JSF2.2 with MyFaces, any idea on how I could do this?

+3


source to share


3 answers


I would suggest defining the following facelet tag:

<?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>

      



and use this tag instead of ui: include in code:

<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>

      

+1


source


ui: include is not UiComponent

and does not have a renderer. This is a Facelet TagHandler

, and therefore is executed when the view is built (or restored). You will need to modify this TagHandler

one to include additional instances ELInstruction

with the desired comments in the component tree.



I don't think JSF offers any good extension point to override an existing tag library's tag handler. You can define a new tag in your own tag library. You can try to replace the existing tag library definition, although I'm not sure if this is possible for built-in libraries. Or, you can hide the class definition of the original label handler in the classpath by providing your own definition for that class (which you get by copying and modifying the original source code). All of these approaches require duplication of framework code and will therefore be fragile to maintain.

+2


source


Customization is also possible with the TagDecorator element , but it helps a little to achieve your goal.

But be careful: The solution only works if <ui:include>

not <ui:param>

.

The following four TODOs are required:

  • Define tag tag inside your 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>

      

  1. Create xhtml for your wrapper 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>

      

  1. Define your TagDecorator in your web.xml file like this:
<context-param>
    <param-name>javax.faces.FACELETS_DECORATORS</param-name>
    <param-value>my.package.CommentTagDecorator</param-value>
</context-param>

      

  1. Create your TagDecorator and fill it with life. (Taken from 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());
    }
}

      

Finally, your result will look like

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

      

0


source







All Articles