我可以将自己的jsp页面导入另一个jsp页面..吗? [英] Can I import my own jsp page into another jsp page..?

查看:132
本文介绍了我可以将自己的jsp页面导入另一个jsp页面..吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用netbeans准备了一个静态html页面. 我可以将该页面导入另一个页面,以免再次再次重写代码n并进行相应的更改.

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.

推荐答案

您可以使用这样的include指令-

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

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

或通过使用<jsp:include>这样的标准操作-

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

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

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

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.

如果使用第二种方法,则在每个用户请求时,都会在运行时而不是页面翻译时包含来自yourfile.jsp的响应.

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.

使用include指令时,基本上是将目标文件的内容复制并粘贴到主文件中.如果目标文件包含生成动态内容的任何标记或EL,则这些标记或EL也将成为主文件的一部分,并且它们将相应执行并生成动态内容.没问题.

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.

但是这种方法有一些局限性.例如,使用include包含的页面无法更改响应状态代码或设置标题,这意味着您无法从yourfile.jsp调用addCookies()或某些其他标题设置方法.但是,如果执行此操作,您将不会得到任何错误,而您只会得到您所希望的.如果使用<jsp:include>,则所有这些操作都可以在包含的页面中完成,并且它们将相应地工作.

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.

这两种方法之间还有另一个重要的区别.假设您要包含一个文件,该文件的上下文相关文本会有所不同,这些文本根据包含文本的页面而有所不同.使用include方法,您将无法优雅地完成此任务.但是,使用<jsp:include>方法,您可以做到这一点-

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>

这表示您正在为yourfile.jsp指定一个新的请求参数,然后可以从该文件访问并相应地呈现它-

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!!

使用include方法,您将无法完成此操作.

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

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

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

您应该记住另一件事-这两种方法都将目标文件的内容包含到主文件中,尽管使用的方式不同.因此,如果它们都包含<html><body>之类的html元素,那么最终呈现的页面中将以两个<html>和两个<body>元素结束,这将是无效的.

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.

修改

还有第三种使用 import 为此目的的JSTL标记-

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" />

此方法的作用与<jsp:include>完全相同,只是功能更强大.您可以在应用程序目录之外的页面中包含内容,甚至在容器之外!例如-

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" />

此行将包含Google主页的HTML内容.

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

如果您需要将参数传递给包含的页面(例如<jsp:include>),则可以使用

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!!

这篇关于我可以将自己的jsp页面导入另一个jsp页面..吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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