`

struts2 map list处理

 
阅读更多
http://younglibin.iteye.com/blog/473483


在jsp页面用iterator 迭代时,如果 list中放的是map对象!而不是Object 用到的如下!

(平时用的list  中存放的都是Object对象)

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;   
    }   
       
       
}  
Java代码 
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;  
    }  
      
      


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;
}


}



2.testMap.jsp

Java代码 
<%@ 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迭代 Map List

    通过以上这些方法,Struts2的`&lt;s:iterator&gt;`标签能够灵活地处理Map和List数据,大大简化了JSP页面的数据展示逻辑。了解并熟练运用这些特性,能提高开发效率,让代码更加简洁易读。在实际项目中,根据具体需求进行...

    struts2传递map list set到action

    struts2传递map list set到action

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

    Struts2会根据表单元素的名字自动填充这些集合,`list[0]`会对应到`list`的第0个元素,`map['key']`会对应到`map`的键为`key`的元素。 3. **结果的展示**: 一旦Action处理完数据,我们可以使用`&lt;s:iterator&gt;`...

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

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

    Struts中List里嵌套Map

    在处理复杂数据结构时,Struts框架提供了灵活的数据绑定能力,其中包括在List中嵌套Map的情况。这个主题主要涉及到如何在Struts的ActionForm或者Action类中接收并处理这种复杂的数据结构,以及在JSP页面上进行展示。...

    struts2 表单数据绑定map、list等.rar

    本资源“struts2 表单数据绑定map、list等.rar”着重讲解了Struts2如何处理表单数据,特别是如何将表单数据绑定到Map和List等集合类型。了解这些内容对于构建动态、复杂的Web应用至关重要。 在Struts2中,数据绑定...

    struts2的方式返回json到jsp页面 List,Map,Object,对象各种操作都有....MyEclipse的,导入直接看效果。

    jsp通过Ajax无刷新获取Action返回的模拟数据,然后通过struts2转化成json数据返回页面....这里面Map,List,对象等等,一些常用的操作都有。主要的代码在Action和 json.js里面。适合新手入门

    struts2标签使用例子

    - `s:iterator`:遍历集合数据,如List、Map等,用于循环渲染数据。 - `s:if` 和 `s:else`:条件判断标签,类似于Java中的if...else语句。 - `s:foreach`:遍历数组或集合,类似Java的for-each循环。 4. **...

    Struts2案例翻译篇-Using Struts2 Tag

    Struts2标签库是基于JSP标准标签库(JSTL)的扩展,它提供了一系列预定义的标签,用于处理表单、链接、显示数据等常见的web交互。这些标签不仅简化了HTML代码,还提供了与Struts2框架的深度集成,如数据绑定、验证...

    AjaxStruts2Json实例

    总结来说,这个AjaxStruts2Json实例展示了如何在Struts2框架下利用Ajax进行异步请求,通过JSON传递和处理数据,从而实现网页的局部刷新。实践这个实例,开发者可以更深入地理解Struts2、Ajax以及JSON在实际项目中的...

    struts2框架学习笔记整理

    Struts2的设计目的是为了替代传统的Servlet技术,并提供一种更加简洁、高效的处理用户请求的方式。 ##### 1.2 Struts2的核心特点 - **核心机制**:Struts2采用拦截器机制处理用户的HTTP请求,这使得业务逻辑控制器...

    struts2和hibernate结合增删改查

    6. **结果集转换**:在查询后,将Hibernate查询得到的结果集转化为Struts2可传递的模型对象,如List或Map,然后返回相应的视图名称。 7. **配置Struts2-Hibernate整合**:在struts.xml配置文件中,为每个Action添加...

    struts2-json

    Action方法可以返回一个Map、List或其他集合对象,这些对象的元素将被转换为JSON。如果需要自定义哪些属性包含在JSON中,可以使用`@SkipValidation`和`@IncludeProperties`注解。 5. **JSON插件配置**: 可以通过...

    struts2中的OGNL的源码

    3. **集合操作**:支持对集合进行操作,如`list[0]`访问列表的第一个元素,`map["key"]`访问映射中的值。 4. **运算符支持**:包括算术运算、比较运算、逻辑运算等。 5. **上下文变量**:OGNL表达式可以在当前上...

    Struts(二)List_Map_LookupDispatchAction_Validate

    3. **List和Map**: 在描述中提到的`List_Map_LookupDispatchAction_Validate`可能是指在处理请求时,LookupDispatchAction使用List和Map来组织和存储数据。List是一个有序的集合,可以按索引访问,适合存储一系列...

    ajax+json+Struts2实现list传递实例讲解.docx

    在本文中,我们将深入探讨如何使用Ajax、JSON和Struts2框架实现List数据的传递。这个实例主要用于在不刷新整个页面的情况下,通过Ajax从后台获取并显示一个包含多个ErrorCondition对象的List集合。 首先,JSON...

    Jquery+Struts2+JSON处理

    **jQuery + Struts2 + JSON 处理** 在Web开发中,jQuery是一个强大的JavaScript库,提供了丰富的DOM操作、事件处理和动画效果。Struts2是一个流行的Java Web框架,用于构建可维护性和可测试性的MVC应用程序。JSON...

    struts2 radio

    在Struts2中,`radio`标签是用于处理单选按钮的,它提供了一种优雅的方式来呈现和处理用户界面中的选择项。这篇博客文章可能是关于如何在Struts2框架中使用`radio`标签的深入讲解。 在Web开发中,单选按钮通常用于...

    struts2中动态填充下拉框的例子

    6. **提交与处理**:当用户选择一个选项并提交表单时,Struts2会调用另一个Action(在这里是`anotherAction`),并将选定的值(`selectedOption`)传递给该Action。 以上就是Struts2中动态填充下拉框的基本流程。在...

    struts2整合jasperreport

    5. **配置Struts2**:为了让Struts2知道如何处理报表请求,我们需要在Struts2的配置文件(struts.xml)中添加相应的Action配置。 ```xml &lt;!-- struts.xml --&gt; &lt;param name="location"&gt;/path/to/your/report....

Global site tag (gtag.js) - Google Analytics