第一种:
取得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 request;
private Map session;
private Map application;
public LoginAction(){
request = (Map)ActionContext.getContext().get("request");
session = ActionContext.getContext().getSession();
application = ActionContext.getContext().getApplication();
} public String execute(){
request.put("rs", "rs");
session.put("ss", "ss");
application.put("as", "as"); return SUCCESS;
}
}

ActionContext.getContext()是获得Action的上下文,然后可以获得MAP类型的request等
在输出页面可获得参数

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <title>My JSP 'MyJsp.<a href="http://www.2cto.com/kf/web/jsp/" target="_blank" class="keylink">jsp</a>' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> hello <br>
<s:property value="#request.rs"> <br><!-- //通过标签取值-->
<s:property value="#session.ss"> <br>
<s:property value="#application.as"> <br>
<%=request.getAttribute("rs") %><br><!-- 用原始的方法取值-->
<%=session.getAttribute("ss") %><br>
<%=application.getAttribute("as") %><br> <!--html>
</s:property></s:property></s:property>

第二种:
实现RequestAware,SessionAware,ApplicationAware接口,该方法成为DI(依赖注入)或者IOC(控制反转)。代码如下

package com.xjtu.st;

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; public class LoginAction extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{
private Map request;
private Map session;
private Map application; public String execute(){
request.put("rs", "rrs");
session.put("ss", "sss");
application.put("as", "aas"); return SUCCESS;
} public void setRequest(Map<string, object=""> request) {
// TODO Auto-generated method stub
this.request=request; } public void setSession(Map<string, object=""> session) {
// TODO Auto-generated method stub
this.session= session;
} public void setApplication(Map<string, object=""> application) {
// TODO Auto-generated method stub
this.application = application;
}
}
</string,></string,></string,>

前面2种是取得Map类型的,后两种是取得HtteServletRequest,HttpSession,ServletContext类型的办法第三种:

代码如下

package com.xjtu.st;

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; public class LoginAction extends ActionSupport {
private HttpServletRequest request;
private HttpSession session;
private ServletContext application; public LoginAction(){
request= ServletActionContext.getRequest();
session = request.getSession();
application = session.getServletContext();
}
public String execute(){
request.setAttribute("rs", "rrrs");
session.setAttribute("ss", "ssss");
application.setAttribute("as", "aaas"); return SUCCESS;
} }
 
 
 
该方法不常用第四种:
也是ioc的方式
代码如下
 
package com.xjtu.st;

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.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware; import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport implements ServletRequestAware{
private HttpServletRequest request;
private HttpSession session;
private ServletContext application; public String execute(){
request.setAttribute("rs", "rrrrs");
session.setAttribute("ss", "sssss");
application.setAttribute("as", "aaaas"); return SUCCESS;
}
public void setServletRequest(HttpServletRequest request) {
// TODO Auto-generated method stub
this.request = request;
session = request.getSession();
application = session.getServletContext(); } }

Struts2里如何取得request,session,application的更多相关文章

  1. struts2标签获取parameter,request,session,application中的值

    http://localhost:8080/demo/index.jsp?flag=kkkk <s:property value="#parameters.flag" /&g ...

  2. struts2 action中获取request session application的方法

    共四种方式: 其中前两种得到的是Map<String,Object>  后两种得到的才是真正的request对象 而Map就是把request对象中的属性取出做成了键值对而已. [方法一] ...

  3. Struts2初学 Struts2在Action获取内置对象request,session,application(即ServletContext)

    truts2在Action中如何访问request,session,application(即ServletContext)对象???? 方式一:与Servlet API解耦的方式      可以使用 ...

  4. page,request,session,application四个域对象的使用及区别

    转自:page,request,session,application四个域对象的使用及区别 1.page指当前页面.只在一个jsp页面里有效 .2.request 指从http请求到服务器处理结束, ...

  5. request, session, application辨析(待更新)

    作用域 request < session < application 分析 1. request 只在一次请求中有效,当请求发送变化时,信息将失效. 2. session 在当前一次会话 ...

  6. request,session,application

    JSP 的3个内置对象request,session,application,其实都有一个作用域,这些对象内部有一个Map成员用于存放数据,比如session对象的setAttribute(key,v ...

  7. struct2访问或添加request/session/application

    访问或添加request/session/application 1 通过ActionContext //这样放置 public String execute()  {     ActionConte ...

  8. JavaWeb -pageContext/request/session/application

    pageContext/request/session/application总结 一.范围差异 1. pageContext jsp页面容器 当前页面有效 2. request 请求对象 同一次请求 ...

  9. Strut2 ognl取出存放在request,session,application和对象栈的中的值

    1.取出request,session,applicaiton中的值 a.往里面加入request,session,application中加入值 public String testServlet( ...

随机推荐

  1. 生成mif文件的几种方法总结

    mif文件就是存储器初始化文件,即memory initialization file,用来配置RAM或ROM中的数据.生成QuartusII11.0可用的mif文件,有如下几种方式: 方法1:利用Q ...

  2. go bufio

    1 bufio 读结构 type Reader struct {    buf          []byte     // 缓冲区    rd           io.Reader // read ...

  3. 【HDOJ】4704 Sum

    数学题.f(n) = 2^(n-1) mod (1e9+7). #include <cstdio> #define MAXN 100005 char buf[MAXN]; __int64 ...

  4. Eight(bfs+全排列的哈希函数)

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22207   Accepted: 9846   Special Judge ...

  5. 利用matlab给图像加高斯噪声

    I = imread('DSC_0034.JPG'); J = imnoise(I,'gaussian',0.20); figure, imshow(I), figure, imshow(J)

  6. 开源库CImg 数据格式存储之二(RGB 顺序)

    在上一篇博客中已经初步说明了GDI和CImg数据的存储格式感谢博友 Imageshop 评论说明 CImg的说明文档中已有详细说明(详见上篇博客说明) CImg的数据格式确实是RRRGGGBBB顺序存 ...

  7. FTP服务器中文环境引起润日下载不了附件问题解析

    20160229日某农商行因为FTP下载功能有问题,导致当天所有涉及FTP文件下载的交易都不能正常使用,对于银行来说影响还是比较大.现将当天出问题的原因及处理过程解析如下,忘能给碰到类似问题的同行以供 ...

  8. 《A First Course in Probability》-chaper4-离散型随机变量-负二项分布

    基于我们最为熟悉的离散型分布——二项分布,我们能够衍生出很多别的分布列,对于之前介绍过的几何分布,我们赋予其的含义是:某个事件成功的概率是p,在n次独立重复实验中恰好成功一次的概率是多少.顺着这层含义 ...

  9. IOS面试题(虽然我们很少用)

    其实我们会考很多C的基本知识,主要还是交流,这个题就是防止那些小白. 1.Objective-C中,与alloc语义相反的方法是dealloc还是release?与retain语义相反的方法是deal ...

  10. Selenium终极自动化测试环境搭建(二)Selenium+Eclipse+Python

    Selenium终极自动化测试环境搭建(二)Selenium+Eclipse+Python 前面举例了Selenium+Eclipse+Junit+TestNG自动化测试环境的搭建,在前一篇的基础上, ...