如何在JSF 2.2中创建包含自定义标记和复合组件的干净标记库(.jar)?

时间:2015-08-14 19:47:24

标签: jsf-2.2 composite-component custom-tag

我已经阅读了有关如何在Web应用程序中创建复合组件和自定义标签的各种博客文章和stackoverflow帖子,并且已经让事情正常工作。我现在正试图将所有内容移动到可重用的JAR文件中。

mylib.jar层次结构:

src/main/java
    com.example.MyComposite.java
src/main/resources/
    META-INF
        faces-config.xml
        my.taglib.xml
    META-INF/resources/components
        myComposite.xhtml
    META-INF/tags
        myTag.xhtml

my.taglib.xml:

<facelet-taglib version="2.2"
            xmlns="http://xmlns.jcp.org/xml/ns/javaee"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facelettaglibrary_2_2.xsd">

    <namespace>http://www.example.com/components</namespace>

    <tag>
        <tag-name>myTag</tag-name>
        <source>
                tags/myTag.xhtml
        </source>
    </tag>

    <tag>
        <tag-name>myComposite</tag-name>
        <component>
            <resource-id>
                components/myComposite.xhtml
            </resource-id>
        </component>
    </tag>
</facelet-taglib>

我已经将它构建为jar并在我的web-app中使用它,自定义标记和复合组件都很好用。我花了100个不同的层次结构来让它工作。

我的问题是我不喜欢我似乎必须将自定义标签放在一个地方(/ tags)和复合组件放在另一个地方(/ resources / components)。此外,我必须引用带有标记的自定义标记和带有组件 / resource-id 标记的复合组件。例如,我尝试将myTag.xhtml放入/ resources / components并使用resource-id引用它,但是当我尝试使用它时我得到了NPE。

标签和组件必须位于不同的目录中吗?我只需要忍受这种等级制度吗?

1 个答案:

答案 0 :(得分:6)

您可以单独使用<composite-library-name>复合材料来减少复合材料不必要的<tag>样板。对于标记文件,您可以将它们放在/resources/tags中并相应地更改<source>,以便文件夹结构更加统一。

src/main/java
 `-- com/example/MyComposite.java

src/main/resources
 `-- META-INF
      |-- resources
      |    |-- components
      |    |    `-- myComposite.xhtml
      |    `-- tags
      |         `-- myTag.xhtml
      |-- faces-config.xml
      `-- my.taglib.xml
<facelet-taglib version="2.2"
            xmlns="http://xmlns.jcp.org/xml/ns/javaee"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facelettaglibrary_2_2.xsd">

    <namespace>http://www.example.com/components</namespace>
    <composite-library-name>components</composite-library-name>

    <tag>
        <tag-name>myTag</tag-name>
        <source>resources/tags/myTag.xhtml</source>
    </tag>
</facelet-taglib>

另见: