我需要在 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/