JSP 自定义标记库与 JSP2 标记文件

发布于 2024-07-08 07:10:14 字数 106 浏览 6 评论 0原文

谁能解释 JSP 自定义标记库和 JSP 2 标记文件背后的想法吗?

它们只是做同一件事的不同方法吗?

他们如何比较? 它们各有什么优点和缺点,哪个更好?

Can anybody explain the idea behind JSP custom tag libraries and the JSP 2 tag files?

Are they just different ways to do the same thing?

How do they compare? What are their pros and cons, and which is better?

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

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

发布评论

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

评论(2

温柔少女心 2024-07-15 07:10:14

我 <3 个标记文件,但是 JSTL 的首席开发人员如果真的这么说的话,那就是吸毒了。 你不能将所有标签库标签重写为标签文件标签,有一个非常重要的原因:标签文件做不到:

return EVAL_BODY_INCLUDE;

换句话说,标记文件的正文内容只有三个选项:

空:没有内部内容,即。 <一些标签/>

无脚本:没有JSP内部内容,即。

你好世界

可以,但不行

<%= helloWorld.toString() %>

tagdependent:你可以有JSP内部内容,但它不会被这样处理; 相反,您必须以您认为合适的方式解析/渲染它

但是使用旧样式标签库标签,您可以拥有:
<正文内容>JSP
(在 tld 文件中)然后“return EVAL_BODY_INCLUDE;” 来自您的“doStartTag”。 如果这样做,所有 JSP 指令都将被解析,就像它们是页面的正常部分一样,并且您的标记只是用适当的内容包装它们。

就我个人而言,我的经验法则是:尽可能使用标签文件,即。 每当您不需要 JSP 指令在标记内工作时,因为它们干净一百万倍,对于非程序员来说更容易使用,不需要 tld(好吧,如果您将它们保存在与您的标签库标签)。

但是,如果您希望标记中包含 JSP 内容,则唯一的选择是标记库标记。 希望有一天,JSP 人们会发布一种在标记文件标记内进行 JSP 指令处理的方法,然后我们真的可以放弃旧的基于类的标记,但在那之前,请不要尝试使用标记文件处理所有标记,因为您很快就会被迫为最后的每一段逻辑创建自定义标记(因为这是在不使用 JSP 指令的情况下执行逻辑的唯一方法)。

I <3 tag files, but that lead developer of JSTL is smoking crack if they really said that. You CANNOT re-write all tag library tags as tag file tags, for one very important reason: tag files can't do:

return EVAL_BODY_INCLUDE;

In other words, tag files only have three options for their body-content:

empty: no inner content, ie. <someTag/>

scriptless: no JSP inner content, ie. <someTag><p>hello world</p></someTag> is ok, but not <someTag><p><%= helloWorld.toString() %></p></someTag>

tagdependent: you can have JSP inner content, but it won't be processed as such; instead you have to parse/render it however you see fit

But with the old style tag library tags, you can have:
<body-content>JSP</body-content>
(in the tld file) and then "return EVAL_BODY_INCLUDE;" from your "doStartTag". If you do this, all of your JSP directives will get parsed just as if they were a normal part of your page, and your tag simply wraps them with the appropriate content.

Personally, my rule of thumb is: use tag files whenever you can, ie. whenever you don't need JSP directives to work inside the tag, because they are a million times cleaner, easier for a non-programmer to work with, don't require a tld (well, if you keep them in a seperate namespace from your tag library tags).

But if you want JSP content inside your tag, your only option is tag library tags. Hopefully, someday, the JSP people will release a way to do JSP directive processing inside a tag file tag, and then we really can abandon the old class-based tags, but until then please don't try to do all tags with tag files, as you'll quickly be reduced to making custom tags for every last piece of logic (since that's the only way to do logic without using JSP directives).

别念他 2024-07-15 07:10:14

开发自定义标签时遇到的问题

传统的自定义标签需要Java编程
技能。

除了最简单的自定义标签之外的所有自定义标签都不容易编写。

与 servlet 相比,JSP 的目的是使用
用于管理嵌入式布局的标记语言
动态内容。

必须编写复杂的Java代码
在专注于标记语言的自定义标签中
向后。

我们可能想使用 JSP 表达式语言或
实现新自定义时的其他自定义标签
标签。

解决方案
JSP 2.0 标记文件

  • 标记文件是 JSP 2.0 引入的重要新概念之一。
  • 标签文件允许更轻松、更快速地开发自定义标签。
  • 标记文件是使用普通 JSP 语法(包括脚本元素)开发的,并且像任何其他自定义标记一样使用。
  • 每个自定义标签都是一个单独的标签文件。

标记文件有何不同?

使用 JSP 语法编写。

  • 并非所有 JSP 指令都允许出现在标记文件中。
  • 由新标记文件特定指令、操作和隐式对象支持。

.tag.tagx 后缀标识。

旨在为自定义标签开发人员提供轻松开发而不损失功能。

  • JSTL 的一位主要开发人员评论说,如果她有时间,她会使用标记文件重写所有 JSTL。

Problems Developing Custom Tags

Traditional custom tags require Java programming
skills.

All but the simplest custom tags are not easy to write.

The purpose of JSP, in contrast to servlets, is to use
markup language to manage layout with embedded
dynamic content.

Having to write complex Java code
in custom tags that focus on markup language is going
backwards.

We might want to use the JSP expression language or
other custom tags when implementing a new custom
tag.

The Solution
JSP 2.0 Tag Files

  • Tag files are one of the important new concepts introduced with JSP 2.0.
  • Tag files permit easier and more rapid development of custom tags.
  • Tag files are developed using normal JSP syntax,including scripting elements, and used just like any other custom tag.
  • Each custom tag is a separate tag file.

How Do Tag Files Differ?

Written using JSP syntax.

  • Not all JSP directives are permitted in a tag file.
  • Supported by new tag file specific directives, actions and implicit objects.

Identified by either a .tag or .tagx suffix.

Intended to provide custom tag developers ease of development without loss of functionality.

  • One of the lead developers of JSTL has commented thatif she had the time, she would rewrite all of JSTL using tag files.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文