在JSTL标记中检索cookie和数组值 [英] Retrieving cookie and array values in JSTL tags

查看:103
本文介绍了在JSTL标记中检索cookie和数组值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在检索我需要使用的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< String,Cookie> 使用cookie名称作为映射键和 Cookie 对象作为地图值。在< c:forEach> 中的 Map 上的每次迭代都会为您提供 Map.Entry 返回 getKey() getValue() 方法。您的困惑是 Cookie 对象依次 a 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>

这是地图< String,Cookie> 因为它允许您在事先知道名称时轻松直接访问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和数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆