jsf2-jsf中的自定义Facelet组件



是否可以创建一个自定义JSF核心Facelet组件。类似于<ui:composition><custom:composition>,或<ui:include><custom:include>如果有人能告诉我所涉及的步骤会很有帮助。

提前感谢

Kaushal

它本质上是标签处理程序。即从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位于/WEB-INF/lib内JAR文件的/META-INF文件夹中时,这部分不是强制性的,就像JSF组件库一样):

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

/some.xhtml 的使用

<html ... xmlns:my="http://example.com/my">
...
<my:hello />

要查看<ui:composition><ui:include>的Mojarra实现的源代码,请单击链接。

另请参阅:

  • 何时使用<ui:include>,标签文件、复合组件和/或自定义组件

最新更新