zoukankan      html  css  js  c++  java
  • 通过spring,在项目的任意位置获取当前Request

    需要引入:

    import javax.servlet.http.HttpServletRequest;
    import org.springframework.web.context.request.RequestContextHolder;
    import org.springframework.web.context.request.ServletRequestAttributes;

    使用方法:

    public static HttpServletRequest getRequest(){
        ServletRequestAttributes ra= (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request =  ra.getRequest();
        return request;
    }

    如果对安全有特别要求,做如下改进:

    /**
         * 
         * @Title: getCurrentRequest
         * @author:liuyx 
         * @date:2016年1月13日下午6:14:43
         * @Description: 获取当前request
         * @return
         * @throws IllegalStateException 当前线程不是web请求抛出此异常.
         */
        public static HttpServletRequest getCurrentRequest() throws IllegalStateException {
            ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            if (attrs == null) {
                throw new IllegalStateException("当前线程中不存在 Request 上下文");
            }
            return attrs.getRequest();
        }
  • 相关阅读:
    SVG:中国地图
    网页编程工具:EditPlus
    js插件
    html: 仿制soundmanager2右上角面板
    代码:页面布局(含图片列表布局)
    写着玩: 图片 圆盘
    表格
    按钮
    插件:左侧下拉菜单
    颜色
  • 原文地址:https://www.cnblogs.com/flying607/p/5128088.html
Copyright © 2011-2022 走看看