Several ways to get the request object

Several ways to get the request object in SpringMVC

1. The easiest way (annotation method)

1

2

@Autowired

private  HttpServletRequest request;

2. The most troublesome method

   a. Configure a listener in web.xml

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

 

   b. It can be used in the program later

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

3. The most direct method

1

public String hello(HttpServletRequest request,HttpServletResponse response) 

 

 

How to get a request object in Struts2

HttpServletRequest request = ServletActionContext.getRequest();

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325539383&siteId=291194637
Recommended