【问题标题】:Problem creating my own custom tags(JSF 2.0)创建我自己的自定义标签时出现问题(JSF 2.0)
【发布时间】:2011-10-26 08:21:47
【问题描述】:

我正在学习创建自己的自定义标签,但我遇到了一些麻烦,我无法让这个简单的应用程序使用我创建的标签。我认为我做的一切都很好,但我担心我创建的新库的路径是错误的。也许有人可以帮助我找到我的错误所在并了解其原因。这是我到目前为止所做的:

1- 我将标签创建为 xhtml 块(mybutton.xhtml)

<!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:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core">

<ui:composition>
    <h:commandButton type="submit" value="#{buttonSubmitLabel}" />
    <h:commandButton type="reset" value="#{buttonResetLabel}" />
</ui:composition>
</html>

2- 然后我创建了一个 .xml 文件,该文件将充当所有自定义标签都被索引的库(mytagsconfig.taglib.xml)

 <?xml version="1.0"?>
<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://mytags.com/facelets</namespace>
    <tag>
        <tag-name>mybutton</tag-name>
        <source>mytags/mybutton.xhtml</source>
    </tag>
</facelet-taglib>

3- 我尝试在 web.xml 中注册我的新库,以便我可以使用它

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>CHAPTER 5 Creating your own Custom tags</display-name>
    <welcome-file-list>
        <welcome-file>index.xhtml</welcome-file>
    </welcome-file-list>
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>

    <!-- REGISTERING A CUSTOM TAG INTO JSF APPLICATION -->
    <context-param>
    <param-name>javax.faces.FACELETS_LIBRARIES</param-name>
    <param-value>/WEB-INF/mytagsconfig.taglib.xml</param-value>
</context-param>
</web-app>

4- 最后我尝试在某些页面中使用标签(在我的情况下,在插入模板的组件中)

 <!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:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:mytags="http://mytags.com/facelets">

<ui:composition template="WEB-INF/templates/masterLayout.xhtml">
    <ui:define name="pagetitle">
    Defining custom tags
    </ui:define>
    <ui:define name="content">
    Defining custom tags is a 3 step process:
    <ul>
        <li>Use ui:compisition to create some content.(Custom tags are stored in WEB-INF/customtags)</li>
        <li>Declares the custom tag in a tag library descriptor into the WEB-INF folder(Example: mycustomtags.taglib.xml).</li>
        <li>Register the tag library descriptor in the web.xml.</li>        
    </ul>

    <!-- Here should go a call to the new created tag -->
    <mytags:mybutton buttonSubmitLabel="Submit" buttonResetLabel="Reset" /> 

    </ui:define>    
</ui:composition>

</html>

这是我的文件夹结构:

****更新**** 当我构建时,我看到 index.xhtml 页面,但自定义标签不存在(我没有看到 2 个按钮)

【问题讨论】:

  • @BalusC 我运行程序并在浏览器上看到 404 错误,控制台显示:WARNING: StandardWrapperValve[Faces Servlet]: PWC1406: Servlet.service() for servlet Faces Servlet threw exception javax.faces.view.facelets.TagAttributeException: /WEB-INF/templates/masterLayout.xhtml @33,66 &lt;ui:include src="WEB-INF/templates/defaultContent.xhtml"&gt; Invalid path : WEB-INF/templates/defaultContent.xhtml 就像模板有问题,但不知道是什么。我将粘贴上面使用的模板。
  • 仅供参考:上述错误只是脏工作区/项目/部署的结果。 OP 在对我的已删除答案的评论中提到,它在清理/重建后消失了。

标签: java jsf jakarta-ee jsf-2 custom-controls


【解决方案1】:

web.xml 中的 taglib 声明没有指向正确的文件名。

您说您创建了一个/WEB-INF/mytagsconfig.taglib.xml,但您在web.xml 中将其声明为/WEB-INF/mytags.taglib.xml。相应地修复它。

与问题没有直接关系,但考虑升级到与 JSF/Facelets 2.0 兼容的 taglib 根声明和 web.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">
    <!-- Taglib config here. -->
</facelet-taglib>

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

【讨论】:

  • 我按照您的建议升级到 facelets2.0,并将参数名从 faces.LIBRARIES 更改为 faces.FACELETS_LIBRARIES 现在我在浏览器中看到了我的新自定义标签。一切正常。很好,现在我知道如何创建自己的标签了 :) 谢谢 BalusC
  • 不客气。您可能会发现此相关问题/答案也很有用:stackoverflow.com/questions/6822000/…
  • 酷,这是一个非常有用的:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-07-08
  • 2011-11-15
  • 2011-10-11
  • 2014-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多