如何在 JSP 中创建宏/片段/HTML 片段/命名部分而不使用标签/包含/JSPFragments?

发布于 2024-08-20 19:30:21 字数 799 浏览 3 评论 0原文

这可能是一个非常新手的问题,但我没有找到任何令人满意的东西,

我想在 JSP 中做这样的事情(最好是开箱即用的):

例如,在一个名为 products.jsp 的文件中,一个虚构的实现解释了我想要的内容

<x:named-segment name="product">
     Product: <strong>${product.name}</strong> <br/>
     price: ${product.price}
</x:named-segment>

,稍后在定义的相同 JSP 中的不同位置使用它

<table>
   <c:forEach var="product" items="${products}">
      <tr>
         <td><x:use-segment name="product"/></td>
      </tr>
   </c:forEach>
</table>

我已经研究了 JSP 标签JSP 片段< /strong>,但是片段片段只是从调用者 JSP 传递到 JSP 标记,我希望它位于相同的位置

唯一的解决方案是为该特定的小片段创建一个 JSP 标记(或包含? )

我错过了一些非常基本的东西吗?

This might be a very newbie question, but I didn't find anything satisfying

I want to do somethign like this in JSP (out of the box preferably):

e.g. in a file called products.jsp an imaginary implementation that explains what I want

<x:named-segment name="product">
     Product: <strong>${product.name}</strong> <br/>
     price: ${product.price}
</x:named-segment>

and later on use this in various location in the same JSP it is defined

<table>
   <c:forEach var="product" items="${products}">
      <tr>
         <td><x:use-segment name="product"/></td>
      </tr>
   </c:forEach>
</table>

I've looked into JSP tags, and JSP Fragements, but there the fragment snippet is just passed from the caller JSP to the JSP tag, and I want it to be in the same location

Is the only solution is to craete a JSP tag for that specific small snippet (or include?)

Am I missing something very basic?

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

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

发布评论

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

评论(2

诗化ㄋ丶相逢 2024-08-27 19:30:21

如果您在很多地方想要的一小段文本是静态的,我会推荐 JSP include。
但是,如果文本来自数据库/平面文件/XML,我建议使用自定义标签。
从您提供的示例来看,您似乎正在尝试列出产品及其价格。这可以在自定义标签中轻松完成。

在标签类中,读取数据,创建一个方法来为数据创建 HTML 标签并作为字符串返回,然后打印该字符串。
现在,在 JSP 中,在需要文本的任何位置调用自定义标记。
当然,您需要参数化标签以确定在什么位置获取/显示什么。

HTV

If the small piece of text that you want at many places is static, I would recommend a JSP include.
However, if the text is from database/Flat file/XML, I would recommend using a custom tag.
From the example you've provided, it appears as though you are trying to list products and their price. This can be easily accomplished in a custom tag.

In your tag class, read the data, create a method that will create the HTML tags for the data and return as a string, print the string.
Now in your JSP, invoke the custom tag wherever you need the text.
Ofcourse you need to parameterize the tag to determine what to fetch/display at what place.

HTH

V

青朷 2024-08-27 19:30:21

我感受到你的痛苦@EranMedan,仍然不敢相信这不是 JSP 的一个功能。经过多年的渴望,我在这里编写了自己的简单解决方案来完成您(和我)想要的事情:
https://stackoverflow.com/a/25575120/1607642

I feel your pain @EranMedan , still can't believe this isn't a feature of JSP. After years of wanting it, I wrote my own simple solution here to do what you (and I) want:
https://stackoverflow.com/a/25575120/1607642

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