简体   繁体   English

当其键在JSTL中包含点时,如何访问映射值?

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

when my Map contains key with dot in their name I cannot access the corresponding value directly with the usual code: 当我的Map包含名称中带点的键时,我无法使用通常的代码直接访问相应的值:

${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): 我甚至为你创建了一个快速n-dirty SSCCE(快速n-dirty,如:使用scriptlet,而你不应该在真正的-java代码中属于java类):

<%@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. 它完美无瑕。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM