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?
aroth
source share