简体   繁体   English

在Struts2 JSP中获取Map的价值

[英]Getting the value of a Map in Struts2 JSP

I am loading a map of values into ServletContext during start of the application/server 我在应用程序/服务器启动期间将值的映射加载到ServletContext中

HashMap<String, List<String>> cfsUfsMap = new HashMap<String, List<String>>();
//some operations
application.setAttribute("UDM_UFS_CFS_MAP", cfsUfsMap); //application = ServletContext

I need to use this map directly in JSP page, for which I have done this 我需要直接在JSP页面中使用此地图,为此

<s:set name="udm_cfs_ufs_map" value="#application.UDM_UFS_CFS_MAP" />
<table class="first" width="100%" border="0" cellpadding="0" id="sups_assignedservices_info_table">
<tr>
    <th width="30%">Assigned service name </th>
    <th width="15%">CFS Code </th>
    <th width="15%">Status </th>
    <th width="20%">Date </th>
    <th width="20%">UDM </th>
</tr>
<s:iterator value="#sups_services.services">
    <s:set name="ufs_list" value="#udm_cfs_ufs_map.['top.code']" /> 
    <tr>                
        <td class="light"><s:property value="top.name"/> </td>
        <td class="light"><s:property value="top.code"/> </td>
        <td class="light"><s:property value="top.status"/> </td>
        <td class="light"><s:property value="top.date"/> </td>  
        <td class="light"><s:property value="#udm_cfs_ufs_map.size()" /> - <s:property value="#ufs_list.size()" /></td>
    </tr>   
</s:iterator>

As you see, I am trying to get the values (List) from map using key which's top.code However I am getting original map size but not the list size based on key. 如您所见,我正在尝试使用top.code的键从地图中获取值(List),但是我正在获取原始地图大小,而不是基于键的列表大小。

Any idea what's missing/going wrong 任何想法都丢失/出了什么问题

Done. 完成。

I solved it myself. 我自己解决了。 I am posting my mistakes and the correct solution. 我正在发布我的错误和正确的解决方案。 SO down the line it might be useful to someone 所以线下可能对某人有用

to access servletContext attributes 访问ServletContext属性

<s:set name="udm_cfs_ufs_map" value="#application.UDM_UFS_CFS_MAP" />

for fetching the content from the map based on key 用于根据密钥从地图中获取内容

<s:iterator value="#sups_services.services">
<s:set name="ufs_list" value="#udm_cfs_ufs_map[top.code]" />
<tr>
    <td class="light"><s:property value="top.name" /></td>
    <td class="light"><s:property value="top.code" /></td>
    <td class="light"><s:property value="top.status" />
    </td>
    <td class="light"><s:property value="top.date" /></td>
    <td class="light"><s:iterator value="ufs_list">
            <s:property />
            <br />
        </s:iterator></td>
</tr>

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

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