问题描述
在检索我需要使用的 cookie 时:
While retrieving cookies I need to use:
<c:forEach items="${cookie}" var="currentCookie">
${currentCookie.value.name} </br>
</c:forEach>
但是,在使用自定义数组时,为什么我们需要跳过 .value 函数?
But, while using custom arrays, why we need to skip the .value function?
<c:forEach items="${myList}" var="myList">
${myList.name} </br>
</c:forEach>
Cookie 包含一个 .getValue 函数(),它以字符串格式返回 cookie 的内容,那么使用 currentCookie.value.name 是如何工作的?
Cookie contains a .getValue function() which returns the content of the cookie in string format, so how does using currentCookie.value.name work?
推荐答案
${cookie}
指向一个 Map
使用 cookie 名称作为映射键和 Cookie
对象作为映射值.在 中对
Map
的每次迭代都会给你一个 Map.Entry
返回 getKey()
和 getValue()
方法.您的困惑是 Cookie
对象又也有一个 getValue()
方法.
The ${cookie}
points to a Map<String, Cookie>
with the cookie name as map key and the Cookie
object as map value. Every iteration over a Map
in <c:forEach>
gives you a Map.Entry
back which in turn has getKey()
and getValue()
methods. Your confusion is that the Cookie
object has in turn also a getValue()
method.
<c:forEach items="${cookie}" var="currentCookie">
Cookie name as map entry key: ${currentCookie.key}<br/>
Cookie object as map entry value: ${currentCookie.value}<br/>
Name property of Cookie object: ${currentCookie.value.name}<br/>
Value property of Cookie object: ${currentCookie.value.value}<br/>
</c:forEach>
这是一个 Map
因为它允许您在事先知道名称的情况下轻松直接访问 cookie 值.下面的例子假设它是 cookieName
:
It's a Map<String, Cookie>
because it allows you easy direct access to cookie value when you already know the name beforehand. The below example assumes it to be cookieName
:
${cookie.cookieName.value}
顺便说一下,您的列表示例无效.var
不应引用与列表本身相同的名称.
Your list example is by the way invalid. The var
should not refer the same name as the list itself.
这篇关于在 JSTL 标签中检索 cookie 和数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!