如何创建自定义 Facelets 标签? [英] How to create a custom Facelets tag?

查看:31
本文介绍了如何创建自定义 Facelets 标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 JSF 2.0.我使用标记文件创建了自定义 JSTL 标记,并且在 JSP 中运行良好.但我也想在 Facelets 中使用自定义 JSTL 标签.是否可以在 Facelets 中创建标记文件?

I am using JSF 2.0. I created custom JSTL tags with tagfiles and that were working fine in JSP. But I want to use custom JSTL tags in Facelets too. Is it possible to create tagfiles in Facelets or not?

推荐答案

自定义 JSTL 标记"老实说毫无意义.这个词完全没有意义.JSTL 本身已经是一个标签库.请仔细阅读我们的 JSTL wiki 页面的介绍性段落,以了解 JSTL 的真正含义.您可能实际上是指自定义 JSP 标记".当然,它们不会在 Facelets 中工作,因为它是一种与 JSP 完全不同的视图技术,实际上是已弃用的 JSP 的继承者.

"Custom JSTL tags" makes honestly no sense. This term is utterly meaningless. JSTL is already a taglib at its own. Please carefully read the introductory paragraphs of our JSTL wiki page to learn what JSTL really is. You perhaps actually meant "Custom JSP tags". Of course they would not work in Facelets as that's a completely different view technology than JSP and actually the successor of the deprecated JSP.

嗯,自定义 JSP 标记"的类比是自定义 Facelets 标记",或者更常见的Facelets 标记文件".它相当简单,您可以遵循与包含文件相同的语法.

Well, the analogy of a "Custom JSP tag" is a "Custom Facelets tag", or more commonly "Facelets tag file". It's rather simple, you can follow the same syntax as an include file.

/WEB-INF/tags/some.xhtml:

<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets">
    Hello World
    ...
    <ui:insert /> <!-- This inserts tag body, if necessary. -->
</ui:composition>

并在/WEB-INF/example.taglib.xml中注册如下:

<?xml version="1.0" encoding="UTF-8"?>
<facelet-taglib 
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facelettaglibrary_2_0.xsd"
    version="2.0">
    <namespace>http://example.com/jsf/facelets</namespace>
    <tag>
        <tag-name>some</tag-name>
        <source>tags/some.xhtml</source>
    </tag>
</facelet-taglib>

依次在/WEB-INF/web.xml中注册如下:

<context-param>
    <param-name>javax.faces.FACELETS_LIBRARIES</param-name>
    <param-value>/WEB-INF/example.taglib.xml</param-value>
</context-param>

(注意,*.taglib.xml 文件在 /META-INF 中时,不需要在 web.xml 中注册> /WEB-INF/lib 中的 JAR 文件夹)

(note that registration in web.xml is unnecessary when the *.taglib.xml file is in /META-INF folder of a JAR in /WEB-INF/lib)

最后在您的模板中使用它,如下所示:

and finally use it in your templates as follows:

<html ... xmlns:my="http://example.com/jsf/facelets">
...
<my:some />

另见:

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆