JSP Custom tags - Fragment Attribute not working

2k Views Asked by At

I am trying to create my jsp custom tag for layout purpose.

I created panelLayout.tag file like

<%@ tag language="java" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<%@ attribute name="heading" required="false" %>
<%@ attribute name="footer" fragment="true"%>

<div class="panel panel-default">

    <c:if test="${not empty heading}">
        <div class="panel-heading">
            <c:out value="${heading}"></c:out>
        </div>
    </c:if>

    <div class="panel-body">    
        <jsp:doBody/>                            
    </div>

    <div class="panel-footer">
        <jsp:invoke fragment="footer"/>
    </div>  
</div>

And I am using it in one of the jsp as below

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

<t:panelLayout>
    This is my body                            
   <jsp:attribute name="footer">
       This is my footer
   </jsp:attribute>
</t:panelLayout>

And I am getting below error

org.apache.jasper.JasperException: /WEB-INF/pages/Groups.jsp (line: 86, column: 33) jsp:attribute must be the subelement of a standard or custom action
    org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:42)
    org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:443)
    org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:89)
    org.apache.jasper.compiler.Parser.parseStandardAction(Parser.java:1126)
    org.apache.jasper.compiler.Parser.parseElementsScriptless(Parser.java:1499)
    org.apache.jasper.compiler.Parser.parseBody(Parser.java:1666)
    org.apache.jasper.compiler.Parser.parseOptionalBody(Parser.java:986)
    org.apache.jasper.compiler.Parser.parseCustomTag(Parser.java:1258)
    org.apache.jasper.compiler.Parser.parseElements(Parser.java:1451)
    org.apache.jasper.compiler.Parser.parse(Parser.java:138)
    org.apache.jasper.compiler.ParserController.doParse(ParserController.java:242)
    org.apache.jasper.compiler.ParserController.parse(ParserController.java:102)
    org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:199)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:374)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:354)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:341)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:662)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:364)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
    org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:263)
    org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1208)
    org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:992)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:939)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.64 logs.

If I remove footer fragment, it is working fine.

I got the reference from here https://docs.oracle.com/cd/E19879-01/819-3669/bnamr/index.html

Searched stackoverflow and other reference as well, but couldn't find what I did wrong.

Any help appreciated.

1

There are 1 best solutions below

0
On

You can try to enclose the body between <jsp:body></jsp:body>:

<t:panelLayout>
    <jsp:attribute name="footer">
        This is my footer
    </jsp:attribute>
    <jsp:body>
        This is my body
    </jsp:body>
</t:panelLayout>

Check The Java EE 5 Tutorial - Types of Tags:

jsp:body Element

You can also explicitly specify the body of a simple tag by using the jsp:body element. If one or more attributes are specified with the jsp:attribute element, then jsp:body is the only way to specify the body of the tag. If one or more jsp:attribute elements appear in the body of a tag invocation but you don’t include a jsp:body element, the tag has an empty body.