Struts2之HttpServletRequest、HttpServletResponse,HttpSession,Parameters处理

在正式讲解如何获取上述对象之前,需要先搞清楚一点,类似于Struts2、SpringMVC框架之所以在诸多方面用着比较方便,简化开发人员重复机械性的工作,就是因为它们把底层的Servlet操作封装起来,替开发人员干了他们该干的工作,因此每一种框架都提供了获取底层Servlet的方式,好的,正式开始讲解本篇博客的内容;

首先讲解一种通过实现Struts2的某些个接口,来获取HttpServletRequest、HttpServletResponse、HttpSession的方式,先看如下几个接口源码:

import java.util.Map;


/**
 * Actions that want access to the user's HTTP session attributes should implement this interface.<p>
 * <p/>
 * This will give them access to a Map where they can put objects that can be made available
 * to subsequent requests.<p/>
 * <p/>
 * Typical uses may be cached user data such as name, or a shopping cart.
 *
 */
public interface SessionAware {

    /**
     * Sets the Map of session attributes in the implementing class.
     *
     * @param session a Map of HTTP session attribute name/value pairs.
     */
    public void setSession(Map<String,Object> session);
}
package org.apache.struts2.interceptor;

import javax.servlet.http.HttpServletRequest;


/**
 * All Actions that want to have access to the servlet request object must implement this interface.<p>
 * <p/>
 * This interface is only relevant if the Action is used in a servlet environment. <p>
 * <p/>
 * Note that using this interface makes the Action tied to a servlet environment, so it should be
 * avoided if possible since things like unit testing will become more difficult.
 *
 */
public interface ServletRequestAware {

    /**
     * Sets the HTTP request object in implementing classes.
     *
     * @param request the HTTP request.
     */
    public void setServletRequest(HttpServletRequest request);
}
package org.apache.struts2.interceptor;

import javax.servlet.http.HttpServletResponse;


/**
 * All Actions that want to have access to the servlet response object must implement this interface.<p>
 * <p/>
 * This interface is only relevant if the Action is used in a servlet environment.<p>
 * <p/>
 * Note that using this interface makes the Action tied to a servlet environment, so it should be
 * avoided if possible since things like unit testing will become more difficult.
 *
 */
public interface ServletResponseAware {

    /**
     * Sets the HTTP response object in implementing classes.
     *
     * @param response the HTTP response.
     */
    public void setServletResponse(HttpServletResponse response);
}
上面这三个接口的大体作用就是如果Actions类想要访问Servlet请求、响应、Session对象,就需要依次实现这几个接口,且这三个接口只有处于Servlet环境中是才起作用,下面通过一个Action类,来演示如何获取 Servlet请求、响应、Session对象
package com.yxd.struts2.action;

import java.util.Map;

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

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

import com.opensymphony.xwork2.ActionSupport;

public class GetServletDomainAction extends ActionSupport implements
		SessionAware, ServletRequestAware, ServletResponseAware {
	
	private HttpServletResponse response;
	
	private HttpServletRequest request;
	
	private Map<String, Object> session;

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		return super.execute();
	}

}
Struts2框架在实例化GetServletDomainAction对象是,会自动调用各种setter方法,将HttpServletRequest、HttpServletResponse、HttpSession对象设置进去,然后就可以在execute方法里面随便使用了;

第二种方式:Struts2中提供了ActionContext类用来获取Session对象中的属性值,同样提供了ServletActionContext类继承自ActionContext,用来获取HttpServletRequest、HttpServletResponse对象,下面看Action实例:

package com.yxd.struts2.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;

public class GetServletDomainAction extends ActionSupport{
	
	@Override
	public String execute() throws Exception {
		
		// 获取Session对象中属性的Map对象
		Map<String,Object> attrs = ActionContext.getContext().getSession();
		
		// 获取HttpServletRequest对象
		HttpServletRequest request = ServletActionContext.getRequest();
		
		// 获取HttpServletResponse对象
		HttpServletResponse response = ServletActionContext.getResponse();
		
		// TODO Auto-generated method stub
		return super.execute();
	}

}
上述两种方式,是在Struts2项目中常用到的获取Servlet对象的方式,需要特别对获取的Session说明一下,大家会发现这个Session对象并不是真正意义上我们想要的HttpSession对象,获取到的是一个封装了HttpSession对象中各种属性的Map集合,那么如何根据获取到的这个Map销毁Session呢?

实际上我们通过上述两种方法获取到的Session对象,是一个org.apache.struts2.dispatcher.SessionMap<K, V>类型的一个实例,且该接口里面有invalidate()方法就是用来销毁Http Session对象的,因此可以通过将获取到的Map向下强制类型转换成SessionMap类型,然后调用这个方法即可,如下:

// 获取Session对象中属性的Map对象
Map<String,Object> attrs = ActionContext.getContext().getSession();

// 销毁Session对象
if(attrs instanceof SessionMap){
	((SessionMap<String,Object>)attrs).invalidate();
}
最后在说一下如何获取传入到Action中的请求参数吧,跟上述两种方式差不多,可以直接通过ActionContext实例获取请求参数,也可以通过让Action类实现ParameterAware接口,然后再Action方法中通过调用setParameters(Map<String, String[]> parameters)将请求参数传递进Action里面去

Map<String,Object> parameters = ActionContext.getContext().getParameters();
上述两种方式都采用线程安全的方式获取Servlet对象,总体来说,在实际项目应用中足够使用了,Struts2官方推荐使用实现接口的方式获取Servlet对象,所以在日常工作中也建议使用该方式!








  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值