开发者

How to access map value when its key contains a dot in JSTL?

开发者 https://www.devze.com 2022-12-09 19:01 出处:网络
whe开发者_C百科n my Map contains key with dot in their name I cannot access the corresponding value directly with the usual code:

whe开发者_C百科n my Map contains key with dot in their name I cannot access the corresponding value directly with the usual code:

${recordForm.map['records.key']}

Is there a way to escape the dot? Or do I have to resort to loop through all values and check against the key? (I know the iteration works).

Thanks!


It should work. Your problem lies somewhere else. Either you aren't running the code you think you are, or you changed the original code "too much" for posting this question and it became correct by coincidence.

[Edit] As an anwer on your comment here below: it certainly works. I even created a quick-n-dirty SSCCE for you (quick-n-dirty as in: using scriptlets while you shouldn't be doing this in real -java code belongs in a java class):

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@page import="java.util.Map"%>
<%@page import="java.util.HashMap"%>

<%
    // NOTE: this code belongs (in)directly in a Servlet class.
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("foo.bar", "fubar");
    map.put("beh.moo", 1234567);
    request.setAttribute("map", map);
%>

<html>
    <head><title>test</title></head>
    <body>
        <p>Access map values by key: ${map['foo.bar']} ${map['beh.moo']}</p>

        <p>Iterate over map values:
            <c:forEach items="${map}" var="entry">
                <br>${entry.key} = ${entry.value}
            </c:forEach>
        </p>
    </body>
</html>

It works flawlessly.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号