首页 > 代码库 > Struts2中获取Web元素request、session、application对象的四种方式

Struts2中获取Web元素request、session、application对象的四种方式

       我们在学习web编程的时候,一般都是通过requet、session、application(servletcontext)进行一系列相关的操作,request、session、和application他们都是web开发最常用和最实用的对象,有了它们可以大大方便开发人员进行开发和操作。但是在struts2中,基本都是action,这些个方法都是没有requet、session、application,所以如何获取这几个常用对象,也成了大家都比较关注的问题,下面我就来演示下,如何在struts2中来获取这三个对象。

方式一:通过ActionContext对象来获取

package com.action.day01;

import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 取map类型的request、session、servletcontext
 *  通过ActionContext对象来获取
 * @author Administrator
 *
 */
public class AccessWebElementAction extends ActionSupport {
    private Map<String, Object> request;
    private Map<String, Object> session;
    private Map<String, Object> application;

    public AccessWebElementAction() {
        request = (Map<String, Object>) ActionContext.getContext().get("request");
        session = ActionContext.getContext().getSession();
        application = ActionContext.getContext().getApplication();
    }

    public String getWebElement() {
        //将数据存到requet中
        request.put("r1", "r1");
        //将数据存到session中
        session.put("s1", "s1");
        //将数据存到application中
        application.put("a1", "a1");
        return SUCCESS;
    }
}

方式二:通过实现RequestAware, SessionAware,  ApplicationAware这三个接口的方式来获取(最常用)

package com.action.day01;

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;

/**
 * 取map类型的request、session、servletcontext 通过是实现RequestAware, SessionAware,
 * ApplicationAware 然后把我们定义的变量传递进去,进行初始化,由容器自动办帮我们创建,我们只是传递个参数变量进去接收对象即可
 * 
 * @author Administrator
 *
 */
public class AccessWebElementAction2 extends ActionSupport implements RequestAware, SessionAware, ApplicationAware {
    private Map<String, Object> request;
    private Map<String, Object> session;
    private Map<String, Object> application;

    public AccessWebElementAction2() {
        request = (Map<String, Object>) ActionContext.getContext().get("request");
        session = ActionContext.getContext().getSession();
        application = ActionContext.getContext().getApplication();
    }

    public String getWebElement() {
        // 将数据存到requet中
        request.put("r1", "r1");
        // 将数据存到session中
        session.put("s1", "s1");
        // 将数据存到application中
        application.put("a1", "a1");
        return SUCCESS;
    }

    @Override
    public void setApplication(Map<String, Object> application) {
        this.application = application;
    }

    @Override
    public void setSession(Map<String, Object> session) {
        this.session = session;
    }

    @Override
    public void setRequest(Map<String, Object> request) {
        this.request = request;
    }
}

 方式三:通过ServletActionContext对象来获取Http类型的request、session、application

package com.action.day01;

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

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

/**
 * 取Http类型的request、session、servletcontext 通过ServletActionContext对象来获取
 * 
 * @author Administrator
 *
 */
public class AccessWebElementAction3 extends ActionSupport {
    private HttpServletRequest request;
    private HttpSession session;
    private ServletContext application;

    public AccessWebElementAction3() {
        request = ServletActionContext.getRequest();
        session = ServletActionContext.getRequest().getSession();
        application = ServletActionContext.getServletContext();
    }

    public String getWebElement() {
        // 将数据存到requet中
        request.setAttribute("r1", "r1");
        // 将数据存到session中
        session.setAttribute("s1", "s1");
        // 将数据存到application中
        application.setAttribute("a1", "a1");
        return SUCCESS;
    }
} 

方式四:通过实现ServletRequestAware接口获取HttpServletRequest对象,进而通过request对象获取session和application对象

package com.action.day01;

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

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.opensymphony.xwork2.ActionSupport;

/**
 * 取Http类型的request、session、servletcontext 通过实现ServletRequestAware接口来获取
 * 
 * @author Administrator
 *
 */
public class AccessWebElementAction4 extends ActionSupport implements ServletRequestAware {
    private HttpServletRequest request;
    private HttpSession session;
    private ServletContext application;

    public AccessWebElementAction4() {

        session = request.getSession();
        application = request.getServletContext();
    }

    public String getWebElement() {
        // 将数据存到requet中
        request.setAttribute("r1", "r1");
        // 将数据存到session中
        session.setAttribute("s1", "s1");
        // 将数据存到application中
        application.setAttribute("a1", "a1");
        return SUCCESS;
    }

    @Override
    public void setServletRequest(HttpServletRequest request) {
        // 使用我们定义的属性来获取容器自动创建的HttpServletRequest对象
        this.request = request;

    }
}

 

至此,在struts2中方式,获取request、sessioin、application对象三种方法已经说明完毕了,有不足的地方,希望大家多多提意见!

Struts2中获取Web元素request、session、application对象的四种方式