来自POJO的HttpServletResponse使用spring

时间:2013-10-23 15:27:09

标签: java spring httpresponse pojo

Source

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

public class SimplePOJO {

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

//Want to send HttpServletResponse 

}

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

1 个答案:

答案 0 :(得分: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/