我正在学习创建自己的自定义标签,但我遇到了一些麻烦,我不能让这个简单的应用程序使用我创建的标签。我认为我做的一切都很好,但我担心我创建的新库的路径是错误的。也许有人可以帮助我找到我的错误所在并理解它的原因。这就是我到目前为止所做的:
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个按钮)
答案 0 :(得分: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>