使用JSP include指令包含文件,JSP包含操作和使用JSP标记文件之间的区别是什么? [英] What's the difference between including files with JSP include directive, JSP include action and using JSP Tag Files?

查看:126
本文介绍了使用JSP include指令包含文件,JSP包含操作和使用JSP标记文件之间的区别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎有两种使用JSP进行模板化的方法。包含其中一个语句的文件

It seems that there are two methods for templating with JSP. Including files with one of these statements

<%@ include file="foo.html" %>
<jsp:include page="foo.html" />

或使用JSP标记文件

// Save this as mytag.tag
<%@ tag description="Description" pageEncoding="UTF-8"%>
<html>
<head>
</head>
<body>
    <jsp:doBody/>
</body>
</html>

在另一个JSP页面中调用它

And in another JSP page call it with

<%@ taglib prefix="t" tagdir="/WEB-INF/tags" %>

<t:mytag>
    <h1>Hello World</h1>
</t:mytag>

那么我应该使用哪种方法?一个现在被认为已被弃用,或者它们是否有效并涵盖不同的用例?

So which method should I use? Is one now considered deprecated or are they both valid and cover different use cases?

编辑

是否使用此标记文件与使用include相同?

Isn't using this tag file the same as using an include?

// Save this as product.tag
<%@ tag description="Product templage" pageEncoding="UTF-8"%>
<%@ tag import="com.myapp.Product" %>
<%@ attribute name="product" required="true" type="com.myapp.Product"%>

Product name: ${product.name} <br/>
Quantity: ${product.quantity} <br/>

并在另一个JSP上调用它

And call it on another JSP with

<%@ taglib prefix="t" tagdir="/WEB-INF/tags" %>

<t:product>
    <c:forEach items="${cart.products}" var="product">
        <t:product product="${product}"/>
    </c:forEach>
</t:product>

在我看来,与使用include和传递参数完全相同。那么Tag文件是否与包含相同?

That seems to me to be the very same as using an include and passing parameters to it. So are Tag Files the same as includes?

推荐答案

有几种机制可以重用JSP文件中的内容。

There are several mechanisms for reusing content in a JSP file.

以下 4在JSP中包含内容的机制可归类为直接重用

(for引用Head First Servlets and JSP的前3个机制

The following 4 mechanisms to include content in JSP can be categorized as direct reuse:
(for the first 3 mechanisms quoting from "Head First Servlets and JSP")


1)包含指令:



1) The include directive:

<%@ include file="header.html" %>

静态:将文件属性值的内容添加到当前页在翻译时。该指令原本是用于静态布局模板的
,如HTML标题。

Static: adds the content from the value of the file attribute to the current page at translation time. The directive was originally intended for static layout templates, like HTML headers.

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

动态:将页面属性值的内容添加到当前页面请求时间。更适用于来自JSP的动态
内容。

3)< c:import>
JSTL代码:

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

动态:将网址属性值中的内容添加到当前页面,请求时间。它有点像
< jsp:include> ,但它更强大和灵活:不像
其他两个包括,< c:import> 网址可以来自
网络容器外

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!

静态:前奏曲和密码只能应用于页面的开头和结尾

您可以为一组JSP隐式包含前奏(也称为标题)和 codas
(也称为页脚)通过在$中分别添加
< include-prelude> < include-coda> 元素的页面Web应用程序 web.xml 部署描述符中的b $ ba < jsp-property-group> 元素。在这里阅读更多内容:

配置JSP开头和结尾的隐式包含

定义隐式包含

Static: preludes and codas can be applied only to the beginnings and ends of pages.
You can implicitly include preludes (also called headers) and codas (also called footers) for a group of JSP pages by adding <include-prelude> and <include-coda> elements respectively within a <jsp-property-group> element in the Web application web.xml deployment descriptor. Read more here:
• Configuring Implicit Includes at the Beginning and End of JSPs
• Defining implicit includes






标记文件是内容重用的间接方法封装可重用内容的方式。
标记文件是一个源文件,其中包含可作为自定义标记重用的JSP代码片段。


Tag File is an indirect method of content reuse, the way of encapsulating reusable content. A Tag File is a source file that contains a fragment of JSP code that is reusable as a custom tag.

包含和标记文件的目的不同。

The PURPOSE of includes and Tag Files is different.

标记文件 (JSP 2.0引入的概念)是的选项之一用于创建自定义标记。这是构建自定义标记的更快速,更简单的方法。
自定义标记 ,也称为标记扩展,是JSP元素,允许将其他Java组件提供的自定义逻辑和输出插入到JSP页面中。通过自定义标记提供的逻辑由称为标记处理程序的Java对象实现。

Tag file (a concept introduced with JSP 2.0) is one of the options for creating custom tags. It's a faster and easier way to build custom tags. Custom tags, also known as tag extensions, are JSP elements that allow custom logic and output provided by other Java components to be inserted into JSP pages. The logic provided through a custom tag is implemented by a Java object known as a tag handler.

自定义标签可以执行的一些任务示例包括对隐式对象,处理表单,访问数据库和其他企业服务(如电子邮件和目录)进行操作,以及实现流程控制。

Some examples of tasks that can be performed by custom tags include operating on implicit objects, processing forms, accessing databases and other enterprise services such as email and directories, and implementing flow control.

也许在你的例子中(在您的编辑),使用直接包含和标记文件没有区别。但自定义标记具有丰富的功能。他们可以

Maybe in your example (in your Edit), there is no difference between using direct include and a Tag File. But custom tags have a rich set of features. They can


  • 通过从调用页面传递的属性进行自定义。

  • Be customized by means of attributes passed from the calling page.

将变量传递回调用页面。

Pass variables back to the calling page.

访问JSP页面可用的所有对象。

Access all the objects available to JSP pages.

互相沟通。您可以创建和初始化JavaBeans组件,创建一个公共EL变量,在一个标记中引用该bean,然后在另一个标记中使用该bean。

Communicate with each other. You can create and initialize a JavaBeans component, create a public EL variable that refers to that bean in one tag, and then use the bean in another tag.

彼此嵌套并通过私有变量进行通信。

Be nested within one another and communicate by means of private variables.

另请阅读Pro JSP 2: 了解JSP自定义标签

Also read this from "Pro JSP 2": Understanding JSP Custom Tags.

JSP技巧让模板更容易

来自 coreservlet.com 的非常丰富且易于理解的教程,包含< jsp:include>的精美
解释VS. <%@ include%>

比较表:

在JSP
页面中包含文件和小程序

Very informative and easy to understand tutorial from coreservlet.com with beautiful explanations that include <jsp:include> VS. <%@ include %> comparison table:
Including Files and Applets in JSP Pages

来自 coreservlets.com 的另一个很好的教程与标记库和
标记文件相关:

创建自定义JSP标记库:
基础

Another nice tutorial from coreservlets.com related to tag libraries and tag files:
Creating Custom JSP Tag Libraries: The Basics

带有示例的官方Java EE 5教程:

封装可重用内容
使用标记
文件

The official Java EE 5 Tutorial with examples:
Encapsulating Reusable Content Using Tag Files.

官方Java EE 5教程中的这个页面应该可以让你更加了解


重用JSP
页面中的内容

This page from the official Java EE 5 tutorial should give you even more understanding:
Reusing Content in JSP Pages.

Pro JSP 2一书中的摘录也讨论了为什么需要
a标记文件而不是使用静态包含


使用标记重新使用内容
文件

This excerpt from the book "Pro JSP 2" also discuses why do you need a Tag File instead of using static include:
Reusing Content with Tag Files

使用权利具体任务的工具。

使用标记文件 作为一种快速简便的创建方式自定义标记

关于JSP中的包含内容(引自这里):

As for the including content in JSP (quote from here):



  • 如果文件很少更改,请使用include指令。这是最快的机制。如果您的容器未自动检测
    更改,则可以通过删除主
    页面类文件强制更改生效。

  • Use the include directive if the file changes rarely. It’s the fastest mechanism. If your container doesn’t automatically detect changes, you can force the changes to take effect by deleting the main page class file.

仅对经常更改的内容使用包含操作,以及在主页为
请求之前无法确定要包含的页面。

Use the include action only for content that changes often, and if which page to include cannot be decided until the main page is requested.

这篇关于使用JSP include指令包含文件,JSP包含操作和使用JSP标记文件之间的区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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