java 在 JSTL 标签中检索 cookie 和数组值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10883251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Retrieving cookie and array values in JSTL tags
提问by Akash
While retrieving cookies I need to use:
在检索 cookie 时,我需要使用:
<c:forEach items="${cookie}" var="currentCookie">
${currentCookie.value.name} </br>
</c:forEach>
But, while using custom arrays, why we need to skip the .value function?
但是,在使用自定义数组时,为什么我们需要跳过 .value 函数?
<c:forEach items="${myList}" var="myList">
${myList.name} </br>
</c:forEach>
Cookie contains a .getValue function() which returns the content of the cookie in string format, so how does using currentCookie.value.name work?
Cookie 包含一个 .getValue 函数(),它以字符串格式返回 cookie 的内容,那么使用 currentCookie.value.name 是如何工作的呢?
回答by BalusC
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 alsoa getValue()
method.
该${cookie}
指向Map<String, Cookie>
与cookie的名称作为地图键和Cookie
对象映射值。每次迭代通过Map
在<c:forEach>
给你一个Map.Entry
回这反过来又getKey()
和getValue()
方法。您的困惑是该Cookie
对象反过来也有一个getValue()
方法。
<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>
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
:
这是Map<String, Cookie>
因为当您事先知道名称时,它允许您轻松直接访问 cookie 值。下面的例子假设它是cookieName
:
${cookie.cookieName.value}
Your list example is by the way invalid. The var
should not refer the same name as the list itself.
顺便说一下,您的列表示例无效。本var
不应该是指相同的名称列表本身。