JSP自定义标签:是否可以拥有多个启动/关闭标签?

时间:2021-10-22 09:28:42

After using the Django template language, I really miss being able to do things like this:

使用Django模板语言后,我真的很想能够做到这样的事情:

{% if condition %}
    <!-- snip -->
{% else %}
    <!-- snip -->
{% endif %}

When I am using JSP, I am stuck doing something like this:

当我使用JSP时,我很难做到这样的事情:

<logic:equal name="something" value="example">
    <!-- snip -->
</logic:equal>
<logic:notEqual name="something" value="example">
    <!-- snip -->
</logic:notEqual>

or:

<% if (condition) { %>
   <!-- snip -->
<% } else { %>
   <!-- snip -->
<% } %>

Is it possible to write a custom tag that supports else and else if, rather than simply having a pair of tags for each check?

是否可以编写支持else的自定义标记,否则,而不是简单地为每个检查添加一对标记?

If it's not possible, which is the "preferred" style? Scriptlets or multiple tag pairs? At my organization, most people seem to frown upon scriptlets, but I haven't really heard a good reason why simple conditional statements like the ones I've listed are so bad.

如果不可能,这是“首选”风格? Scriptlets或多个标签对?在我的组织中,大多数人似乎都不喜欢scriptlet,但我还没有真正理解为什么像我列出的简单条件语句那么糟糕的原因。

5 个解决方案

#1


The tags in XML come in pairs, one to open one to close. The three elements of the if then else end do not lend to a nice open and close format. The only other way is to use the choose tag as follows:

XML中的标签成对出现,一个用于打开一个要关闭的标签。 if then else结尾的三个元素不会提供一个好的打开和关闭格式。唯一的另一种方法是使用choose标记,如下所示:

<c:choose>
  <c:when test="${bean.value == 2}">
    <p>True</p>
  </c:when>
  <c:otherwise>
    <p>False</p>
  </c:otherwise>
</c:choose>

This is the usual way in which to code the if then else structures in jstl.

这是在jstl中编码if then else结构的常用方法。

#2


The JSTL tags come with a choose tag that works like a multi select.

JSTL标签带有一个选择标签,其作用类似于多选。

<c:choose>
    <c:when test="${first condition}">
       whatever
    </c:when>
    <c:when test="${second condition}">
       whatever
    </c:when>
    <c:when test="${third condition}">
       whatever
    </c:when>
    <c:otherwise>
          whatever else
    </c:otherwise>
 </c:choose>

#3


The above solutions will work (<c:choose> and <c:if>).

以上解决方案将起作用( )。

If you're interested in writing custom tags to do more or be more "domain specific", they're actually quite easy.

如果您有兴趣编写自定义标签来做更多或更具“特定领域”,那么它们实际上非常简单。

I did a presentation at JavaOne several years ago -- the slides are at http://javadude.com/articles/javaone/index.html (in the first section). There are details on how to write looping and conditional tags. (I did the presentation before the standard tag libs came out, btw)

几年前我在JavaOne上做了一个演示 - 幻灯片位于http://javadude.com/articles/javaone/index.html(第一部分)。有关如何编写循环和条件标记的详细信息。 (我在标准标签库出来之前做过演示,顺便说一句)

There's also a really good custom tag tutorial at http://www.orionserver.com/docs/tutorials/taglibs/index.html. It's got a few specifics for orion server, but most of it is very generic.

http://www.orionserver.com/docs/tutorials/taglibs/index.html上还有一个非常好的自定义标签教程。它有一些针对orion服务器的细节,但大多数是非常通用的。

#4


There are conditional tags in the standard tag libraries:

标准标记库中有条件标记:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:if ... >

</c:if>

lookup any refererence on jstl.

在jstl上查找任何引用。

You can also do this directly in the jsp, although it's a bit frowned upon:

您也可以直接在jsp中执行此操作,尽管有点不满意:

<% if (something) { %>
 ... this will only display if something is true ...
<% } >

#5


I don't see any reason why you couldn't write a custom JSP tag that knew to look for the tag in it's bodycontent. It wouldn't be a "best practice", but it would be a pretty clean and intuitive way of doing things.

我没有看到任何理由为什么你不能编写一个知道在其bodycontent中寻找标签的自定义JSP标签。这不是一个“最佳实践”,但它将是一种非常干净和直观的做事方式。

#1


The tags in XML come in pairs, one to open one to close. The three elements of the if then else end do not lend to a nice open and close format. The only other way is to use the choose tag as follows:

XML中的标签成对出现,一个用于打开一个要关闭的标签。 if then else结尾的三个元素不会提供一个好的打开和关闭格式。唯一的另一种方法是使用choose标记,如下所示:

<c:choose>
  <c:when test="${bean.value == 2}">
    <p>True</p>
  </c:when>
  <c:otherwise>
    <p>False</p>
  </c:otherwise>
</c:choose>

This is the usual way in which to code the if then else structures in jstl.

这是在jstl中编码if then else结构的常用方法。

#2


The JSTL tags come with a choose tag that works like a multi select.

JSTL标签带有一个选择标签,其作用类似于多选。

<c:choose>
    <c:when test="${first condition}">
       whatever
    </c:when>
    <c:when test="${second condition}">
       whatever
    </c:when>
    <c:when test="${third condition}">
       whatever
    </c:when>
    <c:otherwise>
          whatever else
    </c:otherwise>
 </c:choose>

#3


The above solutions will work (<c:choose> and <c:if>).

以上解决方案将起作用( )。

If you're interested in writing custom tags to do more or be more "domain specific", they're actually quite easy.

如果您有兴趣编写自定义标签来做更多或更具“特定领域”,那么它们实际上非常简单。

I did a presentation at JavaOne several years ago -- the slides are at http://javadude.com/articles/javaone/index.html (in the first section). There are details on how to write looping and conditional tags. (I did the presentation before the standard tag libs came out, btw)

几年前我在JavaOne上做了一个演示 - 幻灯片位于http://javadude.com/articles/javaone/index.html(第一部分)。有关如何编写循环和条件标记的详细信息。 (我在标准标签库出来之前做过演示,顺便说一句)

There's also a really good custom tag tutorial at http://www.orionserver.com/docs/tutorials/taglibs/index.html. It's got a few specifics for orion server, but most of it is very generic.

http://www.orionserver.com/docs/tutorials/taglibs/index.html上还有一个非常好的自定义标签教程。它有一些针对orion服务器的细节,但大多数是非常通用的。

#4


There are conditional tags in the standard tag libraries:

标准标记库中有条件标记:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:if ... >

</c:if>

lookup any refererence on jstl.

在jstl上查找任何引用。

You can also do this directly in the jsp, although it's a bit frowned upon:

您也可以直接在jsp中执行此操作,尽管有点不满意:

<% if (something) { %>
 ... this will only display if something is true ...
<% } >

#5


I don't see any reason why you couldn't write a custom JSP tag that knew to look for the tag in it's bodycontent. It wouldn't be a "best practice", but it would be a pretty clean and intuitive way of doing things.

我没有看到任何理由为什么你不能编写一个知道在其bodycontent中寻找标签的自定义JSP标签。这不是一个“最佳实践”,但它将是一种非常干净和直观的做事方式。