JSP expressions and dynamic attribute names

I am trying to do something similar in that it should be relatively simple and run into a wall.

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

I understand that there are more efficient ways of presenting this information, such as placing all selected identifiers in Map and checking for it, but let me assume that I do not have access to this approach for any reason.

So what I would like to do is iterate products and fix some markup only if the selectedProduct_... attribute is 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> 

But of course, this does not work, as he dies on ${! empty selectedProduct_${product.id}} ${! empty selectedProduct_${product.id}} .

What will work if I hardcode the product identifier in the expression, for example:

${! empty selectedProduct_17}

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

  • Define the correct selectedProduct_... value that will be used for each iteration in the forEach loop. Something simple, like <c:set var="key" value="selectedProduct_${product.id}"/> , will do this, except that I'm not sure how to then take the key and use it to get request attribute values ​​with this name (without cheating and executing Java literal code inside a <% %> ).
  • Get the value of the request attribute, whose name I defined in # 1. It seems to be the hard part.

Is it possible to use pure JSP / JSTL? I know that I could run some Java code inside <% %> to solve this problem, but it looks like it would be in extremely bad shape. Is there a more elegant solution?

+7
source share
1 answer

You can use implicit objects :

There are objects that allow access to various cloud variables described in the "Using Scope Objects" section.

  • pageScope: the names of the variables in the list of pages bound to pages to their values.
  • requestScope: variable names with queries bound to the request, to their values
  • sessionScope: matches the names of variables in the session pane with reference to their values.
  • applicationScope: application scope variable names associated with applications, their values
When an expression refers to one of these objects by name, the corresponding object is returned instead of the corresponding attribute. For example, $ {pageContext} returns a PageContext object, even if there is an existing pageContext attribute containing a different value.

So for example:

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

All Articles