2011-08-13 126 views
0

我正在学习创建自己的自定义标记,但我遇到了一些麻烦,我无法使这个简单的应用程序使用我创建的标记。我认为我做得很好,但我担心我创建的新图书馆的路径是错误的。也许有人可以帮助我找到我的错误,并理解它的原因。这是我做了什么至今:创建我自己的自定义标记(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> 

这是我的文件夹结构:

enter image description here

**** ****更新时 我建立我看到的index.xhtml页,但C ustom标签不存在(我没有看到2个按钮)

+0

@BalusC我运行该程序,并在浏览器中,我看到404错误,并在控制台说:'警告:StandardWrapperValve [面临的Servlet]:PWC1406:Servlet.service() for servlet Faces Servlet抛出异常 javax.faces.view.facelets.TagAttributeException:/WEB-INF/templates/masterLayout.xhtml @ 33,66 无效路径:WEB-INF/templates/defaultContent.xhtml“这就好像模板出了问题,但不知道是什么。我将粘贴上面使用的模板。 – sfrj

+0

仅供参考:上述错误只是脏工作区/项目/部署的结果。 OP在我的一个删除答案的评论中提到它在清理/重建后已经消失。 – BalusC

回答

6

您在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> 
+0

我按照你的建议升级到了facelets2.0,并将paramname从'faces.LIBRARIES'更改为'faces.FACELETS_LIBRARIES'现在我在浏览器中看到了我的新自定义标签。 Everithing正常工作。尼斯现在我知道如何创建我自己的标签:)感谢BalusC – sfrj

+0

不客气。你也可以找到这个相关的问题/答案:http:// stackoverflow。com/questions/6822000/when-to-use-uiinclude-tag-files-composite-components-and-or-custom-component – BalusC

+0

很酷,那真是一个非常有用的:) – sfrj