簡體   English   中英

創建我自己的自定義標簽時出現問題(JSF 2.0)

[英]Problem creating my own custom tags(JSF 2.0)

我正在學習創建自己的自定義標簽,但我遇到了一些麻煩,我無法讓這個簡單的應用程序使用我創建的標簽。 我認為我做的一切都很好,但我擔心我創建的新庫的路徑是錯誤的。 也許有人可以幫助我找到我的錯誤所在並了解其原因。 這是我到目前為止所做的:

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 個按鈕)

您在web.xml中的 taglib 聲明沒有指向正確的文件名。

You said that you've created a /WEB-INF/mytagsconfig.taglib.xml , but you've declared it in web.xml as /WEB-INF/mytags.taglib.xml instead. 相應地修復它。

與問題沒有直接關系,但考慮升級到與 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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM