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

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

问题描述

用JSP做模板好像有两种方法.包括具有这些语句之一的文件

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

或者使用JSP标签文件

//保存为 mytag.tag<%@ tag description="Description" pageEncoding="UTF-8"%><头><身体><jsp:doBody/>

并在另一个 JSP 页面中调用它

<%@ taglib prefix="t" tagdir="/WEB-INF/tags" %><t:mytag><h1>你好世界</h1></t:mytag>

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

编辑

使用这个标签文件和使用包含文件是不是一样的?

//将此保存为 product.tag<%@ tag description="产品模板" pageEncoding="UTF-8"%><%@ tag import="com.myapp.Product" %><%@ attribute name="product" required="true" type="com.myapp.Product"%>产品名称:${product.name} 
数量:${product.quantity} <br/>

并在另一个 JSP 上调用它

<%@ taglib prefix="t" tagdir="/WEB-INF/tags" %><c:forEach items="${cart.products}" var="product"><t:product product="${product}"/></c:forEach></t:产品>

在我看来,这与使用包含并向其传递参数非常相似.那么标签文件和包含文件一样吗?

解决方案

JSP 语法元素概述

首先,为了让事情更清楚,这里是 JSP 语法元素:

  • 指令:这些将有关 JSP 页面的信息作为整个.
  • 脚本元素:这些是 Java 编码元素,例如声明、表达式、scriptlet 和注释.
  • 对象scopes:JSP 对象可以显式地或隐式地并且可以在给定范围内访问,例如从JSP 页面或会话中的任何位置.
  • 操作:这些创建对象或影响 JSP 中的输出流回应(或两者兼而有之).

JSP 中如何包含内容

有多种机制可以重用 JSP 文件中的内容.

以下4种在JSP中包含内容的机制可以归类为直接重用:
(对于前 3 种机制,引用自 "Head First Servlets andJSP")

<块引用>

1) include 指令:

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

静态:翻译时将文件属性值中的内容添加到当前页面.该指令是最初用于静态布局模板,例如 HTML 标头.

<块引用>

2) 标准动作:

动态:在请求时将页面属性值中的内容添加到当前页面.更适合动态内容来自 JSP.

3) JSTL 标签:

动态:在请求时将来自 URL 属性值的内容添加到当前页面.它的工作原理很像<jsp:include>,但它更强大和灵活:不像其他两个包括, url 可以来自外部网络容器

4) 前奏和尾声:

静态:前奏和尾声只能应用于页面的开头和结尾.
您可以隐式包含 preludes(也称为标题)和 codas(也称为页脚)为一组 JSP 页面添加 元素分别位于Web 应用程序 web.xml 部署描述符中的 元素.在此处阅读更多信息:
在 JSP 的开头和结尾配置隐式包含
定义隐式包含


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

包含文件和标记文件的用途不同.

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

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


关于您的编辑

也许在您的示例中(在您的编辑" 段落中),使用直接包含和标记文件之间没有区别.但是自定义标签具有丰富的功能.他们可以

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

  • 将变量传回调用页面.

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

  • 互相交流.您可以创建并初始化一个 JavaBeans 组件,创建一个公共 EL 变量以在一个标签中引用该 bean,然后在另一个标签中使用该 bean.

  • 相互嵌套并通过私有变量进行通信.

另请阅读Pro JSP 2":了解 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" />

or using JSP tag files

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

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?

Edit

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

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>

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?

解决方案

Overview of JSP Syntax Elements

First, to make things more clear, here is a short overview of JSP syntax elements:

  • Directives: These convey information regarding the JSP page as a whole.
  • Scripting elements: These are Java coding elements such as declarations, expressions, scriptlets, and comments.
  • Objects and scopes: JSP objects can be created either explicitly or implicitly and are accessible within a given scope, such as from anywhere in the JSP page or the session.
  • Actions: These create objects or affect the output stream in the JSP response (or both).

How content is included in JSP

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

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) The include directive:

<%@ include file="header.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.

2) The <jsp:include> standard action:

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

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

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!

4) Preludes and codas:

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


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.

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.


Regarding your Edit

Maybe in your example (in your "Edit" paragraph), 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.

  • Access all the objects available to JSP pages.

  • 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.

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


Useful reading.


Conclusion

Use the right tools for each task.

Use Tag Files as a quick and easy way of creating custom tags that can help you encapsulate reusable content.

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

  • 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 包含指令包含文件、JSP 包含操作和使用 JSP 标记文件之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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