JSF中的自定义Facelet组件

Kau*_*hal 4 jsf facelets jsf-2

是否可以创建自定义JSF核心Facelet组件.喜欢的东西<custom:composition><ui:composition>,或者<custom:include>用于<ui:include> 这将是有益的,如果有人能告诉我涉及的步骤.

提前致谢,

Kaushal

Bal*_*usC 16

它实质上是标签处理程序.即延伸的类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.
    }

}
Run Code Online (Sandbox Code Playgroud)

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

/WEB-INF/web.xml(注意:当my.taglib.xml/META-INFJAR文件的文件夹中时,这部分不是必需的,/WEB-INF/lib就像JSF组件库一样):

<context-param>
    <param-name>javax.faces.FACELETS_LIBRARIES</param-name>
    <param-value>/WEB-INF/my.taglib.xml</param-value>
</context-param>
Run Code Online (Sandbox Code Playgroud)

用法 /some.xhtml

<html ... xmlns:my="http://example.com/my">
...
<my:hello />
Run Code Online (Sandbox Code Playgroud)

要查看钻嘴鱼科执行的源代码<ui:composition>,并<ui:include>单击链接.

也可以看看: