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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 02:58:11  来源:igfitidea点击:

Retrieving cookie and array values in JSTL tags

javajspjstl

提问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 Cookieobject as map value. Every iteration over a Mapin <c:forEach>gives you a Map.Entryback which in turn has getKey()and getValue()methods. Your confusion is that the Cookieobject 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 varshould not refer the same name as the list itself.

顺便说一下,您的列表示例无效。本var不应该是指相同的名称列表本身。