我有一个主 JSP 文件,我想将一组递归匹配文件从我的 Web 应用程序导入(它们是 mustache 模板)。
我想做这样的事情:
<jsp:include page="**/*.mustache"/>
或者
<%@ include file="**/*.mustache" %>
有没有办法通过 JSTL 标签等来做到这一点?
我有一个主 JSP 文件,我想将一组递归匹配文件从我的 Web 应用程序导入(它们是 mustache 模板)。
我想做这样的事情:
<jsp:include page="**/*.mustache"/>
或者
<%@ include file="**/*.mustache" %>
有没有办法通过 JSTL 标签等来做到这一点?
您可以使用类似于上述导入语句的 JSTL,就像在 JSP 中一样<c:import>
。<c:param>
该<c:import>
标记提供操作的所有功能,但也允许包含绝对 URL。
我们还可以包含那些不属于当前 Web 应用程序但位于 Web 应用程序之外的内容或文件。所以,jstl<c:import>
比<jsp:include>
.
通过以下语法
<%@ 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>"/>
我用一些 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>