JSP 表达式语言和动态属性名称 [英] JSP expression language and dynamic attribute names

查看:24
本文介绍了JSP 表达式语言和动态属性名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做一些看起来应该相对简单的事情并且遇到了一些障碍.

I'm trying to do something that seems like it should be relatively straightforward and running into a bit of a wall.

假设我有一个产品列表,我将其公开为名称 products 下的请求属性.假设每个产品都有一个 id 字段,并且我还有一堆请求属性以 selectedProduct_ 的形式设置来指示哪些被选中.

Let's say I've got a list of products which I expose as a request attribute under the name products. Let's also say that each product has an id field, and that I also have a bunch of request attributes set in the form of selectedProduct_<product-id> to indicate which ones are selected.

我知道有更好的方法来表示这些信息,例如将所有选定的 id 放入一个 Map 并进行检查,但让我们假设我无法访问该方法不管什么原因.

I understand there are better ways to represent this information, such as placing all the selected ids into a Map and checking against that, but let's assume that I don't have access to that approach for whatever reason.

所以我想做的是迭代 products 并仅在为当前产品设置了 selectedProduct_... 属性时才发出一些标记.类似的东西:

So what I'd like to do is iterate products and emit some markup only if there is a selectedProduct_... attribute set for the current product. Something like:

<c:forEach var="product" items="${products}">
    <c:if test="${! empty selectedProduct_${product.id}}">
        <div class="productId">${product.id}</div>
    </c:if>
</c:forEach> 

但这当然行不通,因为它死在 ${!空 selectedProduct_${product.id}}.

But of course that doesn't work, as it dies on ${! empty selectedProduct_${product.id}}.

如果我将 product-id 硬编码到表达式中会起作用,例如:

What will work is if I hardcode the product-id into the expression, like:

<代码>${!空 selectedProduct_17}

...假设17"是有效的产品 ID.显然这不切实际,但希望它说明了我正在努力实现的目标.基本上我需要:

...assuming that '17' is a valid product id. Obviously that's not practical, but hopefully it illustrates what I'm trying to accomplish. Basically I need to:

  1. 确定用于 forEach 循环中每次迭代的正确 selectedProduct_... 值.像 <c:set var="key" value="selectedProduct_${product.id}"/> 这样简单的事情就可以做到这一点,除非我不确定如何使用 key 并使用它来获取具有该名称的请求属性的值(不作弊并在 <% %> 块中运行文字 Java 代码).
  2. 获取我在 #1 中确定的名称的请求属性的值.这似乎是棘手的部分.
  1. Determine the correct selectedProduct_... value to use for each iteration in the forEach loop. Something as simple as <c:set var="key" value="selectedProduct_${product.id}"/> would do this, except I'm not sure how to then take key and use it to get the value of the request attribute with that name (without cheating and running literal Java code inside a <% %> block).
  2. Get the value of the request attribute whose name I determined in #1. This seems to be the tricky part.

这可以使用纯 JSP/JSTL 吗?我知道我可以在 <% %> 中运行一些 Java 代码来解决这个问题,但这似乎是非常糟糕的形式.当然存在更优雅的解决方案吗?

Is this possible using pure JSP/JSTL? I know I could run some Java code inside of <% %> to solve this, but that seems like it would be in exceedingly bad form. Surely a more elegant solution exists?

推荐答案

您可以通过使用 隐式对象:

有些对象允许访问使用范围对象中描述的各种范围变量.

  • pageScope:将页面范围的变量名称映射到它们的值
  • requestScope:将请求范围的变量名称映射到它们的值
  • sessionScope:将会话范围的变量名称映射到它们的值
  • applicationScope:将应用程序范围的变量名称映射到它们的值
  • There are objects that allow access to the various scoped variables described in Using Scope Objects.

    • pageScope: Maps page-scoped variable names to their values
    • requestScope: Maps request-scoped variable names to their values
    • sessionScope: Maps session-scoped variable names to their values
    • applicationScope: Maps application-scoped variable names to their values

    例如:

    <c:set var="selectedProductAttrName" value="selectedProduct_${product.id}"/>
    ${requestScope[selectedProductAttrName]}
    

    这篇关于JSP 表达式语言和动态属性名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆