Java 如何在运行时加载的jsp中包含一个html片段?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20030773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to include an html fragment in a jsp that loads at run-time?
提问by Thorn
I need to include content in a JSP without usingthe include directive.
我需要在不使用include 指令的情况下在 JSP 中包含内容。
It is easy to do a server-side include with with <%@include file="includeMe.htm" %>
but the content of includeMe.htm
gets added to the JSP beforethe JSP is converted into a Servlet by the container. This means that if includeMe.htm gets modified, the changes are not reflected in the generated .java
Servlet file. I'm tired of going into Tomcats generated files directory to manually delete the generated java and class files or re-deploy my app every time the included file changes.
使用 with 进行服务器端包含很容易,<%@include file="includeMe.htm" %>
但是在容器将 JSP 转换为 Servlet之前,includeMe.htm
将其内容添加到 JSP中。这意味着如果 includeMe.htm 被修改,更改不会反映在生成的Servlet 文件中。我厌倦了进入 Tomcats 生成的文件目录手动删除生成的 java 和 class 文件或每次包含的文件更改时重新部署我的应用程序。.java
Do I need to write a code block to just read in data from the text file line by line and then write it like this?
我是否需要编写一个代码块来逐行从文本文件中读取数据,然后像这样编写?
<%
while( not done reading from file ) {
String line = scanner.nextLine();
response.getWriter().println(line);
} %>
Is there an easier or cleaner way?
有没有更简单或更干净的方法?
采纳答案by Pavel Horal
You can use .tag
files for reusable content or do <jsp:include>
(INCLUDEdispatch) to another JSP.
您可以将.tag
文件用于可重用的内容或执行<jsp:include>
(包括分派)到另一个 JSP。
However Tomcat is able to re-compile JSPs when any of the included files got changed:
但是,当任何包含的文件发生更改时,Tomcat能够重新编译 JSP:
Recompile JSP when included page changes- Jasper 2 can now detect when a page included at compile time from a JSP has changed and then recompile the parent JSP.
在包含的页面更改时重新编译 JSP- Jasper 2 现在可以检测在编译时从 JSP 包含的页面何时发生更改,然后重新编译父 JSP。
Also note, that the common practice is to use .jspf
(JSP fragment) extension for files included via <%@include %>
.
另请注意,通常的做法是对.jspf
通过<%@include %>
.
回答by bancer
Take a look at answers here.
You can also use <c:import>
:
看看这里的答案。您还可以使用<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!
2)
<jsp:include>
标准动作<jsp:include page="header.jsp" />
动态:在请求时将页面属性值中的内容添加到当前页面。更适用于来自 JSP 的动态内容。
3)
<c:import>
JSTL标签:<c:import url=”http://www.example.com/foo/bar.html” />
动态:在请求时将 URL 属性值中的内容添加到当前页面。它的工作原理很像
<jsp:include>
,但它更强大和灵活:与其他两个包含不同,<c:import>
url 可以来自 web Container 之外!
Regarding reading a file into string. If the file is small do it one line:
关于将文件读入字符串。如果文件很小,请执行一行:
String content = new Scanner(new File("filename")).useDelimiter("\Z").next();