Spring AOP中如何获取HttpServletResponse?
如题~若是用
((ServletWebRequest) RequestContextHolder.getRequestAttributes()).getResponse();
就不用说了
这是拿不到的,会抛如下异常:
java.lang.ClassCastException: org.springframework.web.context.request.ServletRequestAttributes cannot be cast to org.springframework.web.context.request.ServletWebRequest
也不要用@args(response)这种方式来拿,因为切点方法没有带这个参数...
--------------------编程问答--------------------
//ServletRequestAttributes may be null, check it first.
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
这样试试 --------------------编程问答-------------------- @defondes
我是想要response啊。。。
不是request...
我想回写cookie... --------------------编程问答-------------------- sorry,看看这个
@Aspect--------------------编程问答-------------------- ServletWebRequest servletContainer = (ServletWebRequest)RequestContextHolder.getRequestAttributes();
public class MyAspect {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Pointcut("execution(public * com..controller.*Controller.*(..)) &&" +
"!execution(public * com..NotThisController.*(..))")
private void pointCut(){}
@Before("pointCut()")
public void beforeInit() throws Throwable {
ServletWebRequest servletContainer = (ServletWebRequest)RequestContextHolder.getRequestAttributes();
HttpServletRequest request = servletContainer.getRequest();
HttpServletResponse response = servletContainer.getResponse();
//do something
}
@After("pointCut()")
public void afterInit() throws Throwable {
ServletWebRequest servletContainer = (ServletWebRequest)RequestContextHolder.getRequestAttributes();
HttpServletRequest request = servletContainer.getRequest();
HttpServletResponse response = servletContainer.getResponse();
//do something
}
}
这个会抛异常:
error is java.lang.ClassCastException: org.springframework.web.context.request.ServletRequestAttributes cannot be cast to org.springframework.web.context.request.ServletWebRequest
我开题的时候已经说明了...
我的springMVC版本是 3.0.5.RELEASE --------------------编程问答-------------------- 这样试试
--------------------编程问答-------------------- 自己顶一下~ --------------------编程问答-------------------- 再帮自己顶一下~ --------------------编程问答-------------------- 就没人在aop里写cookie吗
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
ServletWebRequest servletWebRequest=new ServletWebRequest(request);
HttpServletResponse response=servletWebRequest.getResponse();
除了参数注入或者ThreadLocal就没别的办法? --------------------编程问答-------------------- 声明在方法参数里?
补充:Java , Web 开发