xml配置:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="map" namespace="/map" extends="struts-default">
<!-- 返回视图 -->
<action name="map" class="star.july.map.TypesAction" method="map">
<result name="success">/list.jsp</result>
</action>
<action name="aware" class="star.july.map.TypesAction2" method="map">
<result name="success">/list.jsp</result>
</action>
</package>
</struts>
——————————————————————————————————————————————
TypesAction:
package star.july.map;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class TypesAction extends ActionSupport{
List list = new ArrayList();
//往集合中添加元素
// list.add("小年");
public TypesAction(){
list.add("赵甲第");
list.add("蔡言芝");
list.add("王半斤");
list.add("齐冬草");
list.add("袁树");
list.add("李枝锦");
}
//获取域对象并赋值
public String map(){
ActionContext ac = ActionContext.getContext();
Map request = (Map)ac.get("request");
request.put("rlist", list);
Map session = ac.getSession();
session.put("slist", list);
Map application = ac.getApplication();
application.put("alist", list);
return SUCCESS;
}
}
——————————————————————————————————————
TypesAction2:
package star.july.map;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* 通过接口注入struts2的Map集合
* @author Starjuly
*
*/
public class TypesAction2 extends ActionSupport
implements RequestAware,SessionAware,ApplicationAware{
//创建一个集合
List list = new ArrayList();
//往集合中添加元素
// list.add("小年");
public TypesAction2(){
list.add("赵甲第");
list.add("蔡言芝");
list.add("王半斤");
list.add("齐冬草");
list.add("袁树");
list.add("李枝锦");
}
//定义成员变量存储参数值
private Map rp;
private Map sp;
private Map ap;
public void setApplication(Map<String, Object> application) {
this.ap = application;
}
public void setSession(Map<String, Object> session) {
this.sp = session;
}
public void setRequest(Map<String, Object> request) {
this.rp = request;
}
public String map(){
//将集合的值放入域对象中
ap.put("rlist", list);
sp.put("slist", list);
ap.put("alist", list);
return SUCCESS;
}
}