我无论如何都无法在Tomcat 8下使用Eclipse Java EE IDE for Web Developers 4.4.1测试Firefox 35.0.1 Windows 8.1的JSPX项目中运行JSP自定义标签。症状是:没有错误消息,它们被简单地忽略了,并且该标签未被处理,因为用于调试的标记内的打印语句根本不执行。内置的JSTL标签可以正常工作。
经过一段时间的尝试后,我放弃了并尝试了另一个自定义标签。这也没有起作用,同样的症状。
我尝试过的事情: 我已将我的代码与书籍中预先构建的示例Web项目进行了比较 - 在该项目中,自定义标签被调用并正常执行,因此我认为这可能与项目配置有关 - 但我不知道具体原因,通过Eclipse的属性页面搜寻未显示出明显的差异。 我还比较了上述示例中的web.xml配置和.tld配置,调整其版本号和.xsd引用以确保它们相同,以防这是问题所在。 我查看了类似的问题,与Maven中Tomcat 7上的构建依赖关系有关,我专门检查了这些项目的依赖关系,并认为它们是相同的。
.tld文件(位于WEB-INF目录中)
<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.1" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_1.xsd ">
<description>Auctioneers custom tags</description>
<tlib-version>1.0</tlib-version>
<short-name>dauct</short-name>
<tag>
<description>Create a user</description>
<display-name>createUser</display-name>
<name>createUser</name>
<tag-class>derbyauct.users.UserInsertTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>user</name>
<rtexprvalue>true</rtexprvalue>
<type>derbyauct.users.UserCreation</type>
</attribute>
</tag>
<tag>
<description>Returns auctions at status ready</description>
<display-name>returnReadyAuctions</display-name>
<name>returnReadyAuctions</name>
<tag-class>derbyauct.auctions.NextAuctionTag</tag-class>
<body-content>empty</body-content>
</tag>
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/j2ee"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd"
id="WebApp_ID"
version="2.5">
<display-name>auctioneers</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description>
</description>
<display-name>AuctServlet</display-name>
<servlet-name>AuctServlet</servlet-name>
<servlet-class>auct.AuctServlet</servlet-class>
<init-param>
<param-name>base</param-name>
<param-value>/dauct</param-value>
</init-param>
<init-param>
<param-name>imageURL</param-name>
<param-value>/images/</param-value>
</init-param>
<init-param>
<param-name>jdbcDriver</param-name>
<param-value>org.mariadb.jdbc.Driver</param-value>
</init-param>
<init-param>
<param-name>dbURL</param-name>
<param-value>jdbc:mysql://localhost:3306/derbyauct</param-value>
</init-param>
<init-param>
<param-name>dbUserName</param-name>
<param-value></param-value>
</init-param>
<init-param>
<param-name>dbPassword</param-name>
<param-value></param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>AuctServlet</servlet-name>
<url-pattern>/dauct/</url-pattern>
</servlet-mapping>
</web-app>
标签处理程序
package auct.users;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import javax.servlet.ServletContext;
import auct.utils.DALManager;
import auct.users.UserCreation;
public class UserInsertTag extends TagSupport {
static final long serialVersionUID = 1L;
private UserCreation user;
public UserCreation getUser() {
return user;
}
public void setUser(UserCreation user) {
this.user = user;
System.out.println("user object :" + user);
}
public int doEndTag() throws JspException {
ServletContext context = pageContext.getServletContext();
DALManager dm = (DALManager)context.getAttribute("dalManager");
try {
dm.insertUser(user);
}
catch (Exception e) {}
return EVAL_PAGE;
}
}
JSPX file calling tag
<?xml version="1.0" encoding="ISO-8859-1" ?>
<jsp:root
xmlns:jsp="http://java.sun.com/JSP/Page"
xmlns:dauct="urn:jspltd:/WEB-INF/tlds/dauct.tld"
xmlns:c="http://java.sun.com/jsp/jstl/core"
version="2.3"
>
<jsp:directive.page
language="java"
contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" />
<jsp:text>
<![CDATA[ <?xml version="1.0" encoding="ISO-8859-1" ?> ]]>
</jsp:text>
<jsp:text>
<![CDATA[ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> ]]>
</jsp:text>
<c:url var="cssUrl" value="/css/dauct.jspx"/>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>User</title>
<link rel="stylesheet" href="${cssUrl}" type="text/css"/>
</head>
<body>
<jsp:include page="TopMenu.jspx" flush="true"/>
<jsp:include page="LeftMenu.jspx" flush="true"/>
<div class="content">
<h2>Registration Complete</h2>
<jsp:useBean id="user" class="auct.users.UserCreation" scope="request"/>
<jsp:setProperty property="*" name="user"/>
<dauct:createUser user="${user}"/>
<p class="info">
Thank you for registering.<br/>
Welcome to Auctioneers <c:out value="${user.firstName}"/>
</p>
</div>
</body>
</html>
</jsp:root>
lib文件夹
annotations-api.jar
catalina-ant.jar
catalina-ha.jar
catalina-storeconfig.jar
catalina-tribes.jar
catalina.jar
ecj-4.4.jar
el-api.jar
jasper-el.jar
jasper.jar
javax.servlet.jsp.jstl-1.2.1.jar
javax.servlet.jsp.jstl-api-1.2.1.jar
jsp-api.jar
list.txt
mariadb-java-client-1.1.8.jar
mysql-connector-java-5.1.34-bin.jar
servlet-api.jar
tomcat-api.jar
tomcat-coyote.jar
tomcat-dbcp.jar
tomcat-i18n-es.jar
tomcat-i18n-fr.jar
tomcat-i18n-ja.jar
tomcat-jdbc.jar
tomcat-jni.jar
tomcat-spdy.jar
tomcat-util-scan.jar
tomcat-util.jar
tomcat-websocket.jar
websocket-api.jar