自定义jsp标签创建

时间:2016-07-23 03:10:51

标签: java jsp tomcat servlets jsp-tags

面临尝试创建自定义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$1.run(URLClassLoader.java:366)
    java.net.URLClassLoader$1.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)

1 个答案:

答案 0 :(得分:0)

我尝试在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)
...

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

<?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>