1

资源

如何在 Spring Framework 的 POJO 类中获取 HttpServletRequest 对象?

public class SimplePOJO {

//Receive Request 
ServletRequestAttributes servReq= (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
HttpServletRequest req = servReq.getRequest(); 

//Want to send HttpServletResponse 

}

我们可以从同一个 POJO发送HttpServletResponse吗?像上面一样

4

1 回答 1

2

您可以在任何 Spring bean 中获取 HttpServletRequest 对象,但您必须将 bean 的范围限定为如下所示。

<bean id="simplePOJOBean" class="SimplePOJO" scope="request"/>


 public class SimplePOJO {

 private @Autowired HttpServletRequest request;

 //Just use the autowired request object


 }

无法在服务层获取 http 响应对象,但是您可以通过其他方式进行。这些东西已经在这里很好地解释了http://jawspeak.com/2010/05/06/injecting-httpservletresponse-into-spring-mvc-request-scoped-interceptors/

于 2013-10-23T15:50:34.570 回答