简体   繁体   English

在scriptlet中访问Java Map

[英]Accessing a Java Map inside a scriptlet

I've got a Java Map object imported into a JSP file that I'm working with. 我已经将Java Map对象导入到正在使用的JSP文件中。 What I need to do is take each entry of that map and put it into a table in the JSP. 我需要做的是获取该映射的每个条目,并将其放入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. 我不会表现出我对scriptlet的超级了解,但是据我所知,它们在运行之前都经过了评估,然后放入代码中,这使访问单个条目成为一个挑战。 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. 我目前正在通过在地图上执行toString然后使用正则表达式解析信息来使其工作,但我想更安全地进行操作。

How could this be achieved? 如何做到这一点?

Your assumptions about scriptlets being evaluated before running are wrong. 您关于在运行之前评估scriptlet的假设是错误的。 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. 本质上,所有不在scriptlet中的内容都会放入stream.write ,而所有-的内容都将保持不变。 In most application servers with JSP support you can even find those precompiled JSP servlets and see that generated code. 在大多数具有JSP支持的应用服务器中,您甚至可以找到那些预编译的JSP servlet,并查看生成的代码。 Also, this approach makes it insanely fast. 而且,这种方法使它疯狂地快。

So writing scriptlet would be no problem in your case. 因此,在您的情况下,编写scriptlet不会有问题。

I am assuming your Map is of Map<Integer,String>() type; 我假设您的Map是Map<Integer,String>()类型;

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

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

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

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