vic_sk
vic_sk

Reputation: 39

JSP error with custom tag file

I am getting this JSP error and it makes no sense because syntax in my test code looks fine:

org.apache.jasper.JasperException: /tests/jsptag1.jsp (line: 12, column: 2) Unterminated <my:item tag
org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:41)
org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:275)
org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:91)
org.apache.jasper.compiler.Parser.parseJspAttributeAndBody(Parser.java:1031)
org.apache.jasper.compiler.Parser.parseOptionalBody(Parser.java:999)
org.apache.jasper.compiler.Parser.parseCustomTag(Parser.java:1276)
org.apache.jasper.compiler.Parser.parseElements(Parser.java:1455)
org.apache.jasper.compiler.Parser.parse(Parser.java:139)
org.apache.jasper.compiler.ParserController.doParse(ParserController.java:227)
org.apache.jasper.compiler.ParserController.parse(ParserController.java:100)
org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:199)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:356)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:336)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:323)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:585)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:363)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

This is my test jsptag1.jsp:

<%@taglib prefix="my" tagdir="/WEB-INF/tags" %>


<% request.setAttribute("items", new int[]{1, 2, 3}); %>
<my:item>
    <jsp:attribute name="header">
        <br>This is the header <br>
    </jsp:attribute>
    <jsp:body>
        value: 
    </jsp:body>
    <jsp:attribute name="footer">
        <br>This is footer<br>
    </jsp:attribute>
</my:item>

and this is my item.tag file:

<%@tag body-content="scriptless" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@attribute name="header" fragment="true" %>
<%@attribute name="footer" fragment="true" %>

<jsp:invoke fragment="header"/>
<c:forEach items="${requestScope['items']}" var="thisItem">
    <jsp:doBody/>
    Item: ${thisItem}
</c:forEach>
<jsp:invoke fragment="footer"/>

I really don't see why this error is showing up. I seem to have all tags open and close. Does anybody see why this error is showing?

Upvotes: 2

Views: 1191

Answers (1)

erickson
erickson

Reputation: 269877

Put all of the jsp:attribute elements before the jsp:body element when you are invoking the custom tag. Specifically, the my:item element in jsptag1.jsp should contain the header element, the footer element, and the body element, in that order.

While specification doesn't point this out in the informational text, the JSP grammar in the specification requires this order.

Upvotes: 4

Related Questions