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

时间:2022-11-26 10:34:06

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

任何人都可以解释JSP自定义标记库和JSP 2标记文件背后的想法吗?

Are they just different ways to do the same thing?

他们只是以不同的方式做同样的事情吗?

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

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

2 个解决方案

#1


8  

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:

我<3标记文件,但JSTL的首席开发人员如果他们真的这么说就吸烟了。您无法将所有标记库标记重新编写为标记文件标记,原因很重要:标记文件无法执行以下操作:

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>

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

hello world 没问题,但

<%= helloWorld.toString()%>

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

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

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.

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

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

就个人而言,我的经验法则是:尽可能使用标记文件,即。每当你不需要JSP指令在标签内部工作时,因为它们的清洁度要高一百万倍,非程序员更容易使用,所以不需要tld(如果你将它们保存在一个单独的命名空间中你的标签库标签)。

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

但是如果您想在标记内部使用JSP内容,那么您唯一的选择就是标记库标记。希望有一天,JSP人员会发布一种在标记文件标记内进行JSP指令处理的方法,然后我们真的可以放弃旧的基于类的标记,但在此之前请不要尝试用标记文件做所有标记因为你很快就会被简化为为每一个逻辑部分制作自定义标签(因为这是在不使用JSP指令的情况下进行逻辑的唯一方法)。

#2


6  

Problems Developing Custom Tags

开发自定义标签的问题

Traditional custom tags require Java programming skills.

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

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.

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

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

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

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

在实现新的自定义标记时,我们可能希望使用JSP表达式语言或其他自定义标记。

The Solution JSP 2.0 Tag Files

解决方案JSP 2.0标记文件

  • Tag files are one of the important new concepts introduced with JSP 2.0.
  • 标记文件是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.
  • 标记文件是使用普通的JSP语法开发的,包括脚本元素,并且像任何其他自定义标记一样使用。

  • Each custom tag is a separate tag file.
  • 每个自定义标记都是单独的标记文件。

How Do Tag Files Differ?

标签文件有何不同之处?

Written using JSP syntax.

使用JSP语法编写。

  • Not all JSP directives are permitted in a tag file.
  • 并非所有JSP指令都允许在标记文件中使用。

  • Supported by new tag file specific directives, actions and implicit objects.
  • 由新的标记文件特定指令,操作和隐式对象支持。

Identified by either a .tag or .tagx suffix.

由.tag或.tagx后缀标识。

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.
  • JSTL的主要开发人员之一评论说她有时间,她会使用标签文件重写所有JSTL。

#1


8  

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:

我<3标记文件,但JSTL的首席开发人员如果他们真的这么说就吸烟了。您无法将所有标记库标记重新编写为标记文件标记,原因很重要:标记文件无法执行以下操作:

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>

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

hello world 没问题,但

<%= helloWorld.toString()%>

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

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

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.

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

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

就个人而言,我的经验法则是:尽可能使用标记文件,即。每当你不需要JSP指令在标签内部工作时,因为它们的清洁度要高一百万倍,非程序员更容易使用,所以不需要tld(如果你将它们保存在一个单独的命名空间中你的标签库标签)。

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

但是如果您想在标记内部使用JSP内容,那么您唯一的选择就是标记库标记。希望有一天,JSP人员会发布一种在标记文件标记内进行JSP指令处理的方法,然后我们真的可以放弃旧的基于类的标记,但在此之前请不要尝试用标记文件做所有标记因为你很快就会被简化为为每一个逻辑部分制作自定义标签(因为这是在不使用JSP指令的情况下进行逻辑的唯一方法)。

#2


6  

Problems Developing Custom Tags

开发自定义标签的问题

Traditional custom tags require Java programming skills.

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

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.

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

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

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

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

在实现新的自定义标记时,我们可能希望使用JSP表达式语言或其他自定义标记。

The Solution JSP 2.0 Tag Files

解决方案JSP 2.0标记文件

  • Tag files are one of the important new concepts introduced with JSP 2.0.
  • 标记文件是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.
  • 标记文件是使用普通的JSP语法开发的,包括脚本元素,并且像任何其他自定义标记一样使用。

  • Each custom tag is a separate tag file.
  • 每个自定义标记都是单独的标记文件。

How Do Tag Files Differ?

标签文件有何不同之处?

Written using JSP syntax.

使用JSP语法编写。

  • Not all JSP directives are permitted in a tag file.
  • 并非所有JSP指令都允许在标记文件中使用。

  • Supported by new tag file specific directives, actions and implicit objects.
  • 由新的标记文件特定指令,操作和隐式对象支持。

Identified by either a .tag or .tagx suffix.

由.tag或.tagx后缀标识。

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.
  • JSTL的主要开发人员之一评论说她有时间,她会使用标签文件重写所有JSTL。