`

Struts2中iterator标签遍历map总结

 
阅读更多
 package com.zx.demo.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionSupport;
import com.zx.demo.model.Product;
import com.zx.demo.model.Student;

public class MapAction extends ActionSupport
{

    private Map<String,String> map;
   
	private Map<String,Student> studentMap;
	
	private Map<String,String[]> arrayMap;
	
	private Map<String,List<Student>> listMap;
	


	public String testMap()
	{
		map=new HashMap<String,String>();
		map.put("1", "one");
		map.put("2", "two");
		
		studentMap=new HashMap<String,Student>();
		studentMap.put("student1",new Student(new Long(1),"20034140201","张三1","男",25));
		studentMap.put("student2",new Student(new Long(2),"20034140202","张三2","女",26));
		studentMap.put("student3",new Student(new Long(3),"20034140202","张三3","男",27));
		
		arrayMap=new HashMap<String,String[]>();
		arrayMap.put("arr1", new String[]{"1","2003401","leejie","male","20"});
		arrayMap.put("arr2", new String[]{"2","2003402","huanglie","male","25"});
		arrayMap.put("arr3", new String[]{"3","2003403","lixiaoning","male","21"});
		
		
		
		listMap=new HashMap<String,List<Student>>();
		
		List<Student> list1=new ArrayList<Student>();
		list1.add(new Student(new Long(1),"20034140201","张三1","男",25));
		list1.add(new Student(new Long(2),"20034140202","张三2","男",25));
		list1.add(new Student(new Long(3),"20034140203","张三3","男",25));
		listMap.put("class1", list1);
		
		List<Student> list2=new ArrayList<Student>();
		list2.add(new Student(new Long(1),"20034140301","李四1","男",20));
		list2.add(new Student(new Long(2),"20034140302","李四2","男",21));
		list2.add(new Student(new Long(3),"20034140303","李四3","男",22));
		list2.add(new Student(new Long(4),"20034140304","李四4","男",23));
		listMap.put("class2", list2);
		 
		
		
		
		return SUCCESS;
		
	}

	
	
	public Map<String, String> getMap() {
		return map;
	}

	public void setMap(Map<String, String> map) {
		this.map = map;
	}
	
	public Map<String, Student> getStudentMap() {
		return studentMap;
	}

	public void setStudentMap(Map<String, Student> studentMap) {
		this.studentMap = studentMap;
	}


	public Map<String, String[]> getArrayMap() {
		return arrayMap;
	}


	public void setArrayMap(Map<String, String[]> arrayMap) {
		this.arrayMap = arrayMap;
	}



	public Map<String, List<Student>> getListMap() {
		return listMap;
	}



	public void setListMap(Map<String, List<Student>> listMap) {
		this.listMap = listMap;
	}
	
	
}

 页面

 <%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>struts2中的map遍历总结</title>
</head>
<body>
   <b>1.map中的value为String字符串</b><br>
   <s:iterator value="map" id="column">
   <s:property value="#column"/><br>
   key: <s:property value="key"/><br>
   value:<s:property value="value"/><br>
   ******************************************<br>
  </s:iterator>
 
 
  <b>2.map中的value为Student对象</b>
  <table border="1" width="50%"  cellspacing="0" cellpadding="0">
    <tr>
      <td>key=value</td>
      <td>ID</td>
      <td>num</td>
      <td>name</td>
      <td>sex</td>
      <td>age</td>
    </tr>
    <s:iterator value="studentMap" id="column">
    <tr>
     <td><s:property value="#column"/></td>
     <td><s:property value="value.id"/></td>
     <td><s:property value="value.num"/></td>
     <td><s:property value="value.name"/></td>
     <td><s:property value="value.sex"/></td>
     <td><s:property value="value.age"/></td>
    </tr>
    </s:iterator>
  </table>
  <p>
  
  
  <b>3.map中的value为String数组</b>
  <table border="1" width="50%"  cellspacing="0" cellpadding="0">
    <tr>
      <td>key=value</td>
      <td>ID</td>
      <td>num</td>
      <td>name</td>
      <td>sex</td>
      <td>age</td>
    </tr>
    <s:iterator value="arrayMap" id="column">
    <tr>
     <td><s:property value="#column"/></td>
     <td><s:property value="value[0]"/></td>
     <td><s:property value="value[1]"/></td>
     <td><s:property value="value[2]"/></td>
     <td><s:property value="value[3]"/></td>
     <td><s:property value="value[4]"/></td>
    </tr>
    </s:iterator>
  </table>
  <p>
  <b>4.map中的value为list集合</b>
  <table border="1" width="50%"  cellspacing="0" cellpadding="0">
    <tr>
      <td>class</td>
      <td>ID</td>
      <td>num</td>
      <td>name</td>
      <td>sex</td>
      <td>age</td>
    </tr>
    
   <s:iterator value="listMap" id="column">
     <s:set name="total" value="#column.value.size"/>
     <s:iterator value="#column.value" status="s">
      <tr>
        <s:if test="#s.first"><td rowspan="${total}"><s:property value="#column.key"/></td></s:if>
        <td><s:property value="id"/></td>
        <td><s:property value="num"/></td>
        <td><s:property value="name"/></td>
        <td><s:property value="sex"/></td>
        <td><s:property value="age"/></td>
      </tr>
     </s:iterator>
  </s:iterator>
 </table>
  
  
</body>
</html>
分享到:
评论

相关推荐

    Struts2 iterator 标签遍历 Map,List,数组(三十六)

    在Struts2中,`iterator`标签是用于遍历集合数据的重要工具,它可以用来迭代Map、List和数组等数据结构,以便在视图层进行展示。本篇文章将深入探讨`iterator`标签在遍历这些数据类型时的具体用法。 首先,我们来看...

    struts2中的map遍历

    Struts2是一个非常流行的...在Struts2中,我们可以使用OGNL(Object-Graph Navigation Language)表达式语言来遍历Map。 在Struts2中,Map遍历通常在JSP页面上进行,使用OGNL表达式。以下是一个简单的例子: ```jsp ...

    struts2中iterator 标签的使用详解

    ### Struts2中Iterator标签的深入解析与应用 在Struts2框架中,`&lt;s:iterator&gt;`标签是一个非常强大的工具,用于在JSP页面上循环遍历集合数据,如列表(List)、数组、Map等。它允许开发者以一种动态且灵活的方式展示...

    Struts2 JSP中将list,set ,Map传递到Action然后<s:iterator>遍历(三十五)

    在Struts2框架中,开发Web应用时常常需要在JSP页面与Action之间传递数据,以...通过以上步骤,我们可以高效地在Struts2中处理并展示集合数据。理解和掌握这一流程,对于提升Web应用的开发效率和质量有着显著的帮助。

    Struts iterator JSP Map 中嵌套 Map

    `&lt;s:iterator&gt;`是Struts2中的一个标签,用于迭代集合或者数组中的元素。在处理Map时,我们可以使用这个标签来迭代键值对,并在JSP页面上显示它们。 在描述中提到的"嵌套Map"是指Map的值本身还是另一个Map。这种结构...

    struts2遍历集合

    `s:iterator`标签是Struts2中最常用的遍历集合的标签。它的主要属性包括: - **value**:指定要遍历的集合,可以是数组、列表或Map类型的对象。 - **id**:为每个遍历元素设置一个临时变量名。 - **status**:提供...

    iterator嵌套,struts2

    在Struts2框架中,`iterator`标签是一个非常重要的组件,用于遍历各种集合对象,如List、Map等。在上述描述中,开发者遇到了一个关于`iterator`标签嵌套使用的问题,涉及到`LinkedHashMap`存储的数据结构。让我们...

    计算机遍历Map集合.pdf

    遍历Map集合是常见的操作,可以用于打印、处理或检查Map中的数据。下面我们将详细讲解几种遍历Map集合的方法。 首先,我们来看普通Java类中的遍历方式: 1. **遍历键集合**:Map集合提供了keySet()方法,返回一个...

    对 Struts 2 的 s: iterator tag 中嵌套的所有 s: checkbox 进行全选、反选操作

    `s:iterator`标签是Struts 2 提供的一个迭代标签,它允许开发者遍历任何可迭代的对象,如数组、集合或Map。在描述中提到的场景,这个标签用于遍历一个数据集,然后为每个元素创建一个`&lt;s:checkbox&gt;`标签。 `s:...

    struts2标签使用例子

    在Struts2框架中,标签库是其一大特色,它提供了丰富的自定义标签,使得开发者能够更加便捷地创建动态页面。这些标签极大地简化了JSP页面的编写,提高了代码的可读性和可维护性。 1. **Struts2核心标签库**: - `s...

    Struts2 使用OGNL遍历map方法详解

    在Struts2中,OGNL(Object-Graph Navigation Language)是默认的表达式语言,用于处理数据绑定、属性访问以及视图层的数据呈现。本文将深入讲解如何使用OGNL遍历Map对象,以展示Struts2在处理复杂数据结构时的灵活...

    详解Struts2标签遍历

    Struts2标签遍历详解 Struts2标签遍历是Struts2框架中的一个重要组件,用于在JSP页面中遍历集合中...总结,Struts2标签遍历提供了多种方式来遍历集合中的元素,并提供了多种属性来获取当前迭代的索引信息和元素信息。

    struts2迭代 Map List

    `&lt;s:iterator&gt;`标签是Struts2中最常用的标签之一,它用于迭代任何可迭代的对象,包括List和Map。基本语法如下: ```jsp &lt;s:iterator value="collection" var="item"&gt; &lt;!-- 迭代体 --&gt; &lt;/s:iterator&gt; ``` ...

    Struts2中ognl遍历数组,list和map方法详解

    在Struts2中,也可以使用 `&lt;s:iterator /&gt;` 标签遍历Map对象。Map对象中有几对key-value就迭代几次,分别使用 `&lt;s:property value="key"/&gt;` 和 `&lt;s:property value="value"/&gt;` 。 四、iterator的value iterator的...

    struts2的标签应用

    在Struts2中,标签库是其核心特性之一,它们为开发者提供了便利的方式来构建动态用户界面,无需深入HTML和JavaScript的底层细节。在本篇中,我们将深入探讨Struts2的标签应用及其在实际开发中的使用。 首先,了解...

    struts2中siterator 标签的使用详解 及 OGNL用法

    在 Struts2 中,`siterator` 标签是一种非常实用的标签,它允许开发者遍历各种类型的集合,如数组、列表、Map 等,并进行迭代操作。同时,OGNL (Object-Graph Navigation Language) 是 Struts2 中的表达式语言,用于...

    struts2标签大全

    10. **s:iterator标签**:用于迭代集合,常用于遍历列表、数组或Map,可以在循环内部嵌套其他Struts2标签。 11. **s:property标签**:用于显示Action类属性的值,可以格式化输出,支持EL表达式。 12. **s:debug...

    struts2.0常用标签文档

    4. **遍历集合**:`&lt;s:iterator&gt;`是Struts2中非常实用的标签,用于遍历集合,如List、Set、Map等。通过`value`属性设置遍历的对象,`var`属性定义迭代变量名,`status`属性可以获取当前迭代的状态信息。 5. **条件...

    struts2标签详解与实例

    本文将深入探讨Struts2中的标签及其使用实例。 一、Struts2标签库概述 Struts2标签库是基于JSP标准标签库(JSTL)的扩展,它包含了一系列用于构建动态Web应用的标签。这些标签分为两类:核心标签和主题标签。核心...

    struts2中siterator 标签的使用详解 及 OGNL用法.pdf

    在Struts2中,`s:iterator`标签是用于迭代集合数据并在JSP页面上展示的重要标签。本篇文章将深入讲解`s:iterator`标签的使用及其与OGNL(Object-Graph Navigation Language)的结合应用。 1. **s:iterator标签的...

Global site tag (gtag.js) - Google Analytics