SpringMVC获取request对象和response对象

其他Action继承BaseAction

方法一:

1、BaseAction类

public class BaseAction {
	
	protected HttpServletRequest request;
	protected HttpServletResponse response;
	protected HttpSession session;
	
	@ModelAttribute
	public void setReqAndRes(HttpServletRequest request, HttpServletResponse response){
		this.request = request;
		this.response = response;
		this.session = request.getSession();
	}
	

说明:ModelAttribute的作用


1)放置在方法的形参上:表示引用Model中的数据

2)放置在方法上面:表示请求该类的每个Action前都会首先执行它,也可以将一些准备数据的操作放置在该方法里面。

方法二:

1、BaseAction类

public class BaseAction {
	
	protected HttpServletRequest request;
	protected HttpServletResponse response;
	protected HttpSession session;

	HttpServletRequest request1 = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
	HttpServletResponse response1 = ((ServletWebRequest)RequestContextHolder.getRequestAttributes()).getResponse();
	
2、web.xml

<listener>
	<listener-class>org.springframework.web.context.request.RequestContentListener</listener-class>
</listener>





你可能感兴趣的:(SpringMVC获取request对象和response对象)