用struts2获取session、request、parmeter的方法

package com.hanqi.action;

import java.util.Map;

import com.opensymphony.xwork2.ActionContext;

public class TestAction {

    //解耦方式
    //封装了WEB资源
    //使用ActionContext
    @SuppressWarnings("unchecked")
    public String testweb()
    {
        //访问web资源
        System.out.println("经过了action类");
        //单例模式
        //Action实例的上下文对象
        ActionContext ac= ActionContext.getContext();
        //1、获取application
        Map<String, Object>mapApp = ac.getApplication();

        System.out.println("app1="+mapApp.get("app1"));
        //放入新的键值对
        mapApp.put("app2", "新放入的application");
        //2、获取session资源
        Map<String, Object>mapSes=ac.getSession();
        System.out.println("ses1="+mapSes.get("ses1"));
        //放入新的值
        mapSes.put("ses2","放入的新的session");
        mapSes.remove("ses1");
        //3、获取request资源
        Map<String, Object>mapReq= (Map<String, Object>) ac.get("request");
        //不能直接获取之前的request属性
        System.out.println("req1="+mapReq.get("req1"));
        //可以放入新的request的属性
        mapReq.put("req2", "放入新的request");
        //4、直接获取传递的参数
        Map<String, Object>mapPar=ac.getParameters();
        //参数值被封装成String数组
        String[] str=(String[])mapPar.get("user");
        for(String t:str)
        {
        System.out.println("user="+t);
        }
        //放入新的参数不能被获取到
        mapPar.put("pw", new String[]{"123456"});
        return "success";

    }

}
package com.hanqi.action;
import java.util.Map;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.ParameterAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;

public class TestActionAware implements ApplicationAware,SessionAware,RequestAware,ParameterAware{
    private Map<String, Object>mapApp;
    private Map<String, Object>mapSes;
    private Map<String, Object>mapReq;
    private Map<String, String[]>mapPar;
    //实现ApplicationAware的方法
    //提供给struts2,放入application的数据集合
    @Override
    public void setApplication(Map<String, Object> arg0) {

        //接受
        mapApp=arg0;

    }
    public String testAware()
    {
        //访问application
        System.out.println("app1="+mapApp.get("app1"));

        mapApp.put("app2", "以Aware放入的application");

        //访问session
        System.out.println("ses1="+mapSes.get("ses1"));
        mapSes.put("ses2", "以Aware放入的Session");
        //访问 request
        System.out.println("req="+mapReq.get("req1"));
        mapReq.put("req2", "以Aware放入的request");
        //访问Parameter
        String[]str=mapPar.get("user");
        for(String t:str)
        {
        System.out.println("user"+t);
        }
        mapPar.put("pw", new String[]{"12345"});
        return "success";
    }

    @Override
    public void setSession(Map<String, Object> arg0) {

        mapSes=arg0;
    }

    @Override
    public void setRequest(Map<String, Object> arg0) {

        mapReq=arg0;
    }

    @Override
    public void setParameters(Map<String, String[]> arg0) {

        mapPar=arg0;
    }

}
package com.hanqi.action;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

public class TestServletAction {

    public String testServletAction()
    {
        //访问原生的web资源
        //得到ServletContext,相当于application
        ServletContext sc=ServletActionContext.getServletContext();
        System.out.println("app1="+sc.getAttribute("app1"));
        sc.setAttribute("app2", "原生方式放入的内容");
        //2、访问session
        HttpServletRequest hsr=ServletActionContext.getRequest();
        HttpSession hs=hsr.getSession();
        System.out.println("ses1="+hs.getAttribute("ses1"));
        hs.setAttribute("ses2", "原生方式放入的内容");

        //3、访问request
        System.out.println("req1="+hsr.getAttribute("req1"));

        hsr.setAttribute("req2", "原生方式放入的request");
        //4、访问parameter
        System.out.println("user1="+hsr.getParameter("user"));
        System.out.println("user2="+hsr.getParameter("user"));
        return "success";
    }

}
package com.hanqi.action;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.util.ServletContextAware;

public class TestServletActionAware implements ServletRequestAware, ServletContextAware {

    private ServletContext sc;
    private HttpServletRequest hsr;
    private HttpSession hs;
    @Override
    public void setServletContext(ServletContext arg0) {
        sc=arg0;

    }

    @Override
    public void setServletRequest(HttpServletRequest arg0) {
        hsr=arg0;
        hs=arg0.getSession();

    }
    public String testServletAware()
    {
        System.out.println("app1= "+sc.getAttribute("app1"));
        sc.setAttribute("app2", "新的app2");
        System.out.println("ses1= "+hs.getAttribute("ses1"));
        hs.setAttribute("ses2", "新的ses2");
        System.out.println("user= "+hsr.getParameter("user"));

        return "success";
    }

}
时间: 2024-11-08 19:08:44

用struts2获取session、request、parmeter的方法的相关文章

Struts2获取Session方法

Struts2里面有三种方法可以获取request,最好使用ServletRequestAware接口通过IOC机制注入Request对象. 方法1:IOC方式action类实现SessionAware接口,并创建一个MAP对象sessionpublic class UserLoginAction extends ActionSupport implements ServletRequestAware,SessionAware{ public void setServletRequest(Htt

【Struts2】Struts2获取session的三种方式

1.Map<String,Object> map =  ActionContext.getContext().getSession(); 2.HttpSession session = ServletActionContext.getRequest().getSession(); 3.让Action实现SessionAware接口,并实现public void setSession(Map<String, Object> session) {} 方法,Struts2会在实例化Act

Struts2访问session的两种方法

Struts2 的Action中若希望访问Session对象,可采用两种方式:     1.从ActionContext中获取:     2.实现SessionAware接口. 1.从ActionContext中获取: import java.util.Map; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class SessionTestAc

zk 获取session,request,servletContext(参考:http://www.dotblogs.com.tw/rockywang/archive/2010/01/13/12995.aspx)

session = Executions.getCurrent().getDesktop().getSession();或 HttpSession session = (HttpSession) Executions.getCurrent().getDesktop().getSession(); ServletContext sc = (ServletContext) Sessions.getCurrent().getWebApp().getNativeContext(); HttpServle

spring在普通类中获取session和request

在使用spring时,经常需要在普通类中获取session,request等对像.比如一些AOP拦截器类,在有使用struts2时,因为struts2有一个接口使用org.apache.struts2.ServletActionContext即可很方便的取到session对像.用法:ServletActionContext.getRequest().getSession(); 但在单独使用spring时如何在普通类中获取session,reuqest呢?首先要在web.xml增加如下代码: <l

Struts学习笔记(三)struts2中获得request、response和session的三种方法

struts2中获得request.response和session的三种方法   (1)非IoC方式 方法一:使用org.apache.struts2.ActionContext类,通过它的静态方法getContext()获取当前Action的上下文对象. ActionContext ctx = ActionContext.getContext(); ctx.put("liuwei", "andy"); //request.setAttribute("l

获取Session和request方法

action中的几种写法 //第一种很少用public class LoginAction1 extends ActionSupport {        private Map request;    private Map session;    private Map application;        public LoginAction1() {        request = (Map)ActionContext.getContext().get("request")

Struts2后台使用Request和Session方法

在Struts2后台,如果需要使用Request和Session的话,可以通过下面的方法: 主要是利用了com.opensymphony.xwork2.ActionContext类以及ora.apache.struts2.ServletActionContext类,具体的方法如下所示.获得request对象:1).HttpServletRequest request = ServletActionContext.getRequest ();2).ActionContext ct= ActionC

struts2获取request、session、application的四种方式

//获取map类型的request.session.application public class LoginAction1 extendsActionSupport { private Map request; private Map session; private Map application; publicLoginAction1() { request= (Map)ActionContext.getContext().get("request"); session= Ac