博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Struts2访问request,session,application的四种方式
阅读量:5924 次
发布时间:2019-06-19

本文共 4079 字,大约阅读时间需要 13 分钟。

hot3.png

 方式1--》访问Map类型的:
package cn.pdsu.edu;import java.util.Map;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;/** * 普通方式:Map类型的request,session,application */public class ActionDemo1 extends ActionSupport {	private Map
request; private Map
session; private Map
application; public ActionDemo1() { this.request = (Map)ActionContext.getContext().get("request"); this.session = ActionContext.getContext().getSession(); this.application = ActionContext.getContext().getApplication(); } @Override public String execute() throws Exception { request.put("message", "message"); session.put("message", "message"); application.put("message", "message"); return SUCCESS; }}

方式2--》访问map类型的:

package cn.pdsu.edu;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,application */public class ActionDemo2 extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{	private Map
request; private Map
session; private Map
application; @Override public String execute() throws Exception { request.put("message", "message"); session.put("message", "message"); application.put("message", "message"); return SUCCESS; } public void setRequest(Map
request) { this.request=request; } public void setSession(Map
session) { this.session=session; } public void setApplication(Map
application) { this.application = application; }}

方式3--》访问HttpServletRequest

package cn.pdsu.edu;import java.util.Map;import javax.servlet.ServletContext;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpSession;import org.apache.struts2.ServletActionContext;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;/** * 普通方式:Servlet类型的request,session,application */public class ActionDemo3 extends ActionSupport {	private HttpServletRequest request;	private HttpSession session;	private ServletContext application;	public ActionDemo3() {		request = ServletActionContext.getRequest();		session = request.getSession();		application = session.getServletContext();	}	@Override	public String execute() throws Exception {		request.setAttribute("message", "message");		session.setAttribute("message", "message");		application.setAttribute("message", "message");		return SUCCESS;	}}

方式4--》通过接口访问HttpServletRequest

package cn.pdsu.edu;import java.util.Map;import javax.management.j2ee.statistics.ServletStats;import javax.servlet.ServletContext;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpSession;import org.apache.struts2.ServletActionContext;import org.apache.struts2.interceptor.ApplicationAware;import org.apache.struts2.interceptor.RequestAware;import org.apache.struts2.interceptor.ServletRequestAware;import org.apache.struts2.interceptor.SessionAware;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;/** * 接口方式:Servlet类型的request,session,application */public class ActionDemo4 extends ActionSupport implements ServletRequestAware{	private HttpServletRequest request;	private HttpSession session;	private ServletContext application;	@Override	public String execute() throws Exception {		request.setAttribute("message", "message");		session.setAttribute("message", "message");		application.setAttribute("message", "message");		return SUCCESS;	}	public void setServletRequest(HttpServletRequest request) {		this.request = request;		this.session = request.getSession();		this.application = session.getServletContext();	}}

 

转载于:https://my.oschina.net/java2010/blog/356451

你可能感兴趣的文章
ubuntu 下 apache+tomcat整合_(mod-jk方法)[转]
查看>>
iis6 zencart1.39 伪静态规则
查看>>
Python学习之路7——深浅拷贝剖析
查看>>
宁波竞赛题
查看>>
Java实现Socket5代理服务器
查看>>
Android-异步图像装载机
查看>>
MySQL学习笔记——安装及配置环境
查看>>
SQL Server代理(3/12):代理警报和操作员
查看>>
android 物理按键 监听
查看>>
npm 主要命令
查看>>
【Android Studio安装部署系列】五、新建你的第一个项目:HelloWorld
查看>>
使用Phaser开发你的第一个H5游戏(一)
查看>>
十、performSelector延时调用内存泄漏的问题
查看>>
理解JVM(二):垃圾收集算法
查看>>
Redux专题:实用
查看>>
杠上Spark、Flink?Kafka为何转型流数据平台
查看>>
分类模型的评价方法
查看>>
微服务系列-Spring Cloud优质项目推荐
查看>>
11-玩转数据结构-并查集
查看>>
Android--List转换String,String转换List
查看>>