SpringMVC与Struts2中获取request、response对象


出处:http://hi.baidu.com/ufobject/item/f836412ae58ca7140875088f

一、SpringMVC中获取request、response对象

方法一:

BaseAction.java

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前都会首先执行它,也可以将一些准备数据的操作放置在该方法里面。


方法二:

public class BaseAction {

protected HttpServletRequest request;

protected HttpServletResponse response;

protected HttpSession session;

1.注解法

@Autowired

private  HttpServletRequest request;


2.直接取

HttpServletRequest request1 = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();

HttpServletResponse response1 = ((ServletWebRequest)RequestContextHolder.getRequestAttributes()).getResponse();

3.在web点xml中配置一个监听

<listener>  

        <listener-class>  

            org.springframework.web.context.request.RequestContextListener  

        </listener-class>  

</listener>  


二、Struts2中获取request、response对象

HttpServletRequest request = ServletActionContext.getRequest();


你可能感兴趣的:(SpringMVC与Struts2中获取request、response对象)