我可以将自己的jsp页面导入到另一个jsp页面中吗?
我已经使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以通过使用像这样的
include
指令将一个 JSP 页面合并到另一个页面中 -或者通过使用像这样的
标准操作 -从上述两种方法中,第一个将导致
yourfile.jsp
的内容在页面翻译时包含在内。也就是说,当页面转换为完整的 servlet 类时,yourfile.jsp
的内容将包含在 servlet 中。因此,这种包含只会在页面翻译时发生一次,仅在应用程序启动后的第一个用户请求时发生。如果您使用第二种方法,那么对于每个用户请求,
yourfile.jsp
的响应将在运行时包含,而不是在页面翻译时包含。当您使用
include
指令时,您基本上将目标文件的内容复制并粘贴到主文件中。如果目标文件包含任何生成动态内容的标签或EL,那么它们也将成为主文件的一部分,并且它们将相应地执行并生成动态内容。那里没问题。但这种方法有一些局限性。例如,使用
include
包含的页面无法更改响应状态代码或设置标头,这意味着您无法调用addCookies()
或其他一些标头设置yourfile.jsp
中的方法。如果你这样做,你不会得到错误,但你只是不会得到你所希望的。如果您使用
那么所有这些都可以在包含的页面中完成,并且它们将相应地工作。这两种方法之间存在另一个重要区别。假设您要包含一个文件,其中包含一些上下文相关的文本,这些文本会根据包含它们的页面而变化。使用
include
方法,您将无法优雅地完成此任务。但是使用
方法,您可以做到这一点 -这意味着您正在为
yourfile.jsp
指定一个新的请求参数,然后您可以从该文件并相应地渲染它 -使用
include
方法,您将无法完成此任务。您应该使用哪一种完全取决于您的设计选择。
您应该记住另一件事 - 这两种方法都会将目标文件的内容包含到主文件中,尽管方式不同。因此,如果它们都包含
或
这样的 html 元素,那么您最终会得到两个
> 和最终渲染页面中的两个
元素,该元素无效。
有关更多信息,您可以查看此处和此处。
编辑
还有第三种方法可以使用 JSTL。您可以使用
导入< /code>
用于此目的的 JSTL 标记 -
此方法的工作方式与
完全相同,只是功能更强大一些。您可以包含应用程序目录外部页面的内容,甚至也可以包含容器外部的内容!例如 -此行将包含 google 主页的 HTML 内容。
如果您需要将参数传递到包含的页面,例如
,那么您可以使用param
标记 -并以相同的方式访问它 -
You can incorporate a JSP page into another by using
include
directive like this -or by using
<jsp:include>
standard action like this -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 ofyourfile.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 calladdCookies()
or some other header-setting methods fromyourfile.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 -which means you are specifying a new request parameter for
yourfile.jsp
, which you can then access from that file and render it accordingly -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 -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 -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 useparam
tag -and access it in the same way -
是的,您可以将jsp页面导入到另一个jsp中。
在 JSP 页面中包含内容
Yes you can import the jsp page in another jsp.
Including Content in a JSP Page
你可以通过这些方式做到这一点,
或者
或者
You can do it in these ways,
or
or