Java自定义标签不起作用/使用问题
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java自定义标签不起作用/使用问题相关的知识,希望对你有一定的参考价值。
我在JSP中使用自定义标签存在很大的问题。我已经在WEB-INF / tlds中创建了一个标记库描述符文件,称为“装饰”。我将前缀设置为“ d”。然后,我创建了名为“ decorateHandler”的标签处理程序类。
我想用简单的方法修饰JSP标题页中的文本,但出现错误...
我的decorate.tld文件:
<tlib-version>1.0</tlib-version>
<short-name>l</short-name>
<uri>/WEB-INF/tlds/decorate</uri>
<tag>
<name>decorate</name>
<tag-class>decorateHandler</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>value</name>
</attribute>
</tag>
</taglib>
在decorateHandler.java中,我试图这样做:
private String value;
private void writeTagBodyContent(JspWriter out, BodyContent bodyContent) throws IOException {
out.println("<font color='"+value+"'>");
bodyContent.writeOut(out);
out.println("</font>");
bodyContent.clearBody();
}
当然,我已经为String值变量创建了getter和setter。
在index.jsp中,我像这样包含了taglib:
<%@taglib uri="/WEB-INF/tlds/decorate.tld" prefix="d" %>
而且我尝试过这样的事情:
<d:decorate value="red">Lorem ipsum</d:decorate>
但是我有一个例外:
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: [112] in the jsp file: [/index.jsp]
decorateHandler cannot be resolved to a type
109: <li class="nav-item">
110: <a class="nav-link" href="index.jsp">
111: <img src="logo.png" height="20px" width="20px">
112: <d:decorate value="red">Lorem ipsum</d:decorate>
113: </a>
114: </li>
115: </ul>
An error occurred at line: [112] in the jsp file: [/index.jsp]
decorateHandler cannot be resolved to a type
109: <li class="nav-item">
110: <a class="nav-link" href="index.jsp">
111: <img src="logo.png" height="20px" width="20px">
112: <d:decorate value="red">Lorem ipsum</d:decorate>
113: </a>
114: </li>
115: </ul>
An error occurred at line: [112] in the jsp file: [/index.jsp]
The method get(Class<? extends Tag>) in the type TagHandlerPool is not applicable for the arguments (Class<decorateHandler>)
109: <li class="nav-item">
110: <a class="nav-link" href="index.jsp">
111: <img src="logo.png" height="20px" width="20px">
112: <d:decorate value="red">Lorem ipsum</d:decorate>
113: </a>
114: </li>
115: </ul>
An error occurred at line: [112] in the jsp file: [/index.jsp]
decorateHandler cannot be resolved to a type
109: <li class="nav-item">
110: <a class="nav-link" href="index.jsp">
111: <img src="logo.png" height="20px" width="20px">
112: <d:decorate value="red">Lorem ipsum</d:decorate>
113: </a>
114: </li>
115: </ul>
Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:102)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:212)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:549)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:378)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:350)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:334)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:595)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:399)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Filtr2.doFilter(Filtr2.java:67)
我不知道是什么问题...:/
答案
首先,我认为您应该检查Web容器(例如Tomcat,Glassfish?)的日志。该错误的堆栈跟踪将在此处可用,并提示您真正的原因是什么。无论如何,乍一看,很明显该错误是由自定义装饰器taglib引起的,它在最后似乎执行了bodyContent.clearBody()
。我认为这里不允许这样做,也许您是想改用bodyContent.flush()
?
[Here有关如何创建自定义jsp标记的教程。
以上是关于Java自定义标签不起作用/使用问题的主要内容,如果未能解决你的问题,请参考以下文章