如何创建自定义Facelets标签? [英] How to create a custom Facelets tag?
问题描述
我正在使用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本身已经是 的taglib.请仔细阅读我们的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
中,如下所示:
and register it in /WEB-INF/example.taglib.xml
as follows:
<?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
中注册,如下所示:
which is in turn registered in /WEB-INF/web.xml
as follows:
<context-param>
<param-name>javax.faces.FACELETS_LIBRARIES</param-name>
<param-value>/WEB-INF/example.taglib.xml</param-value>
</context-param>
(请注意,当*.taglib.xml
文件位于/WEB-INF/lib
中的JAR的/META-INF
文件夹中时,无需在web.xml
中进行注册)
(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 />
另请参见:
- 何时使用< ui:include> ;、标记文件,复合组件和/或自定义组件?
- 具有共享代码的多个JSF项目的结构
- When to use <ui:include>, tag files, composite components and/or custom components?
- Structure for multiple JSF projects with shared code
See also:
这篇关于如何创建自定义Facelets标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!