struts2中获取action中request,response,session的方法

http://www.javaeye.com/topic/581820

 

不做不知道,一做才知道自己原来不知道。之前用惯struts1.x,那些request啊session之类都是方法自带有的,我们直接调用就可以;而平时公司项目中用到有struts2.x的话,action所继承的BaseAction等底层那些都是人家封装好的,直接继承就可以。现在自己搞个,才知道struts2的request、response、session原来都被隐藏的了,不过struts2提供有两种方式给我们访问。

    第一种方式,非IoC(Spring中的控制反转)方式:

Java代码 复制代码
  1.   
  2. /**   
  3.     * File Name:BaseAction.java * Version: * Date:2010-1-27 * Copyright Belongs To Musoon  Corporation 2010    
  4. */  
  5.        
  6. package com.action;   
  7.   
  8. import java.util.Map;   
  9.   
  10. import javax.servlet.http.HttpServletRequest;   
  11. import javax.servlet.http.HttpServletResponse;   
  12.   
  13. import org.apache.struts2.ServletActionContext;   
  14.   
  15. import com.opensymphony.xwork2.ActionContext;   
  16. import com.opensymphony.xwork2.ActionSupport;   
  17.   
  18. /**  
  19.  * Project Name:ZhiMing   ** Class Name:BaseAction   
  20.  * Author:Musoon        ** Created Time:2010-1-27 下午06:45:35   
  21.  * Changed By:Musoon   ** Changed Time:2010-1-27 下午06:45:35   
  22.  * Changed Memo:   
  23.  * @version   
  24.  * Class Description:   
  25.  */  
  26.   
  27. public class BaseAction extends ActionSupport {   
  28.   
  29.     private static final long serialVersionUID = 7620009925942346125L;   
  30.        
  31.     ActionContext context = ActionContext.getContext();   
  32.     HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);   
  33.     HttpServletResponse response = (HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE);   
  34.     Map session = context.getSession();   
  35.     //SessionMap session = (SessionMap) context.get(ActionContext.SESSION);   
  36.        
  37. }  
/** 
	* File Name:BaseAction.java * Version: * Date:2010-1-27 * Copyright Belongs To Musoon  Corporation 2010  
*/
	
package com.action;

import java.util.Map;

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

import org.apache.struts2.ServletActionContext;

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

/**
 * Project Name:ZhiMing   ** Class Name:BaseAction 
 * Author:Musoon        ** Created Time:2010-1-27 下午06:45:35 
 * Changed By:Musoon   ** Changed Time:2010-1-27 下午06:45:35 
 * Changed Memo: 
 * @version 
 * Class Description: 
 */

public class BaseAction extends ActionSupport {

	private static final long serialVersionUID = 7620009925942346125L;
	
	ActionContext context = ActionContext.getContext();
	HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);
	HttpServletResponse response = (HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE);
	Map session = context.getSession();
	//SessionMap session = (SessionMap) context.get(ActionContext.SESSION);
	
}



    我们平常所说的session一般是HttpSession,但在struts2中被封装成了Map类型。

    第二种方式,IoC方式:

Java代码 复制代码
  1.   
  2. /**   
  3.     * File Name:BaseAction.java * Version: * Date:2010-1-27 * Copyright Belongs To Musoon  Corporation 2010    
  4. */  
  5.        
  6. package com.action;   
  7.   
  8. import java.util.Map;   
  9.   
  10. import javax.servlet.http.HttpServletRequest;   
  11. import javax.servlet.http.HttpServletResponse;   
  12.   
  13. import org.apache.struts2.dispatcher.SessionMap;   
  14. import org.apache.struts2.interceptor.ServletRequestAware;   
  15. import org.apache.struts2.interceptor.ServletResponseAware;   
  16. import org.apache.struts2.interceptor.SessionAware;   
  17.   
  18. import com.opensymphony.xwork2.ActionContext;   
  19. import com.opensymphony.xwork2.ActionSupport;   
  20.   
  21.   
  22.   
  23. /**  
  24.  * Project Name:ZhiMing   ** Class Name:BaseAction   
  25.  * Author:Musoon        ** Created Time:2010-1-27 下午06:45:35   
  26.  * Changed By:Musoon   ** Changed Time:2010-1-27 下午06:45:35   
  27.  * Changed Memo:   
  28.  * @version   
  29.  * Class Description:   
  30.  */  
  31.   
  32. public class BaseAction extends ActionSupport implements SessionAware, ServletRequestAware, ServletResponseAware {   
  33.   
  34.     private static final long serialVersionUID = 7620009925942346125L;   
  35.        
  36.     ActionContext context = ActionContext.getContext();   
  37.     HttpServletRequest request;   
  38.     HttpServletResponse response;   
  39.     SessionMap session;   
  40.        
  41.     //获取request,response,session方式一,非IoC方式,不用实现SessionAware, ServletRequestAware, ServletResponseAware   
  42.     //ActionContext context = ActionContext.getContext();   
  43.     //HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);   
  44.     //HttpServletResponse response = (HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE);   
  45.     //Map session = context.getSession();   
  46.     //SessionMap session = (SessionMap) context.get(ActionContext.SESSION);   
  47.        
  48.     //获取request,response,session方式一,IoC方式,必须实现SessionAware, ServletRequestAware, ServletResponseAware   
  49.     public void setSession(Map map) {   
  50.         this.session = (SessionMap) map;   
  51.     }   
  52.     public void setServletRequest(HttpServletRequest request) {   
  53.         this.request = request;   
  54.     }   
  55.     public void setServletResponse(HttpServletResponse response) {   
  56.         this.response = response;   
  57.     }   
  58.        
  59. }  
/** 
	* File Name:BaseAction.java * Version: * Date:2010-1-27 * Copyright Belongs To Musoon  Corporation 2010  
*/
	
package com.action;

import java.util.Map;

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

import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

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



/**
 * Project Name:ZhiMing   ** Class Name:BaseAction 
 * Author:Musoon        ** Created Time:2010-1-27 下午06:45:35 
 * Changed By:Musoon   ** Changed Time:2010-1-27 下午06:45:35 
 * Changed Memo: 
 * @version 
 * Class Description: 
 */

public class BaseAction extends ActionSupport implements SessionAware, ServletRequestAware, ServletResponseAware {

	private static final long serialVersionUID = 7620009925942346125L;
	
	ActionContext context = ActionContext.getContext();
	HttpServletRequest request;
	HttpServletResponse response;
	SessionMap session;
	
	//获取request,response,session方式一,非IoC方式,不用实现SessionAware, ServletRequestAware, ServletResponseAware
	//ActionContext context = ActionContext.getContext();
	//HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);
	//HttpServletResponse response = (HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE);
	//Map session = context.getSession();
	//SessionMap session = (SessionMap) context.get(ActionContext.SESSION);
	
	//获取request,response,session方式一,IoC方式,必须实现SessionAware, ServletRequestAware, ServletResponseAware
	public void setSession(Map map) {
		this.session = (SessionMap) map;
	}
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}
	
}


    
这样我们在写action时直接继承这个BaseAction,那些request、response、session之类就可以正常地用了,good。等我下午有时间反编译一下别人封装好的BaseAction,看看是不是这样搞的,哈哈。

ps:
平时我们在action中要把值设进session然后在jsp页面去的话,一般是这样(struts2不行):

Java代码 复制代码
  1. public String findAllUsers() throws Exception {   
  2.        
  3.     List<User> userList = userService.findAllUsers();   
  4.        
  5.     HttpSession se = request.getSession();   
  6.     se.setAttribute("userList", userList);   
  7.        
  8.     session.put("userList", userList);   
  9.     //session.put("aaa", "bbb");   
  10.     //session.put(key, value);   
  11.        
  12.     //request.setAttribute("userList", userList);   
  13.        
  14.     return SUCCESS;   
  15. }  
	public String findAllUsers() throws Exception {
		
		List<User> userList = userService.findAllUsers();
		
		HttpSession se = request.getSession();
		se.setAttribute("userList", userList);
		
		session.put("userList", userList);
		//session.put("aaa", "bbb");
		//session.put(key, value);
		
		//request.setAttribute("userList", userList);
		
		return SUCCESS;
	}



在struts2中,设进session的话则应该变成这样了,因为session是一个map类型:

Java代码 复制代码
  1. public String findAllUsers() throws Exception {   
  2.        
  3.     List<User> userList = userService.findAllUsers();   
  4.     session.put("userList", userList);   
  5.     //request.setAttribute("userList", userList);   
  6.        
  7.     return SUCCESS;   
  8. }  
	public String findAllUsers() throws Exception {
		
		List<User> userList = userService.findAllUsers();
		session.put("userList", userList);
		//request.setAttribute("userList", userList);
		
		return SUCCESS;
	}


据说,如果直接到jsp页面的话,一般推荐用request而不用session,多人单机同时操作的话保险一点,虽然一个浏览器同一时间只有一个session。

在jsp页面取值的话:

Html代码 复制代码
  1.     <table class="table_report">  
  2.     <tr>    
  3.         <th>用户ID</th>  
  4.         <th>用户名称</th>  
  5.         <th>用户性别</th>  
  6.         <th>用户年龄</th>  
  7.         <th>用户地址</th>  
  8.         <th>用户电话</th>  
  9.         <th>用户邮箱</th>  
  10.     </tr>  
  11.     <!-- struts2最正规的取值方式 -->  
  12. <%--  <s:iterator id="user" value="%{#session.userList}">--%>  
  13.   
  14.    <s:iterator id="user" value="#session.userList">  
  15. <%--   <s:iterator id="user" value="#request.userList">--%>  
  16.     <tr>  
  17.         <td>${user.id}</td>  
  18.         <td>${user.name}</td>  
  19.         <td>${user.sex}</td>  
  20.         <td>${user.age}</td>  
  21.         <td>${user.address}</td>  
  22.         <td>${user.phone}</td>  
  23.         <td>${user.email}</td>  
  24.     </tr>  
  25.     </s:iterator>  
  26.   </table>  
  27. <%-- 用完要清空 --%>  
  28. <%request.removeAttribute("userList");%>  
  29. <%--<%session.removeAttribute("userList");%>--%>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值