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= ActionContext.getContext().getSession();

application= ActionContext.getContext().getApplication();

}

publicString execute() {

request.put("r1","r1");

session.put("s1","s1");

application.put("a1","a1");

returnSUCCESS;

}

}

//采用接口方式获得map类型的request、session、application

public class LoginAction2 extendsActionSupport implements RequestAware,SessionAware, ApplicationAware {

private Map<String, Object> request;

private Map<String, Object> session;

private Map<String, Object> application;

//DIdependency injection

//IoCinverse of control

publicString execute() {

request.put("r1","r1");

session.put("s1","s1");

application.put("a1","a1");

returnSUCCESS;

}

@Override

publicvoid setRequest(Map<String, Object> request) {

this.request= request;

}

@Override

publicvoid setSession(Map<String, Object> session) {

this.session= session;

}

@Override

publicvoid setApplication(Map<String, Object> application) {

this.application= application;

}

}

//获取Http类型的request、session、application

public class LoginAction3 extendsActionSupport {

private HttpServletRequest request;

private HttpSession session;

private ServletContext application;

publicLoginAction3() {

request= ServletActionContext.getRequest();

session= request.getSession();

application= session.getServletContext();

}

publicString execute() {

request.setAttribute("r1","r1");

session.setAttribute("s1","s1");

application.setAttribute("a1","a1");

returnSUCCESS;

}

}

//采用接口方式获取Http类型的request、session、application

public class LoginAction4 extendsActionSupport implements ServletRequestAware {

private HttpServletRequest request;

private HttpSession session;

private ServletContext application;

publicString execute() {

request.setAttribute("r1","r1");

session.setAttribute("s1","s1");

application.setAttribute("a1","a1");

returnSUCCESS;

}

@Override

publicvoid setServletRequest(HttpServletRequest request) {

this.request= request;

this.session= request.getSession();

this.application= session.getServletContext();

}

}

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

时间: 2024-10-27 03:54:24

struts2获取request、session、application的四种方式的相关文章

Struts2中访问web元素的四种方式

Struts2中访问web元素的四种方式如下: 通过ActionContext来访问Map类型的request.session.application对象. 通过实现RequestAware.SessionAware.ApplicationAware接口来访问Map类型的request.session.application对象(IoC方式). 通过ServletActionContext来访问Servlet API类型的HttpServletRequest. HttpSession. Serv

获取项目根路径的四种方式

获取项目根路径的四种方式 CreateTime--2018年3月2日11:39:52 Author:Marydon <!-- 假设项目的请求根路径是"/demo" --> <!-- 方式一:EL表达式 --> ${pageContext.request.contextPath} <br>显示结果:"/demo" 应用: <i:form id="AddForm" method="post"

struts2 action中获取request session application的方法

共四种方式: 其中前两种得到的是Map<String,Object>  后两种得到的才是真正的request对象 而Map就是把request对象中的属性取出做成了键值对而已. [方法一] public class LoginAction { private Map request; private Map session; private Map application; public String execute() { request = (Map)ActionContext.getCo

Spring框架中获取连接池常用的四种方式

1:DBCP数据源 DBCP类包位于 /lib/jakarta-commons/commons-dbcp.jar,DBCP是一个依赖Jakarta commons-pool对象池机制的数据库连接池,所以在类路径下还必须包括/lib/jakarta-commons/commons-pool.jar.下面是使用DBCP配置oracle数据源的配置片断: <bean id="dataSource" class="org.apache.commons.dbcp.BasicDat

Struts2中获取HttpServletRequest,HttpSession等的几种方式

转自:http://www.kaifajie.cn/struts/8944.html package com.log; import java.io.IOException; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import o

Action中取得request,session的四种方式

在Struts2中,从Action中取得request,session的对象进行应用是开发中的必需步骤,那么如何从Action中取得这些对象呢?Struts2为我们提供了四种方式.分别为servlet 不相关的 非IoC 取得Request等对象的方式servlet 不相关的 IoC 取得Request等对象的方式servlet 相关的 非IoC 取得Request等对象的方式servlet 相关的 IoC 取得Request等对象的方式以下分别叙述. 首先请看struts.xml文件文件内容:

Struts2里如何取得request,session,application

第一种:取得MAP类型的request,session,application在java文件里写 package com.xjtu.st; import java.util.Map; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport{ private Map reques

Struts2 Web 资源获取的四种方式

Struts2 Web 资源获取一个有四种方式,两个大类 拦截器获取 Web 资源模式 静态对象获取 Web 资源模式 第一种:实现ServletRequestAware.ServletResponseAware.ServletContextAware接口 struts.xml <action name="FirstAction" class="Action.FirstAction"> <result name="success"

[Struts2]访问request,session和application对象(转)

与Servlet API解耦的访问方式 Structs2对HttpServletRequest,HttpSession,和ServletContext进行了封装,构造了三个Map对象来替代这三种对象,在Action中,直接使用HttpServletRequest,Httpsession,ServletContext对应的Map对象来保存和读取数据. 要获取这三个Map对象,可以使用com.opensymphony.xwork2.ActionContext类. ActionContext是acti