Java 运行经典标记处理程序时出现问题
我想通过扩展类BodyTagSupport以自定义标记名Simple从标记处理程序中显示JSP的主体值,但得到一个运行时异常;o) JSP代码是:Java 运行经典标记处理程序时出现问题,java,jsp,tomcat,tags,Java,Jsp,Tomcat,Tags,我想通过扩展类BodyTagSupport以自定义标记名Simple从标记处理程序中显示JSP的主体值,但得到一个运行时异常;o) JSP代码是: <html><body> <%@ taglib prefix="mine" uri="simpleTags" %> Advisor page <mine:simple> Balle Balle </mine:simple> </html></body> 我得到的例
<html><body>
<%@ taglib prefix="mine" uri="simpleTags" %>
Advisor page
<mine:simple>
Balle Balle
</mine:simple>
</html></body>
我得到的例外是:
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: foo.SelectTagHandler.setJspContext(Ljavax/servlet/jsp/JspContext;)V
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:460)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:355)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
root cause
javax.servlet.ServletException: foo.SelectTagHandler.setJspContext(Ljavax/servlet/jsp/JspContext;)V
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:841)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:774)
org.apache.jsp.new_jsp._jspService(new_jsp.java:60)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
root cause
java.lang.NoSuchMethodError: foo.SelectTagHandler.setJspContext(Ljavax/servlet/jsp/JspContext;)V
org.apache.jsp.new_jsp._jspx_meth_mine_005fsimple_005f0(new_jsp.java:73)
org.apache.jsp.new_jsp._jspService(new_jsp.java:51)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
note The full stack trace of the root cause is available in the Apache Tomcat/5.5.27 logs.
编辑:我的TLD文件的代码是:
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" version="2.0">
<tlib-version>1.2</tlib-version>
<uri>simpleTags</uri>
<tag>
<name>simple</name>
<tag-class>foo.SelectTagHandler</tag-class>
<body-content>scriptless</body-content>
</tag>
</taglib>
1.2
单纯形
简单的
foo.SelectTagHandler
无脚本
我在这里怎么了?
setJspContext
只适用于SimpleTag
和SimpleTagSupport
,而不是BodyTag
(不要问我为什么)。出于某种原因,Tomcat将您的标签视为SimpleTag。问题可能出在您的TLD
文件中,您能否修改您的问题以添加该文件?setJspContext
仅适用于SimpleTag
和SimpleTagSupport
,而不是BodyTag
(不要问我为什么)。出于某种原因,Tomcat将您的标签视为SimpleTag。问题可能存在于您的TLD
文件中,您能否修改您的问题以添加该文件?您正在直接在代码中打印正文内容
:
pageContext.getOut().print(bodyContent);
bodyContent
不是字符串,它是javax.servlet.jsp.tagext.bodyContent
类的对象,并且它没有定义合适的toString()方法(也不应该)以这种方式使用。您应该通过getString()
方法从中获取实际的正文内容:
if (bodyContent!=null) {
String bodyText = bodyContent.getString();
pageContext.getOut().print(bodyText); // or whatever you want to do with it
}
您直接在代码中打印出
bodyContent
:
pageContext.getOut().print(bodyContent);
bodyContent
不是字符串,它是javax.servlet.jsp.tagext.bodyContent
类的对象,并且它没有定义合适的toString()方法(也不应该)以这种方式使用。您应该通过getString()
方法从中获取实际的正文内容:
if (bodyContent!=null) {
String bodyText = bodyContent.getString();
pageContext.getOut().print(bodyText); // or whatever you want to do with it
}
如果使用Taglib版本2.1而不是2.0,就会发生这种情况。更改tld文件中taglib标记的版本和模式属性。它一定是这样的:
<taglib version="2.0"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd">
如果使用Taglib 2.1版而不是2.0版,就会发生这种情况。更改tld文件中taglib标记的版本和模式属性。它一定是这样的:
<taglib version="2.0"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd">
只是一种预感,但请尝试将正文内容设置为“JSP”而不是“无脚本”谢谢,它现在工作正常,并将输出返回为“Advisor page org.apache.jasper.runtime”。BodyContentImpl@7be8c2而不是“顾问页芭蕾”。为什么?这只是一种预感,但请尝试将主体内容设置为“JSP”而不是“无脚本”谢谢,它现在工作正常,并将输出返回为“Advisor page org.apache.jasper.runtime”。BodyContentImpl@7be8c2而不是“顾问页芭蕾”。为什么?这里的例子不太正确。如果要访问响应输出(在扩展BodyTag支持的标记中的doAfterTag()方法中,并且doStartTag()方法返回EVAL_BODY_BUFFERED),则需要执行bodyContent.getEnclosingWriter().write(bodyText)之类的操作;这里的例子不太正确。如果要访问响应输出(在扩展BodyTag支持的标记中的doAfterTag()方法中,并且doStartTag()方法返回EVAL_BODY_BUFFERED),则需要执行bodyContent.getEnclosingWriter().write(bodyText)之类的操作;