【问题标题】:Custom Facelet component in JSFJSF 中的自定义 Facelet 组件
【发布时间】:2013-02-07 01:17:58
【问题描述】:

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

提前致谢,

考沙尔

【问题讨论】:

    标签: jsf jsf-2 facelets


    【解决方案1】:

    本质上是标签处理程序。 IE。从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 />
    

    要查看 &lt;ui:composition&gt;&lt;ui:include&gt; 的 Mojarra 实现源代码,请单击链接。

    另见:

    【讨论】:

    • 感谢您的示例。这确实很有帮助。我必须说,您对 JSF 的回答非常有价值。
    • 我正在使用这种策略,我注意到在回发时,创建的组件被移动,成为父组件的第一个子组件。令人沮丧...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-02
    • 2010-10-13
    • 1970-01-01
    • 2011-11-27
    • 2011-06-30
    相关资源
    最近更新 更多