1.创建JavaBean实例
2.创建Action类
3.创建JSP文件
注释:[color=red][b]Map中的Value存储的是一个JavaBean对象,要使用#studentJavaBean.value.name和#studentJavaBean.value.age的格式进行属性的获取并输出。[/b][/color]
4.修改struts.xml文件
5.运行结果:
[img]http://dl.iteye.com/upload/attachment/380414/1e316676-ce4b-3584-8adc-a9760e7bbbb0.png[/img]
package com.wl.control.Bean;
public class StudentInfo {
private String name;
private int age;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
2.创建Action类
package com.wl.control.tagsAction;
import java.util.HashMap;
import java.util.Map;
import com.opensymphony.xwork2.ActionSupport;
import com.wl.control.Bean.StudentInfo;
public class ShowMapJavaBean extends ActionSupport {
private Map listMapJavaBean=new HashMap();
public Map getListMapJavaBean() {
return listMapJavaBean;
}
public void setListMapJavaBean(Map listMapJavaBean) {
this.listMapJavaBean = listMapJavaBean;
}
@Override
public String execute() throws Exception {
StudentInfo studentInfo1=new StudentInfo();
studentInfo1.setName("张三");
studentInfo1.setAge(20);
StudentInfo studentInfo2=new StudentInfo();
studentInfo2.setName("李四");
studentInfo2.setAge(23);
StudentInfo studentInfo3=new StudentInfo();
studentInfo3.setName("王五");
studentInfo3.setAge(25);
listMapJavaBean.put("1", studentInfo1);
listMapJavaBean.put("2", studentInfo2);
listMapJavaBean.put("3", studentInfo3);
return "showmapjavabean";
}
}
3.创建JSP文件
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<s:iterator value="listMapJavaBean" var="studentJavaBean">
id:<s:property value="#studentJavaBean.key"/>
姓名:<s:property value="#studentJavaBean.value.name"/>
年龄:<s:property value="#studentJavaBean.value.age"/>
<br>
</s:iterator>
</body>
</html>
注释:[color=red][b]Map中的Value存储的是一个JavaBean对象,要使用#studentJavaBean.value.name和#studentJavaBean.value.age的格式进行属性的获取并输出。[/b][/color]
4.修改struts.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="Struts2.1" extends="struts-default">
<action name="showmapjavabean" class="com.wl.control.tagsAction.ShowMapJavaBean">
<result name="showmapjavabean">/controlTags/showmapjavabean.jsp</result>
</action>
</package>
</struts>
5.运行结果:
[img]http://dl.iteye.com/upload/attachment/380414/1e316676-ce4b-3584-8adc-a9760e7bbbb0.png[/img]