我的博客我做主

          我的未來不是夢!
          posts - 9, comments - 10, trackbacks - 0, articles - 0
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          使用Struts2的iterator標簽輕松遍歷復雜的Map類型

          Posted on 2010-08-26 22:18 平常心 閱讀(6901) 評論(0)  編輯  收藏 所屬分類: Struts2
          更多博客請查看:http://www.v5cn.cn
          1.創建一個Web工程,添加Struts2支持。
          2.創建兩個實體類:
          a). Mother(母親)的Java類。
          package struts.map.entity;

          import java.io.Serializable;

          public class Mother implements Serializable {

          private static final long serialVersionUID = 1L;

          private int motherId;        //母親ID
              private String motherName;        //母親名字
              public int getMotherId() {
          return motherId;
          }
          public void setMotherId(int motherId) {
          this.motherId = motherId;
          }
          public String getMotherName() {
          return motherName;
          }
          public void setMotherName(String motherName) {
          this.motherName = motherName;
          }
          }

          b).Children(孩子)的Java類

          package struts.map.entity;

          import java.io.Serializable;

          public class Children implements Serializable {

          private static final long serialVersionUID = 1L;

          private int childId;        //孩子ID
              private int motherId;        //母親的ID
              private String childName;        //孩子名字
              
          public int getChildId() {
          return childId;
          }
          public void setChildId(int childId) {
          this.childId = childId;
          }
          public int getMotherId() {
          return motherId;
          }
          public void setMotherId(int motherId) {
          this.motherId = motherId;
          }
          public String getChildName() {
          return childName;
          }
          public void setChildName(String childName) {
          this.childName = childName;
          }
          }

           

          3. 創建一個Action,并創建一位母親和她的孩子。

          package struts.map.test;

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

          import struts.map.entity.Children;
          import struts.map.entity.Mother;

          import com.opensymphony.xwork2.ActionSupport;

          public class Struts2_Map extends ActionSupport {

          private static final long serialVersionUID = 1L;

          private Map<Mother,List<Children>> motherChildn;

          public Map<Mother, List<Children>> getMotherChildn() {
          return motherChildn;
          }

          @Override
          public String execute() throws Exception {
          /*-------------------以對象做父節點的鍵,List做子節點的值,的Map-----------------------*/
          Mother mother 
          = new Mother();
          mother.setMotherId(
          10000);
          mother.setMotherName(
          "花木蘭");

          Children children1 
          = new Children();
          children1.setChildId(
          10000);
          children1.setMotherId(
          10000);
          children1.setChildName(
          "小花木蘭1");

          Children children2 
          = new Children();
          children2.setChildId(
          10001);
          children2.setMotherId(
          10000);
          children2.setChildName(
          "小花木蘭2");

          Children children3 
          = new Children();
          children3.setChildId(
          10002);
          children3.setMotherId(
          10000);
          children3.setChildName(
          "小花木蘭3");

          motherChildn 
          = new HashMap<Mother,List<Children>>();

          List
          <Children> childrens = new ArrayList<Children>();

          childrens.add(children1);
          childrens.add(children2);
          childrens.add(children3);

          motherChildn.put(mother,childrens);

          return SUCCESS;
          }
          }

          struts.xml
          <?xml version="1.0" encoding="UTF-8" ?>
          <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
          <struts>
          <package name="map" extends="struts-default">
          <action name="struts_map" class="struts.map.test.Struts2_Map">
          <result>result.jsp</result>
          </action>
          </package>
          </struts>  

          4.創建兩個頁面:
          a).跳轉頁面:
          <%@ page language="java" pageEncoding="UTF-8"%>
          <%
          String path = request.getContextPath();
          %>

          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
          <html>
          <head>

          <title>Struts_Map</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">
          </head>

          <body>
          <href="struts_map.action">查看Map</a>
          </body>
          </html>

          b).最終頁面,也是作重要的頁面:
          <%@ page language="java" pageEncoding="UTF-8"%>
          <%@taglib uri="/struts-tags" prefix="s" %>
          <%
          String path = request.getContextPath();
          %>

          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
          <html>
          <head>

          <title>Struts_Map</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">
          </head>

          <body>
          <div>
          <h3>-----------------以對象做父節點的鍵,List做子節點的值,的Map--------------------</h3>
          <s:iterator var="mc" value="motherChildn">
          <div>
          母親名稱:
          <s:property value="key.motherName"/>
          </div>
          <s:iterator var="ch" value="value">
          <div>
          &nbsp;&nbsp;&nbsp;孩子名稱:<s:property value="#ch.childName"/>
          </div>
          </s:iterator>
          </s:iterator>
          </div>
          </body>
          </html>

          最終運行結果:
          主站蜘蛛池模板: 常州市| 金川县| 宝丰县| 北票市| 汪清县| 方正县| 安仁县| 北宁市| 威海市| 深水埗区| 长葛市| 漳浦县| 潼南县| 乐昌市| 漯河市| 宁阳县| 望奎县| 长岭县| 汽车| 沙湾县| 临澧县| 台州市| 布尔津县| 宝鸡市| 鹰潭市| 且末县| 仙桃市| 凤城市| 潮安县| 兰溪市| 黔江区| 松原市| 宁远县| 天水市| 绥江县| 桓仁| 长寿区| 剑河县| 南郑县| 靖宇县| 宁化县|