使用 JSTL“动态”参数名称
JSTL 代码:
<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>
我有一个用户对象列表,并显示它们的所有名称。如果想要查看任何用户的详细信息,则会将请求发送到 servlet,该 servlet 将返回一个名为 id 的属性和一个 DetailUser 对象
Servlet:
req.setAttribute(currentUSer.getId, userDetails);
问题是,现在如果必须在 Servlet 中使用“id”的值if test 不是作为参数,而是作为参数名称。我已经尝试过 EL in EL 表达式,但我确信这是不允许的。还有其他建议吗?
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 to 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在我看来,你让它变得比它可能的更复杂。我会简单地做
,并在 JSP 中测试当前用户的 ID 是否与详细的用户 ID 相同:
顺便说一句,param.foo 返回值请求参数的值,而不是请求属性的值。
您可以通过使用
[]
表示法而不是.
运算符来执行您想要的操作,这是传递动态名称而不是静态名称的方法。
It seems to me that you're making it more complex than it could be.I would simply do
and, in the JSP, test if the current user's ID is the same as the one being detailed:
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
The
[]
notation instead of the.
operator is the way to pass a dynamic name rather than a static one.