第一种:
取得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. 独家分享——大牛教你如何学习Web前端开发

    2014-12-18 14:35:42     引语 自从2008年接触网站开发以来到现在已经有六个年头了,今天偶然整理电脑资料看到当时为参加系里面一个比赛而做的第一个网站时,勾起了在这网站开发道路上 ...

  2. Sphnix创建文档

    http://www.ibm.com/developerworks/cn/opensource/os-sphinx-documentation/ Oracle实例名和服务名

  3. codeforces C. Booking System

    题意:有n组客人,分别告诉每一组的个数和花费,然后给你餐厅内k个桌子,每个桌子的最大容纳人数,如何安排使得餐厅最大收益并且容纳人数尽可能大: 思路:贪心,对花费排序,然后对每一组客人找桌子就可以. # ...

  4. Sublime Text 有哪些使用技巧

    1. 更改变量名的几种方法&lt;img src="https://pic4.zhimg.com/d93cf0e8987e0117f3a3187cfe8e53fb_b.jpg&quo ...

  5. 快速创建php server

    mkdir testmv test.html testphp -S localhost:8082http://localhost:8082/test.html

  6. 图论(网络流):SPOJ OPTM - Optimal Marks

    OPTM - Optimal Marks You are given an undirected graph G(V, E). Each vertex has a mark which is an i ...

  7. 图论(A*算法,K短路) :POJ 2449 Remmarguts' Date

    Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 25216   Accepted: 6882 ...

  8. ToArray()和IEnumerable<T>,List<T>

    一:ToArray() 在程序中,我们往往习惯使用List<>这种集合类,但是程序中却要求需要传递一个数组,List<>已经为我们提供了toArray()方法 二:IEnume ...

  9. Maximum Subarray——LeetCode

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  10. Android SDK更新失败最新解决方案

    解决方案: 将https修改为http://在Android SDK manager中,的tools文件夹下,选择options...选项, 勾选 Force https//... sources t ...