JSP递归导入
问题描述:
我有一个主JSP文件,我想从我的web应用程序导入一组递归匹配文件(它们是胡须模板)。JSP递归导入
我想要做这样的事情:
<jsp:include page="**/*.mustache"/>
或
<%@ include file="**/*.mustache" %>
有没有办法做到这一点通过JSTL标签等?
答
我解决了这个问题,有位哈克小脚本的:
<%
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>
答
您可以使用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>"/>
嗯,但我想通过子目录递归地做到这一点,拿起所有的* .mustache文件。我不太清楚上面的语法与简单的有什么不同? –
Kong