Struts2在页面中,取得Map的某一个key对应value值

本文介绍在Struts2框架中如何从Map集合中获取特定键对应的值,并提供了两种方法的具体实现步骤。同时展示了如何使用迭代器遍历Map中的所有键值对。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'index.jsp' starting page</title>
   
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->

  </head>
 
  <body>  
    Map 的取得某一个key的值, OK!<br/>
   
      参考:http://blog.csdn.net/hakunamatata2008/archive/2010/02/07/5295500.aspx
       
    This is my JSP page. <br/>
   
       方法一:<br/>
   直接取值的调用方式: <s:property value="powerMap['powerright_'+3]"/><br/>
   有变量直接取值的调用方式: <s:property value="powerMap['powerright_'+powerRightsVO.power]"/>
  
    方法二:<br/>
    <s:set name="myMapKey" value="'powerright_'+powerRightsVO.power"></s:set>
    <s:property value="#myMapKey"/> <br/>
    <s:property value="powerMap[#myMapKey]"/><br/>
   
   <s:if test="powerMap[#myMapKey] != null "> 
 我的值:<s:property value="powerMap[#myMapKey]" /><br/> 
 test时,不需要用 ${}
   </s:if> 
  
    <s:if test="powerMap[''+#myMapKey] != null "> 
 我的值:powerMap[''+#myMapKey] 也行!<br/>   
   </s:if> 
  
    <s:if test="powerMap[#myMapKey] == true"> 
 我的值为true:<s:property value="powerMap[#myMapKey]" /><br/> 
   </s:if>
     
    <br/>
    <s:iterator value="powerMap" id="column">  
•    <s:property value="#column"/><br> 
   <s:property value="#column.key"/><br> 
•     key: <s:property value="key"/><br>  
•        value:<s:property value="value"/><br> 
•  </s:iterator>  
   
    如果value是对象,直接再加点进行取得对应的属性。
   
  </body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值