当前位置: 代码迷 >> Web前端 >> struts2 Action中获取request, response,Session对象的步骤
  详细解决方案

struts2 Action中获取request, response,Session对象的步骤

热度:147   发布时间:2012-09-18 16:21:42.0
struts2 Action中获取request, response,Session对象的方法
非IoC方式
   要获得上述对象,关键Struts 2.0中com.opensymphony.xwork2.ActionContext类。我们可以通过它的静态方法getContext()获取当前 Action的上下文对象。 另外,org.apache.struts2.ServletActionContext作为辅助类(Helper Class),可以帮助您快捷地获得这几个对象。

<pre name="code" class="java"><pre name="code" class="java">///我们平时用的,这个很好理解
		HttpServletRequest    request = ServletActionContext.getRequest ();
		HttpServletResponse   response=ServletActionContext.getResponse();
		HttpSession session=ServletActionContext.getRequest().getSession();
		///////通过ActionContext获得
	    ActionContext ac = ActionContext.getContext();       
	    //request
	    HttpServletRequest request2 = (HttpServletRequest)ac.get(ServletActionContext.HTTP_REQUEST);       
	    //response
	    HttpServletResponse response2 = (HttpServletResponse)ac.get(ServletActionContext.HTTP_RESPONSE);       
	    //session以下是错误的,会报异常
java.lang.ClassCastException: 
org.apache.struts2.dispatcher.SessionMap cannot be cast to javax.servlet.http.HttpSession 
//对于session因为从ActionContext  获取的是Map类型,不能强转化成HttpSession类型,这是ActionContext与ServletActionContext的区别<pre name="code" class="java"><pre name="code" class="java"><pre name="code" class="java">
           &nbsp;HttpSession session2=(HttpSession)ac.get(ServletActionContext.SESSION);//错
	    HttpSession session3=(HttpSession)ac.getContext().getSession();//同上
	    HttpSession session4=(HttpSession)ac.get(ActionContext.SESSION);//错
	    
	    //session对象被封装成Map,我们对Map操作就可以
	    Map session5=ac.getSession();
	    Map session6=ServletActionContext.getContext().getSession();

            session5.put("key5", "value5");
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;     session6.put("key6", "value6");


写个例子测试一下:
package com.visit.frame.test;

import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.*;
import org.apache.struts2.dispatcher.*;

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

public class HelloAction extends ActionSupport{
	private String leaveMessage;
	
	public String execute(){
		this.setLeaveMessage("here me");
		///我们平时用的,这个很好理解
		HttpServletRequest    request = ServletActionContext.getRequest ();
//		HttpServletResponse   response=ServletActionContext.getResponse();
		HttpSession session=ServletActionContext.getRequest().getSession();
		
		
		
		
		///////通过ActionContext获得
	    ActionContext ac = ActionContext.getContext();       
	    //request
	    HttpServletRequest request2 = (HttpServletRequest)ac.get(ServletActionContext.HTTP_REQUEST);       
	    //response
//	    HttpServletResponse response2 = (HttpServletResponse)ac.get(ServletActionContext.HTTP_RESPONSE);       
	    //session
	    Map session2= (Map) ac.get(ServletActionContext.SESSION);
	    Map session3=ac.getContext().getSession();//同上
	    Map session4=(Map) ac.get(ActionContext.SESSION);
	    
	    //session对象被封装成Map,我们对Map操作就可以
	    Map session5=(Map)ac.getSession();
	    Map session6=ServletActionContext.getContext().getSession();
//	    
	    request.setAttribute("request", "request0");
	    session.setAttribute("session", "session0");
	    request2.setAttribute("request2", "request2");
	    
	    session2.put("session2", "session2");
	    
	    session3.put("session3", "session3");
	    session4.put("session4", "session4");
	    
	    session5.put("session5", "session5");
	    session6.put("session6", "session6");
	    

		return SUCCESS;
	}

	public String getLeaveMessage() {
		this.leaveMessage="hhhh";
		return leaveMessage;
	}

	public void setLeaveMessage(String leaveMessage) {
		this.leaveMessage = leaveMessage;
	}

}


请求的页面是:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>

<html>
<head><title>Simple ha page</title></head>
<body>
    <h3>hahaha</h3>

    <h3>you success,ha</h3>
    <br>
    request---<%=request.getAttribute("request") %><br>
    request2----<%=request.getAttribute("request2") %><br>
    session--<%=session.getAttribute("session") %><br>
    <br>------
    session2--<%=session.getAttribute("session2") %><br>
    session3--<%=session.getAttribute("session3") %><br>
    session4--<%=session.getAttribute("session4") %><br>
    session5--<%=session.getAttribute("session5") %><br>
    session6--<%=session.getAttribute("session6") %><br>
   
</body>
</html>
  相关解决方案