【发布时间】: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 <ui:include src="WEB-INF/templates/defaultContent.xhtml"> Invalid path : WEB-INF/templates/defaultContent.xhtml就像模板有问题,但不知道是什么。我将粘贴上面使用的模板。 -
仅供参考:上述错误只是脏工作区/项目/部署的结果。 OP 在对我的已删除答案的评论中提到,它在清理/重建后消失了。
标签: java jsf jakarta-ee jsf-2 custom-controls