在切面处获取 HttpServletRequest 的方法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/HeatDeath/article/details/80339061

切面处获取

        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
        HttpServletRequest request = servletRequestAttributes.getRequest();

web.xml

方案一:

<filter>
    <filter-name>springRequestFilter</filter-name>
    <filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>springRequestFilter</filter-name>
    <url-pattern>/your-url/*</url-pattern>
</filter-mapping>

方案二:

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

参考资料:

1、null pointer exception from Session
https://stackoverflow.com/questions/13168648/null-pointer-exception-from-session

2、@Spring MVC 中几种获取request和response的方式
https://www.cnblogs.com/winner-0715/p/6270513.html


阅读更多
换一批

没有更多推荐了,返回首页