简体   繁体   English

如何遍历地图和struts 2中的地图值

[英]how to iterate over a map and map values in struts 2

i need help here in resolving an issue. 我需要在这里解决问题。 I am migrating an existing struts2 based web app to spring boot, I have managed to do that however the below piece of code is not working as expected. 我正在将现有的基于struts2的Web应用程序迁移到Spring Boot,我设法做到了,但是下面的代码无法正常工作。 Any help would be appreciated. 任何帮助,将不胜感激。

<s:iterator value="{#session['user'].menu}" id = "item" status="rowStatus">
  <div class='menucontent_bg' id ='menumenucontent_<s:property value="#rowStatus.index"/>'>
    <s:iterator value="value" id = "menu"> 
      <div class=menucontent_heading>
        <a href='javascript:createTab("<s:property value ="#menu.menuOption"/>", <s:property value ="#menu.menuUrl"/>");'>

          <s:property value ="#menu.menuOption"/>
        </a>
      </div>
    </s:iterator>
  </div>
</s:iterator>

In the above code iteration is taking place as expected but the javascript method call to createTab is being done with empty parameters which means #menu.menuOption and other parameter passed is returning empty string, which 在上述代码中,迭代按预期进行,但是对createTab的javascript方法调用是使用空参数完成的,这意味着#menu.menuOption和其他传递的参数将返回空字符串,

I am not sure why as I verified with a simple scriptlet and the values are not empty and also the same code is there in production. 我不确定为什么要用简单的scriptlet进行验证并且值不为空并且生产中也使用相同的代码。 I upgraded struts version to 2.5.1 for migration purpose. 我将struts版本升级到2.5.1以便进行迁移。

<a 
  href='javascript:createTab(". <s:property value ="#menu.menuOption"/>", 
        <s:property s:property value ="#menu.menuUrl"/>");'>

This is (at the very least): 这是(至少):

  • Missing a quote 缺少报价
  • Has a spurious s:property in the middle of the second parameter 在第二个参数中间有一个伪造的s:property

It's almost always better, and clearer, to use intermediate values and/or functions to do work like this, eg, move the JS into a support function, so you can read your own code. 使用中间值和/或函数来做这样的工作几乎总是更好,更清楚的,例如,将JS移入支持函数,以便您可以阅读自己的代码。

I finally managed to find a solution. 我终于设法找到了解决方案。 the problem was not just with iterating over a map but was with all the iteration related code in my project. 问题不仅仅在于遍历地图,还在于项目中所有与迭代相关的代码。 So i searched with different options and finally got the answer in the link below 所以我搜索了不同的选项,最后在下面的链接中得到了答案

How do you iterate through a list of objects? 您如何遍历对象列表?

with struts 2.2.x id attribute has been deprecated and replaced with var for the iterator tag. 带有struts 2.2.x的id属性已被弃用,并用var替换为iterator标记。 so now the working code is as below 所以现在的工作代码如下

               <s:iterator value="{#session['user'].menu}" var= "item" status="rowStatus">
              <div class='menucontent_bg' id 
                     ='menumenucontent_<s:property 
                           value ="#rowStatus.index"/>'>
                     <s:iterator value="value" var = "menu"> 
                     <div class=menucontent_heading>
                        <a href='javascript:createTab("<s:property         value ="#menu.menuOption"/>", <s:property value 
                                                  ="#menu.menuUrl"/>");'>

                           <s:property value ="#menu.menuOption"/>
                       </a>
                    </div>
                    </s:iterator>
              </div>
              </s:iterator>

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

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