Question

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?

Était-ce utile?

La solution 2

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>

Autres conseils

You can use JSTL<c:import>with <c:param> which is similar to above import statements as like in the JSP. The <c:import> tag provides all of the functionality of the action but also allows for inclusion of absolute URLs.

we can also include those contents or files which are not a part of the current web application but lying somewhere outside the web application. So, the jstl <c:import> is more useful than the <jsp:include> .

By the following syntax

    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
   <c:import url = "yourPage.jsp">
<c:param name = "anyParameter" value = "<h1>Here is your value</h1>"/>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top