JSP 自定义标签:是否可以有多个开始/结束标签?

发布于 2024-07-22 16:22:43 字数 775 浏览 8 评论 0 原文

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

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

当我使用JSP时,我被困在做这样的事情:

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

或者:

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

是否可以编写一个支持elseelse if,而不是简单地为每个检查添加一对标签?

如果不可能,哪种是“​​首选”风格? Scriptlet 或多标记对? 在我的组织中,大多数人似乎都不喜欢 scriptlet,但我还没有真正听到一个很好的理由来解释为什么像我列出的那样的简单条件语句如此糟糕。

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

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

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

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

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.

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

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

发布评论

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

评论(5

傲鸠 2024-07-29 16:22:43

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>

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

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:

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

挽容 2024-07-29 16:22:43

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>

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

<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>
陌伤浅笑 2024-07-29 16:22:43

上述解决方案将起作用(> 和 >)。

如果您有兴趣编写自定义标签来执行更多或更“特定于域”的操作,它们实际上非常简单。

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

http://www.orionserver.com/docs/tutorials/taglibs/index.html。 它有一些关于 orion 服务器的细节,但大部分都是非常通用的。

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)

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.

许一世地老天荒 2024-07-29 16:22:43

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

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

</c:if>

在 jstl 上查找任何参考。

您也可以直接在 jsp 中执行此操作,尽管这有点令人不悦:

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

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.

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

<% if (something) { %>
 ... this will only display if something is true ...
<% } >
秋凉 2024-07-29 16:22:43

我不明白为什么您不能编写一个知道在其正文内容中查找该标记的自定义 JSP 标记。 这不是一个“最佳实践”,但它是一种非常干净和直观的做事方式。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文