Getting cookie and array values ββin JSTL tags
When receiving cookies, I need to use:
<c:forEach items="${cookie}" var="currentCookie"> ${currentCookie.value.name} </br> </c:forEach>
But using custom arrays, why do we need to skip the .value function?
<c:forEach items="${myList}" var="myList"> ${myList.name} </br> </c:forEach>
A cookie contains a .getValue () function that returns the contents of a cookie in a string format, so how does currentCookie.value.name work?
${cookie}
points to Map<String, Cookie>
with the cookie name as the map key and Cookie
as the map value. Each iteration over Map
in <c:forEach>
gives you Map.Entry
back, in turn, getKey()
and getValue()
. Your confusion is that the Cookie
object, in turn, also has the 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>
This is a Map<String, Cookie>
because it allows you to easily access directly the cookie value if you already know this name in advance. The example below assumes it has a cookieName
:
${cookie.cookieName.value}
An example of your list, by the way, is invalid. var
must not refer to the same name as the list itself.