如何在运行时加载的jsp中包含html片段? [英] How to include an html fragment in a jsp that loads at run-time?

查看:112
本文介绍了如何在运行时加载的jsp中包含html片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要不使用包含指令将内容包含在JSP中.

I need to include content in a JSP without using the include directive.

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

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 before the 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.

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

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?

推荐答案

您可以使用.tag文件获取可重复使用的内容,也可以<jsp:include>( INCLUDE 分派)到另一个JSP.

You can use .tag files for reusable content or do <jsp:include> (INCLUDE dispatch) to another JSP.

但是,如果其中包含以下任何一项,则Tomcat 能够重新编译JSP文件已更改:

However Tomcat is able to re-compile JSPs when any of the included files got changed:

当包含的页面发生更改时重新编译JSP -现在,Jasper 2可以检测到JSP编译时包含的页面何时发生了更改,然后重新编译父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.

还请注意,通常的做法是对通过<%@include %>包含的文件使用.jspf(JSP片段)扩展名.

Also note, that the common practice is to use .jspf (JSP fragment) extension for files included via <%@include %>.

这篇关于如何在运行时加载的jsp中包含html片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆