【问题标题】:JSP Recursive ImportJSP 递归导入
【发布时间】:2013-04-12 08:35:57
【问题描述】:

我有一个主 JSP 文件,我想将一组递归匹配文件从我的 Web 应用程序导入(它们是 mustache 模板)。

我想做这样的事情:

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

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

有没有办法通过 JSTL 标签等来做到这一点?

【问题讨论】:

    标签: java jsp include jspinclude


    【解决方案1】:

    您可以使用 JSTL&lt;c:import&gt;&lt;c:param&gt;,这类似于 JSP 中的上述导入语句。 &lt;c:import&gt; 标签提供操作的所有功能,但也允许包含绝对 URL。

    我们还可以包含那些不属于当前 Web 应用程序但位于 Web 应用程序之外的内容或文件。所以,jstl &lt;c:import&gt;&lt;jsp:include&gt; 更有用。

    通过以下语法

        <%@ 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 文件。我不太明白上面的语法与简单的 ? 有什么不同
    【解决方案2】:

    我用一个 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多