【问题标题】:My custom jsp tag library throws an error HTTP Status 500 - Unable to compile class for JSP我的自定义 jsp 标记库抛出错误 HTTP Status 500 - Unable to compile class for JSP
【发布时间】:2017-06-03 01:28:14
【问题描述】:

我的自定义 jsp 标记库抛出错误 HTTP 状态 500 - 无法为 JSP 编译类,如果我刷新同一页面,它将出现新的错误 HTTP 状态 500 - java.lang.ClassNotFoundException: org.apache .jsp.jspstn_jsp 。我已经厌倦了从 jsp 中删除我的自定义 tld 的 taglib 指令和标记,而不是正常工作。我不知道我的代码出了什么问题。

jsp文件:

<%@page  contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib  uri="/WEB-INF/tlds/ct.tld" prefix="cp"  %>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>
    <h1>Hello World! jspstn</h1>
    <cp:tp></cp:tp>
</body>

自定义 tld 文件:

 <?xml version="1.0" encoding="UTF-8"?>
 <taglib version="2.1" 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-jsptaglibrary_2_1.xsd">
 <tlib-version>1.0</tlib-version>
  <short-name>ct</short-name>
  <uri>/WEB-INF/tlds/ct</uri>
   <tag>
  <name>tp</name>
  <body-content>empty</body-content>
   <tag-class>ctc</tag-class>
     </tag>
     </taglib>

自定义 tld 类(标签处理程序类):

import java.io.IOException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class ctc extends SimpleTagSupport {


@Override
public void doTag() throws JspException, IOException {
    JspWriter hp = getJspContext().getOut();
    hp.print("works");
}

 }

web.xml 文件:

 <?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">

<jsp-config>
    <taglib>
        <taglib-location>/WEB-INF/tlds/ct.tld</taglib-location>
        <taglib-uri>/WEB-INF/tlds/ct.tld</taglib-uri>
    </taglib>
</jsp-config>

<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>

错误:

   HTTP Status 500 - Unable to compile class for JSP:

   type Exception report

  message Unable to compile class for JSP:

  description The server encountered an internal error that prevented it from    fulfilling this request.

  exception

   org.apache.jasper.JasperException: Unable to compile class for JSP: 

    An error occurred at line: 17 in the jsp file: /jspstn.jsp
     ctc cannot be resolved to a type
        14:     </head>
           15:     <body>


            16:         <h1>Hello World! jspstn</h1>
             17:         <cp:tp></cp:tp>
              18:     </body>
               19: </html>

错误 2:

     HTTP Status 500 - java.lang.ClassNotFoundException:  org.apache.jsp.jspstn_jsp

       type Exception report

       message java.lang.ClassNotFoundException: org.apache.jsp.jspstn_jsp

     description The server encountered an internal error that prevented it from fulfilling this request.

     exception

     org.apache.jasper.JasperException: java.lang.ClassNotFoundException: org.apache.jsp.jspstn_jsp
  org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:176)
  org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:380)
 org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

  root cause

  java.lang.ClassNotFoundException: org.apache.jsp.jspstn_jsp
java.net.URLClassLoader.findClass(URLClassLoader.java:381)
org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:129)
org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:60)
   org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:171)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:380)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

Netbeans 8.2,Tomcat 8.5.8。如果在回答我的问题时需要更多信息,请发表评论。如有错别字请见谅

【问题讨论】:

  • 尝试在你的类中设置一个断点,你的标签类有一些编译问题。
  • 检查 Tomcat 8.5.8 文档后。得出的结论是标签不应该放在默认包中

标签: java jsp tomcat servlets jsp-tags


【解决方案1】:

在您的标记库描述符 (tld) 文件中,指定类的完全限定包名称

而不是

 <tag-class>ctc</tag-class>

<tag-class>your.package.ctc</tag-class>

【讨论】:

  • 标签类在默认包中,那么什么是完全限定的包名称?
猜你喜欢
  • 2017-03-29
  • 2013-03-07
  • 2015-05-14
  • 2015-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-22
相关资源
最近更新 更多