Struts2中action获取request、response、session的方式

简介: 之前用惯struts1.x,那些request啊session之类都是方法自带有的,我们直接调用就可以;而平时公司项目中用到有struts2.x的话,action所继承的BaseAction等底层那些都是人家封装好的,直接继承就可以。

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

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

  

/**    
    * 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);    
        
} 
/** 
* 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方式:


  

/**    
    * 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不行):

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;    
} 
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类型:

public String findAllUsers() throws Exception {    
        
    List<User> userList = userService.findAllUsers();    
    session.put("userList", userList);    
    //request.setAttribute("userList", userList);    
        
    return SUCCESS;    
} 
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页面取值的话:

<table class="table_report">   
    <tr>     
        <th>用户ID</th>   
        <th>用户名称</th>   
        <th>用户性别</th>   
        <th>用户年龄</th>   
        <th>用户地址</th>   
        <th>用户电话</th>   
        <th>用户邮箱</th>   
    </tr>   
    <!-- struts2最正规的取值方式 -->   
<%-- <s:iterator id="user" value="%{#session.userList}">--%>   
   
   <s:iterator id="user" value="#session.userList">   
<%--   <s:iterator id="user" value="#request.userList">--%>   
    <tr>   
        <td>${user.id}</td>   
        <td>${user.name}</td>   
        <td>${user.sex}</td>   
        <td>${user.age}</td>   
        <td>${user.address}</td>   
        <td>${user.phone}</td>   
        <td>${user.email}</td>   
    </tr>   
    </s:iterator>   
</table>   
<%-- 用完要清空 --%>   
<%request.removeAttribute("userList");%>   
<%--<%session.removeAttribute("userList");%>--%> 


 

 

相关文章
|
Java Spring
springMVC中获取request 对象
springMVC中获取request 对象
|
Java
PageContext request session application区别以及EL表达式
PageContext request session application区别以及EL表达式
108 0
|
Java 应用服务中间件
Servlet三大作用域:Request、Session、Application
Servlet三大作用域:Request、Session、Application
220 0
|
移动开发 前端开发 JavaScript
通过Filter,对request 和 response 进行处理
通过Filter,对request 和 response 进行处理,统一加解密,Xss过滤,HttpServletRequestWrapper,HttpServletResponseWrapper
2185 0
|
Java
Request和Response对象详解
Request和Response对象详解
213 0
Request和Response对象详解
JavaWeb - request.getSession() & request.getSession(true/false)区别
JavaWeb - request.getSession() & request.getSession(true/false)区别
208 0
|
Java Maven
JSP 使用 ${pageContext.request.contextPath} 无效
通过 ${pageContext.request.contextPath}可在JSP 中取得当前的项目绝对路径,比如当前项目是 http://localhost:8080/demo, 则 ${pageContext.request.contextPath} 代表的就是 /demo,其中 / 代表 http://localhost:8080,所以一般使用${pageContext.request.contextPath} 定位资源。
|
应用服务中间件 容器 数据安全/隐私保护
Servlet中request请求Get和Post方法以及乱码解决
前言: 传递的请求参数如何获取 GET方式: 参数放在URI后面 POST方式: 参数放在实体内容中 后台获取前台数据方法: 核心的API: request.getParameter("参数名"); 根据参数名获取参数值(注意,只能获取一个值的参数) request.
3142 0