Search code examples
javajspincludejspinclude

JSP Recursive Import


I have a master JSP file that I want to import a recursive set of matching files from my web app into (they are mustache templates).

I want to do something like this:

<jsp:include page="**/*.mustache"/>

or

<%@ include file="**/*.mustache" %>

Is there any way to do that via a JSTL tag etc?


Solution

  • I solved the issue with a bit of a hacky scriptlet:

    <%
    String path = pageContext.getServletContext().getRealPath("/");
    Collection<String> paths = new ArrayList<String>();
    for (File file : FileUtils.listFiles(new File(path), new String[] { "ext" }, true)) {
        //Make the path relative
        paths.add(file.getAbsolutePath().substring(path.length()));
    }
    %>
    <c:forEach items="<%=paths%>" var="path">
        <jsp:include page="../${path}"/>
    </c:forEach>