`
jessen163
  • 浏览: 457250 次
  • 性别: Icon_minigender_1
  • 来自: 潘多拉
社区版块
存档分类
最新评论

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

阅读更多
之前用惯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);
	
}


我们平常所说的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。
平时我们在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;
	}


在struts2中,设进session的话则应该变成这样了,因为session是一个map类型:
	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");%>--%>


分享到:
评论

相关推荐

    Struts2的Action中获得request response session几种方法

    Struts2的Action中获得request response session几种方法

    POI导入导出EXCEL文件.(struts 1.x and struts2.x).doc

    struts1.x的例子,struts2.x可以参考自己修改 1.action的写法 import java.io.*; import java.sql.*; import java.util.ArrayList; import javax.servlet.http.HttpServletRequest; import javax.servlet....

    从J2SE到J2EE知识点介绍

    (五) jsp内置对象(request,response.session,out) 121 1. request和response 121 2. session 122 (六) Servlet的 xml配置 126 1. xml的文件结构 127 2. 通过web.xml配置文件访问servlet的流程 129 (七) servlet里面...

    Java Web编程宝典-十年典藏版.pdf.part2(共2个)

    8.3.1 Struts2的获取与放置 8.3.2 第一个Struts2程序 8.4 Action对象 8.4.1 认识Action对象 8.4.2 请求参数的注入原理 8.4.3 Action的基本流程 8.4.4 什么是动态Action 8.4.5 动态Action的应用 8.5 Struts2的配置...

    外文翻译 stus MVC

    • Before passing it to the Action class, Struts will also conduct form state validation by calling the validation() method on UserActionForm. Note: This is not always wise to do. There might be ways ...

    拦截器和控制器的区别

    1、struts2对servlet封装(request,response) ,资源调配和资源的映射 2、框架设计的思想 istruts 配置,过滤器,反射 istruts.properties 3、starts2的使用思路 1、jar 2、配置文件 3、常用类 ...

    ssh(structs,spring,hibernate)框架中的上传下载

     LobHandler必须注入到Hibernate会话工厂sessionFactory中,因为sessionFactory负责产生与数据库交互的Session。LobHandler的配置如代码 5所示:  代码 5 Lob字段的处理句柄配置 1. 2. … 3. 4. class="org....

    struts 标签 logic:iterate使用 logic:iterate

    ActionForm form, HttpServletRequest request, HttpServletResponse response) { InformationForm informationForm = (InformationForm) form;// TODO Auto-generated method stub //业务开始 ...

    J2EE应用开发详解

    118 8.3.5 Struts2配置文件 119 8.4 Action的配置方式 121 8.4.1 动态方法调用 121 8.4.2 设置action元素的method属性 122 8.4.3 使用通配符配置action 122 8.4.4 默认action 123 8.5 拦截器Interceptor 123 8.5.1 ...

    Servlets和JSP核心技术 卷2(英文版) 第二部分

    Servlets和JSP核心技术 卷2 内容还是很详细的,看过卷1的人可以继续用这本书深造,呵呵 目录: Chapter 1. Using and Deploying Web Applications Section 1.1. Purpose of Web Applications Section 1.2. ...

    java面试题

    答:Struts1和Struts2是两个完全不同的框架,Struts1以ActionServlet作为核心控制器,由ActionServlet负责拦截用户的所有请求。Struts2以核心控制器FilterDispatcher为基础,包含了框架内部的控制流程和处理机制。 ...

    Servlets和JSP核心技术 卷2(英文版) 第一部分

    Servlets和JSP核心技术 卷2 内容还是很详细的,看过卷1的人可以继续用这本书深造,呵呵 目录: Chapter 1. Using and Deploying Web Applications Section 1.1. Purpose of Web Applications Section 1.2. ...

    java 面试题 总结

    redirect就是服务端根据逻辑,发送一个状态码,告诉浏览器重新去请求那个地址,一般来说浏览器会用刚才请求的所有参数重新请求,所以session,request参数都可以获取。 20、EJB与JAVA BEAN的区别? Java Bean 是可复用...

    李兴华Java Web开发实战经典(高清版) Part2

    6.4、response对象 6.4.1、设置头信息 6.4.2、页面跳转 6.4.3、操作Cookie 6.5、session对象 6.5.1、取得Session Id 6.5.2、登陆及注销 6.5.3、判断新用户 6.5.4、取得用户的操作时间 6.6、...

    超级有影响力霸气的Java面试题大全文档

    redirect就是服务端根据逻辑,发送一个状态码,告诉浏览器重新去请求那个地址,一般来说浏览器会用刚才请求的所有参数重新请求,所以session,request参数都可以获取。 23、EJB与JAVA BEAN的区别?  Java Bean 是可...

    java web 视频、电子书、源码(李兴华老师出版)

    6.4、response对象 6.4.1、设置头信息 6.4.2、页面跳转 6.4.3、操作Cookie 6.5、session对象 6.5.1、取得Session Id 6.5.2、登陆及注销 6.5.3、判断新用户 6.5.4、取得用户的操作时间 6.6、...

    李兴华 Java Web 开发实战经典_带源码_高清pdf 带书签 上

    6.4、response对象 6.4.1、设置头信息 6.4.2、页面跳转 6.4.3、操作Cookie 6.5、session对象 6.5.1、取得Session Id 6.5.2、登陆及注销 6.5.3、判断新用户 6.5.4、取得用户的操作时间 6.6、application对象...

    MLDN+李兴华+Java+Web开发实战经典.part3.rar )

    6.4、response对象 6.4.1、设置头信息 6.4.2、页面跳转 6.4.3、操作Cookie 6.5、session对象 6.5.1、取得Session Id 6.5.2、登陆及注销 6.5.3、判断新用户 6.5.4、取得用户的操作时间 6.6、...

    李兴华 java_web开发实战经典 源码 完整版收集共享

    6.4、response对象 6.4.1、设置头信息 6.4.2、页面跳转 6.4.3、操作Cookie 6.5、session对象 6.5.1、取得Session Id 6.5.2、登陆及注销 6.5.3、判断新用户 6.5.4、取得用户的操作时间 6.6、application对象...

Global site tag (gtag.js) - Google Analytics