I've got a Java Map object imported into a JSP file that I'm working with. What I need to do is take each entry of that map and put it into a table in the JSP. I'm not going to act like I'm super knowledgeable about scriptlets, but from what I can tell they're all evaluated before running and then placed into the code, which makes accessing the individual entries a challenge. I've currently got it working by doing a toString on the map and then using regexs to parse out the info, but I want to do it more safely.

How could this be achieved?

有帮助吗?

解决方案 2

I am assuming your Map is of Map<Integer,String>() type;

<ul>
<%
for(Integer key : yourmap.keySet()) {
    String value = data.get(key);
    System.out.printf("%s = %s%n", key, value);

    %>
    <li><%= value%></li>
    <%
}
%>
</ul>

其他提示

Your assumptions about scriptlets being evaluated before running are wrong. Scriptlets work like this example:

<p> Hello world </p>
<% 
while (something) {
    doSomeStuff();
}
%>
<p> Bye </p>

is compiled to something like this:

stream.write("<p> Hello world </p>");
while (something) {
    doSomeStuff();
}
stream.write("<p> Bye </p>");

In essence, everything that's not in scriptlet is put into stream.write, and everything that is - is inserted with no changes. In most application servers with JSP support you can even find those precompiled JSP servlets and see that generated code. Also, this approach makes it insanely fast.

So writing scriptlet would be no problem in your case.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top