如何将自定义facelet标记attr传递给JSF 2中的实现

时间:2013-06-06 02:49:33

标签: jsf-2

我有下一个:

/src/main/webapp/WEB-INF/web.xml

<?xml version="1.0" encoding="utf-8"?>

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

/src/main/webapp/WEB-INF/my-tablib.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/jsf/taglib</namespace>
    <tag>
        <tag-name>my-tag</tag-name>
        <!-- /src/main/webapp/WEB-INF/tags/com/example/myTag.xml -->
        <source>tags/com/example/myTag.xml</source>
    </tag>
</facelet-taglib>

/src/main/webapp/WEB-INF/tags/com/example/myTag.xml

<!DOCTYPE html
        PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:composite="http://java.sun.com/jsf/composite">

    <composite:interface>
        <composite:attribute name="customAttr" />
    </composite:interface>

    <composite:implementation>
        <h:outputText value="#{cc.attrs.customAttr}" />
    </composite:implementation>
</html>

/src/main/webapp/index.xhtml

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:exui="http://example.com/jsf/taglib">
    <h:head>
    </h:head>
    <h:body>
        <exui:my-tag customAttr="showMe" />
    </h:body>
</html>

结果,在部署战争后我没有看到“showMe”消息。如何解决?

1 个答案:

答案 0 :(得分:0)

您将复合组件放在/resources/<composite name>文件夹中(无需将my-tablib.xml添加到web.xml), 例如:

enter image description here

然后你可以使用declare:

    <html xmlns="http://www.w3.org/1999/xhtml"
          ...
          xmlns:cps="http://java.sun.com/jsf/composite/composites/CPSI35"
          >
          ...
          <cps:cpscontact ....></cps:cpscontact>