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

查看:145
本文介绍了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_< product-id> ; 表示选择了哪些。

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.

所以我想做的是迭代产品并且只有在<$时才发出一些标记c $ c>为当前产品设置的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> 

但当然这不起作用,因为它死于 $ { ! empty 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:

$ {! empty 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. 确定要使用的正确 selectedProduct _... 值对于 forEach 循环中的每次迭代。像< c:set var =keyvalue =selectedProduct _ $ {product.id}/> 之类的东西会做到这一点,除非我不是确定如何获取 key 并使用它来获取具有该名称的request属性的值(不在< <>中运行文字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天全站免登陆