迭代JavaScript中存储在模型中的哈希图 [英] Iterate hashmap stored in Model in javascript

查看:63
本文介绍了迭代JavaScript中存储在模型中的哈希图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试通过以下方式传递 key 来迭代javascript函数中的地图时:

While trying to iterate a map in a javascript function by passing key as below:

        <html> <head> 
    <script type="text/javascript"> 
    function demo(tmp){ 
    <c:forEach var="user" items="${usermap}"> 
    <c:out value="${usermap.get(\"+'tmp'+\").name}"></c:out>    
    </c:forEach>    
    } 
    </script> 
<title>Insert title here</title> </head>
 <body> 
<h1 onclick="demo('user1')">User VO</h1> 
<c:forEach var="user" items="${usermap}"> 
 Key: ${user.key}  - Name: ${user.value.name} - Id: ${user.value.userid}<br/><br/> 
</c:forEach> 
</body> </html>

我得到的是空白值。但是当我硬编码key *** user1 ***的值时,它就可以工作。

I am getting blank value. But when I hard code the value of key***user1***, it works.

Servlet代码
protected void doGet(HttpServletRequest request,HttpServletResponse response )抛出ServletException,IOException {
// TODO自动生成的方法存根

Servlet Code protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub

    //System.out.println("in servlet doGet:"+ ++count);
    UserVO user1= new UserVO("Y","701");
    UserVO user2= new UserVO("D","834");


    hmap.put("user1", user1);
    hmap.put("user2", user2);
    //hmap.values()
    request.setAttribute("usermap", hmap);
    //response.sendRedirect("User.jsp");
    RequestDispatcher view = request.getRequestDispatcher("User.jsp");
    view.forward(request, response);
    //response.getWriter().append("Served at: ").append(request.getContextPath());
}

有人可以在这里帮助我吗?

Can somebody help me here?

推荐答案

问题:

您正在混合客户端和服务器端代码。 JavaScript和JavaServer Pages分别执行。

You are mixing client side and server side code. JavaScript and JavaServer Pages are executed separately.


  • JSP代码是在服务器上编译的,

  • 结果是HTML,已交付到浏览器

  • 在浏览器中执行JavaScript

因此JSP相关的东西如下:

So JSP related stuff like:


  • JSTL,JSP标准标记库(诸如< jsp:something> 之类的标记< c:something>

  • JSP-EL,表达式语言(字符串,如 $ {something}

  • JSTL, JSP Standard Tag Library (tags like <jsp:something> <c:something>)
  • JSP-EL, Expression Language (strings like ${something})

在服务器上处理。

通过在Firefox / Chrome中按 Ctrl + U ,您可以看到在浏览器中接收到了什么HTML代码。

You can see, what HTML code is received in browser, by pressing Ctrl+U in Firefox/Chrome.

在您的情况下:

在select标签中选择一个选项是在浏览器的客户端上执行。

对于EL评估为时已晚。 EL已对bean进行了评估。

您可以使用ajax根据用户选择请求所需的数据(映射)。

Selecting an option in the select-tag is executed on the client side in browser.
It is too late for EL-evaluation. The EL has bean already evaluated.
You can use ajax to request the needed data (map) according the user selection.

编辑

硬编码行的EL在服务器上执行并替换为值。在另一种情况下,当涉及到选择标签时,EL在服务器上执行,并由 var Cfs_id =; 代替。因此,虚拟函数将忽略参数serviceId并将变量始终设置为空字符串。

The EL of the hardcoded line is executed on the server and replaced with the value. On the other case, when select-tag is involved, the EL was executed on the server and replaced by var Cfs_id ="";. So the dummy function ignore the parameter serviceId and set the variable always to empty string.

在浏览器中查找代码。只有HTML和Javascript。 JSP EL不再存在。

Look the code in your browser. There is only Html and Javascript. The JSP EL are no more there.

这篇关于迭代JavaScript中存储在模型中的哈希图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆