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

wsa*_*ton 4 custom-tag composite-component jsf-2.2

我已经阅读了关于如何在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
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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

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

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

Bal*_*usC 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
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)

也可以看看:

  • 我同意我们应该通过引入更多的约定/隐含来更容易地开发标记文件,这将进一步减少XML.正是出于这个原因,复合材料对标签文件的滥用太多了. (2认同)