是否可以创建自定义JSF核心Facelet组件。类似<custom:composition>
<ui:composition>
或<custom:include>
<ui:include>
如果有人能告诉我所涉及的步骤,将会很有帮助。
提前致谢,
Kaushal
答案 0 :(得分:16)
它本质上是taghandlers。即从TagHandler
延伸的课程。
这是一个Hello World标记处理程序。
com.example.HelloTagHandler
public class HelloTagHandler extends TagHandler {
public HelloTagHandler(TagConfig config) {
super(config);
}
@Override
public void apply(FaceletContext context, UIComponent parent) throws IOException {
// Do your job here. This example dynamically adds another component to the parent.
if (ComponentHandler.isNew(parent)) {
UIOutput child = new HtmlOutputText();
child.setValue("Hello World");
parent.getChildren().add(child);
}
nextHandler.apply(context, parent); // Delegate job further to first next tag in tree hierarchy.
}
}
/WEB-INF/my.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/my</namespace>
<tag>
<tag-name>hello</tag-name>
<handler-class>com.example.HelloTagHandler</handler-class>
</tag>
</facelet-taglib>
/WEB-INF/web.xml
(注意:my.taglib.xml
位于/META-INF
内的/WEB-INF/lib
文件夹的<context-param>
<param-name>javax.faces.FACELETS_LIBRARIES</param-name>
<param-value>/WEB-INF/my.taglib.xml</param-value>
</context-param>
文件夹中,与JSF组件库一样,此部分不是必需的:
/some.xhtml
<html ... xmlns:my="http://example.com/my">
...
<my:hello />
{{1}}
要查看<ui:composition>
和<ui:include>
的Mojarra实施的源代码,请点击链接。