Using JSTL "dynamic" parameter name
JSTL Code:
<c:forEach var = "currentUser" items = "${users}">
<c:out value = "${currentUser.userName}" /></p>
<c:if test = "${!empty param.${currentUser.id}}">
<p>Details</p>
</c:if>
</c:forEach>
I have a list of user objects and i display all of their names. If one wants to see the details of any user a request is sent t开发者_JAVA技巧o a servlet which will return a Attribute with the name id and a DetailUser Object
Servlet:
req.setAttribute(currentUSer.getId, userDetails);
The problem is that, now if have to use the value of "id" in the if test not as a parameter, but a parameter name instead. I've tried that EL in EL expression but I am really sure that it isn't allowed. Any other suggestions?
It seems to me that you're making it more complex than it could be.I would simply do
req.setAttribute("userDetails", userDetails);
and, in the JSP, test if the current user's ID is the same as the one being detailed:
<c:if test="${userDetails.id == currentUser.id}">
<p>Details</p>
</c:if>
BTW, param.foo returns the value of a request parameter, not the value of a request attribute. You could do what you wanted by using
<c:if test = "${!empty requestScope[currentUser.id]}">
The []
notation instead of the .
operator is the way to pass a dynamic name rather than a static one.
精彩评论