我可以将自己的jsp页面导入到另一个jsp页面中吗?

发布于 2024-12-01 19:08:29 字数 78 浏览 1 评论 0原文

我已经使用 netbeans 准备了一个静态 html 页面。 我可以将该页面导入到另一个页面中,以便不再重新编写代码,然后进行相应的更改吗?

I have prepared a static html page using netbeans.
Can I import that page into another page so as to not re-write the code again n again and then make the respective changes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

仅一夜美梦 2024-12-08 19:08:29

您可以通过使用像这样的 include 指令将一个 JSP 页面合并到另一个页面中 -

<%@ include file="/path/to/yourfile.jsp" %>

或者通过使用像这样的 标准操作 -

<jsp:include page="/path/to/yourfile.jsp"/>

从上述两种方法中,第一个将导致 yourfile.jsp 的内容在页面翻译时包含在内。也就是说,当页面转换为完整的 servlet 类时,yourfile.jsp 的内容将包含在 servlet 中。因此,这种包含只会在页面翻译时发生一次,仅在应用程序启动后的第一个用户请求时发生。

如果您使用第二种方法,那么对于每个用户请求,yourfile.jsp 的响应将在运行时包含,而不是在页面翻译时包含。

当您使用 include 指令时,您基本上将目标文件的内容复制并粘贴到主文件中。如果目标文件包含任何生成动态内容的标签或EL,那么它们也将成为主文件的一部分,并且它们将相应地执行并生成动态内容。那里没问题。

但这种方法有一些局限性。例如,使用 include 包含的页面无法更改响应状态代码或设置标头,这意味着您无法调用 addCookies() 或其他一些标头设置yourfile.jsp 中的方法。如果你这样做,你不会得到错误,但你只是不会得到你所希望的。如果您使用 那么所有这些都可以在包含的页面中完成,并且它们将相应地工作。

这两种方法之间存在另一个重要区别。假设您要包含一个文件,其中包含一些上下文相关的文本,这些文本会根据包含它们的页面而变化。使用 include 方法,您将无法优雅地完成此任务。但是使用 方法,您可以做到这一点 -

<jsp:include page="/path/to/yourfile.jsp">
    <jsp:param name="myContextSensitiveText" value="Context Sensitive!!" />
</jsp:include>

这意味着您正在为 yourfile.jsp 指定一个新的请求参数,然后您可以从该文件并相应地渲染它 -

${param.myContextSensitiveText} - Context Insensitive Text!!

使用 include 方法,您将无法完成此任务。

您应该使用哪一种完全取决于您的设计选择。

您应该记住另一件事 - 这两种方法都会将目标文件的内容包含到主文件中,尽管方式不同。因此,如果它们都包含 这样的 html 元素,那么您最终会得到两个 > 和最终渲染页面中的两个 元素,该元素无效。

有关更多信息,您可以查看此处此处

编辑

还有第三种方法可以使用 JSTL。您可以使用 导入< /code>用于此目的的 JSTL 标记 -

<%-- You need to declare this at the top of your jsp page--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
...............
...............

<c:import url="/path/to/yourfile.jsp" />

此方法的工作方式与 完全相同,只是功能更强大一些。您可以包含应用程序目录外部页面的内容,甚至也可以包含容器外部的内容!例如 -

<c:import url="http://www.google.com" />

此行将包含 google 主页的 HTML 内容。

如果您需要将参数传递到包含的页面,例如 ,那么您可以使用 param 标记 -

<c:import url="/path/to/yourfile.jsp">
    <c:param name="myContextSensitiveText" value="Context Sensitive!!" />
</c:import>

并以相同的方式访问它 -

${param.myContextSensitiveText} - Context Insensitive Text!!

You can incorporate a JSP page into another by using include directive like this -

<%@ include file="/path/to/yourfile.jsp" %>

or by using <jsp:include> standard action like this -

<jsp:include page="/path/to/yourfile.jsp"/>

From the above two approaches, the first one will cause the contents of yourfile.jsp to be included at page-translation time. That is, when the page is translated into a full-fledged servlet class, contents of yourfile.jsp will be included in the servlet. So this inclusion will happen only once, at page translation time which occurs only on the first user request after your app is up.

If you use the second approach, then on every user request the response from the yourfile.jsp will be included at run-time, rather than at page-translation time.

When you use include directive, you basically copy and paste the content of the target file into the main file. If the target file contains any tag or EL which generated dynamic content, then these will also become a part of the main file and they will execute accordingly and will generate dynamic content. No problem there.

But this approach has some limitations. For example, a page which has been included using include cannot change the response status code or set headers which means you can't call addCookies() or some other header-setting methods from yourfile.jsp. You won't get an error if you do this though, you just won't get what you are hoping for. If you use <jsp:include> then all of these can be done in the included page and they will work accordingly.

Another important distinction exists between these two approaches. Suppose you want to include a file which has a little context-sensitive texts that change depending on the page into which they are being included. With the include approach, you won't be able to accomplish this elegantly. But with the <jsp:include> approach, you can do this -

<jsp:include page="/path/to/yourfile.jsp">
    <jsp:param name="myContextSensitiveText" value="Context Sensitive!!" />
</jsp:include>

which means you are specifying a new request parameter for yourfile.jsp, which you can then access from that file and render it accordingly -

${param.myContextSensitiveText} - Context Insensitive Text!!

with the include approach, you won't be able to accomplish this.

Which one should you use will purely depend on your design choice.

You should remember another thing - both of these approaches will include the contents of the target file into the main file, although in different way. So if both of them contains html elements like <html> or <body>, then you will end up with two <html> and two <body> elements in the final-rendered page, which won't be valid.

For more information, you can take a look at here and here.

Edit

There is a third way to include a page using JSTL. You can use import JSTL tag for this purpose -

<%-- You need to declare this at the top of your jsp page--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
...............
...............

<c:import url="/path/to/yourfile.jsp" />

This approach works in exactly the same way as <jsp:include>, except it's a bit more powerful. You can include contents from a page which is outside of your application directory, even outside of your container too! As an example -

<c:import url="http://www.google.com" />

this line will include the HTML content of the google's home page.

If you need to pass parameters to your included page like <jsp:include>, then you can use param tag -

<c:import url="/path/to/yourfile.jsp">
    <c:param name="myContextSensitiveText" value="Context Sensitive!!" />
</c:import>

and access it in the same way -

${param.myContextSensitiveText} - Context Insensitive Text!!
不即不离 2024-12-08 19:08:29

是的,您可以将jsp页面导入到另一个jsp中。

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

在 JSP 页面中包含内容

Yes you can import the jsp page in another jsp.

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

Including Content in a JSP Page

梦萦几度 2024-12-08 19:08:29

你可以通过这些方式做到这一点,

<%@ include file="/path/to/yourfile.jsp" %>

或者

<jsp:include page="/path/to/yourfile.jsp"/>

或者

 <c:import url="/path/to/yourfile.jsp" />

You can do it in these ways,

<%@ include file="/path/to/yourfile.jsp" %>

or

<jsp:include page="/path/to/yourfile.jsp"/>

or

 <c:import url="/path/to/yourfile.jsp" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文