我希望通过扩展类BodyTagSupport,但是获取运行时异常,从标记处理程序中显示自定义标记名称中的JSP的正文值; o)
JSP代码是:
<html><body>
<%@ taglib prefix="mine" uri="simpleTags" %>
Advisor page
<mine:simple>
Balle Balle
</mine:simple>
</html></body>
和Tag处理程序类是:
package foo;
import java.io.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
public class SelectTagHandler extends BodyTagSupport
{
public int doStartTag() throws JspException{
return EVAL_BODY_BUFFERED;
}
public int EndTag()
{
try{
pageContext.getOut().print(bodyContent);
}
catch(Exception e)
{
}
return EVAL_PAGE;
}
和异常,我得到的是:
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>
我在这里做错了什么?
答案 0 :(得分:1)
setJspContext
仅适用于SimpleTag
和SimpleTagSupport
,而不适用BodyTag
(不要问我原因)。由于某种原因,Tomcat将您的标记视为SimpleTag。问题可能出在您的TLD
文件中,您可以修改问题以添加该文件吗?
答案 1 :(得分:0)
您在代码中直接打印bodyContent
:
pageContext.getOut().print(bodyContent);
bodyContent
不是String,它是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
}
答案 2 :(得分:0)
如果您使用Taglib版本2.1而不是2.0,则会发生这种情况。更改tld文件中taglib-Tag的版本和架构属性。看起来应该是这样的:
<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">