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

when my card contains a key with a dot in its name, I cannot access the corresponding value directly using regular code:

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

Is there a way to avoid the point? Or do I need to resort to a loop through all the values ​​and check the key? (I know that iteration works).

Thanks!

+3
source share
1 answer

It should work. Your problem lies elsewhere. Either you don’t use the code that you think you have, or you changed the source code too much to post this question, and it became correct by coincidence.

[] : , , . -n- SSCCE ( n-, : , -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>

.

+4

Source: https://habr.com/ru/post/1721195/


All Articles