JSF中的自定义Facelet组件 [英] Custom Facelet component in JSF

查看:103
本文介绍了JSF中的自定义Facelet组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以创建自定义JSF核心Facelet组件. <ui:composition><custom:composition><ui:include><custom:include>之类的东西 如果有人可以告诉我其中涉及的步骤,将会很有帮助.

Is it possible to create a custom JSF core Facelet component. Something like <custom:composition> of <ui:composition>, or <custom:include> for <ui:include> It would be helpful if someone can tell me the steps involved.

预先感谢

考沙尔

推荐答案

它本质上是标记处理程序. IE.从 TagHandler 扩展的类

It are in essence taghandlers. I.e. classes extending from TagHandler.

这是Hello World标记处理程序.

Here's a Hello World taghandler.

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组件库一样,此部分不是必需的):

/WEB-INF/web.xml (note: this part is not mandatory when my.taglib.xml is in /META-INF folder of a JAR file inside /WEB-INF/lib like as with JSF component libraries):

<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 />

查看

To see the source code of Mojarra implementation of <ui:composition> and <ui:include>, click the links.

这篇关于JSF中的自定义Facelet组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆