Struts的Map

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

}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值