java - 如何在运行时加载的 jsp 中包含 html 片段?

标签 java file jsp servlets

我需要在 JSP 中包含内容而不使用 include 指令。

服务器端包含 <%@include file="includeMe.htm" %> 很容易但是includeMe.htm的内容容器将 JSP 转换为 Servlet 之前添加到 JSP。这意味着如果 includeMe.htm 被修改,这些更改不会反射(reflect)在生成的 .java 中。服务小程序文件。我厌倦了进入 Tomcats 生成的文件目录手动删除生成的 java 和类文件或每次包含的文件更改时重新部署我的应用程序。

我是否需要编写一个代码块来逐行读取文本文件中的数据,然后这样写?

 <%
    while( not done reading from file ) {
        String line = scanner.nextLine();
        response.getWriter().println(line);
    }  %>

有没有更简单或更清洁的方法?

最佳答案

看看答案here . 您也可以使用 <c:import> :

2) The <jsp:include> standard action

<jsp:include page="header.jsp" />

Dynamic: adds the content from the value of the page attribute to the current page at request time. Was intended more for dynamic content coming from JSPs.

3) The <c:import> JSTL tag:

<c:import url=”http://www.example.com/foo/bar.html” />

Dynamic: adds the content from the value of the URL attribute to the current page, at request time. It works a lot like <jsp:include>, but it’s more powerful and flexible: unlike the other two includes, the <c:import> url can be from outside the web Container!

关于将文件读入字符串。如果文件很小,只需要一行:

String content = new Scanner(new File("filename")).useDelimiter("\\Z").next();

关于java - 如何在运行时加载的 jsp 中包含 html 片段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20030773/

相关文章:

c++ - 在 C++ 中更改文件的一部分

php - 如何计算PHP目录中.txt文件的数量?

java - 如何在 netui 数据转发器中使用 if 子句

java - 如何在运行时将 JSR-045 SMAP 信息添加到 Java 堆栈跟踪?

java - 将 Java Applet 连接到 MySQL DB - 通信链路故障

java - MQ 发布/订阅特定于域的接口(interface)通常比点对点更快吗?

python - py2exe 打包数据文件

java - 使用 java 流和收集来获取泛型类型

java - 在方法中使用对象

java - WSO2 IoT 服务器管理器在登录时返回 403 错误