【发布时间】:2013-04-12 08:35:57
【问题描述】:
我有一个主 JSP 文件,我想将一组递归匹配文件从我的 Web 应用程序导入(它们是 mustache 模板)。
我想做这样的事情:
<jsp:include page="**/*.mustache"/>
或
<%@ include file="**/*.mustache" %>
有没有办法通过 JSTL 标签等来做到这一点?
【问题讨论】:
标签: java jsp include jspinclude
我有一个主 JSP 文件,我想将一组递归匹配文件从我的 Web 应用程序导入(它们是 mustache 模板)。
我想做这样的事情:
<jsp:include page="**/*.mustache"/>
或
<%@ include file="**/*.mustache" %>
有没有办法通过 JSTL 标签等来做到这一点?
【问题讨论】:
标签: java jsp include jspinclude
您可以使用 JSTL<c:import> 和 <c:param>,这类似于 JSP 中的上述导入语句。
<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>
【讨论】: