Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
292 views
in Technique[技术] by (71.8m points)

java - Retrieving cookie and array values in JSTL tags

While retrieving cookies I need to use:

<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?

<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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

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}

Your list example is by the way invalid. The var should not refer the same name as the list itself.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...