1. 程式人生 > >在SpringMVC中獲取request對象的幾種方式

在SpringMVC中獲取request對象的幾種方式

hello ext spring pub strong framework () frame -c

1.最簡單的方式(註解法)

@Autowired
private HttpServletRequest request;

2.最麻煩的方法

a. 在web.xml中配置一個監聽

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

b.之後在程序裏可以用

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

3.最直接的方法

public String hello(HttpServletRequest request,HttpServletResponse response)

贈送一個如何在Struts2中獲取request對象

HttpServletRequest request = ServletActionContext.getRequest();

在SpringMVC中獲取request對象的幾種方式