自定义 jsp 标记创建

Custom jsp tag creating

在尝试创建自定义 jsp 标签时遇到问题。我想问题出在我 jsps 路径中的某处,但无法解决。我在另一个没有自定义标签的 jsp 页面 (jsp:useBean GregorianCalendar) 上尝试了这个,它成功了。

TimeTag.java

package pack;

import java.io.IOException;
import java.util.GregorianCalendar;
import java.util.Locale;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
public class TimeTag extends TagSupport {
    @Override
    public int doStartTag() throws JspException {
        GregorianCalendar gc = new GregorianCalendar();
        String time = "<hr/>Time : <b> " + gc.getTime() + " </b><hr/>";
        String locale = "Locale : <b> " + Locale.getDefault() + " </b><hr/> ";
        try {
            JspWriter out = pageContext.getOut();
            out.write(time + locale);
        } catch (IOException e) {
            throw new JspException(e.getMessage());
        }
        return SKIP_BODY;
    }

    @Override
    public int doEndTag() throws JspException {
        return EVAL_PAGE;
    }
}

custom.tld

<?xml version="1.0" encoding="ISO-8859-1"?>

<taglib>

    <tlib-version>2.1</tlib-version>
    <short-name>ctg</short-name>
    <tag>
        <name>info-time</name>
        <tag-class>pack.TimeTag</tag-class>
        <body-content>empty</body-content>
    </tag>
</taglib>

info.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="ctg" uri="/custom.tld" %>
<html>
<head><title>Info</title>
    Time & Locale info
</head>
<body>
<ctg:info-time/>
</body>
</html>

index.jsp

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<html>
<head><title>Index</title></head>
<body>
<br/>
Time-tag
<jsp:include page="info.jsp"/>
</body>
</html>

project structure

我得到:

type Exception report

message An exception occurred processing JSP page /index.jsp at line 7

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

exception
org.apache.jasper.JasperException: An exception occurred processing JSP page /index.jsp at line 7

4: <body>
5: <br/>
6: Time-tag
7: <jsp:include page="info.jsp"/>
8: </body>
9: </html>


Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:584)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:466)
    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
org.apache.jasper.JasperException: java.lang.ClassNotFoundException: org.apache.jsp.info_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.jasper.runtime.JspRuntimeLibrary.include(JspRuntimeLibrary.java:945)
    org.apache.jsp.index_jsp._jspService(index_jsp.java:115)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:443)
    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.info_jsp
    java.net.URLClassLoader.run(URLClassLoader.java:366)
    java.net.URLClassLoader.run(URLClassLoader.java:355)
    java.security.AccessController.doPrivileged(Native Method)
    java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    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.jasper.runtime.JspRuntimeLibrary.include(JspRuntimeLibrary.java:945)
    org.apache.jsp.index_jsp._jspService(index_jsp.java:115)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:443)
    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)

我尝试 运行 您在 Tomcat 7.x 中的代码并得到以下错误:

org.apache.jasper.JasperException: Mandatory TLD element jsp-version missing or empty in TLD /custom.tld 
    org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:56)
    org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:445)
...

您可以通过在 custom.tld 中添加 jsp-version 元素来解决问题:

<?xml version="1.0" encoding="ISO-8859-1"?>
<taglib>
    <tlib-version>2.1</tlib-version>
    <jsp-version>2.1</jsp-version>
    <short-name>ctg</short-name>
    <tag>
        <name>info-time</name>
        <tag-class>pack.TimeTag</tag-class>
        <body-content>empty</body-content>
    </tag>
</taglib>