SpringMVC request的获取问题

来源:互联网  时间:2016/7/17 12:04:35

关于网友提出的“ SpringMVC request的获取问题”问题疑问,本网通过在网上对“ SpringMVC request的获取问题”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:

问题: SpringMVC request的获取问题
描述:

springMVC 可以在父BaseController 里面使用

@Autowired
protected HttpServletRequest request;

获取request ?网上说springMVC 是单例模式会有问题,有没有哪位大师比较清楚这方面的,给个准确的答案,拜谢


解决方案1:

一般确实是单例,当前请求响应相关的状态应该从Controller方法参数,或通过Model来访问。一般获取request有两种方法。

1. 直接在控制器方法参数上声明,他在调用时会自己注入,这种方式简单,但是如果不是Controller而是调用函数内部需要的话,你就只能把对象传进去。

2. 可以在web.xml中声明监听器RequestContextListener



    org.springframework.web.context.request.RequestContextListener

然后在代码中需要的时候,可以通过RequestContextHolder的静态方法获取到HttpServletRequest等对象,比如专门写个工具类:

package org.fox.support.mvc;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * 获取上下文相关对象的静态工具
 *
 * @author Chen Rui
 */
public interface WebUtils {
    /* ----- ----- ----- ----- 请求响应对象获取 ----- ----- ----- ----- */
    /* NOTE 这些方法都依赖于Spring Web以及RequestContextListener的部署 */
    /**
     * 返回当前Servlet上下文的请求参数对象
     *
     * @return 返回当前Servlet上下文的请求参数对象
     */
    public static ServletRequestAttributes getRequestAttributes() {
        // NOTE 这里不使用getRequestAttributes()是为了防止RequestContextListener没有部署, 返回不可控的null值
        // NOTE currentRequestAttributes()在RequestContextListener未部署,取不到对象时,会直接抛出异常
        RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
        // 如果是ServletRequestAttributes,返回对象
        if (requestAttributes instanceof ServletRequestAttributes) {
            return (ServletRequestAttributes) requestAttributes;
        } else {
            // 一般出现这个异常原因是没有部署RequestContextListener且启用了JSF环境
            throw new IllegalStateException("当前上下文环境非Servlet环境");
        }
    }

    /**
     * 返回当前上下文的请求对象
     *
     * @return 返回当前上下文的请求对象
     */
    public static HttpServletRequest getRequest() {
        return getRequestAttributes().getRequest();
    }

    /**
     * 返回当前上下文的响应对象
     *
     * @return 返回当前上下文的响应对象
     */
    public static HttpServletResponse getResponse() {
        return getRequestAttributes().getResponse();
    }

    /**
     * 返回当前上下文Session Id
     *
     * @return 返回当前上下文Session Id
     */
    public static String getSessionId() {
        return getRequestAttributes().getSessionId();
    }

    /**
     * 返回当前上下文Session
     *
     * @param isCreate 如果session不存在, 是否创建
     * @return 返回当前上下文Session
     */
    public static HttpSession getSession(boolean isCreate) {
        return getRequest().getSession(isCreate);
    }

    /**
     * 获取Spring Web应用程序上下文
* 只能得到顶层WebApplicationContext, 而无法得到MVC DispatchServlet管理的ApplicationContext
* 建议优先考虑Spring提供的ApplicationContextAware * * @return 返回当前Spring Web应用程序上下文 */ public static WebApplicationContext getWebApplicationContext() { return WebApplicationContextUtils.getWebApplicationContext(getRequest().getServletContext()); } /** * 获取Servlet上下文
* 建议优先考虑Spring提供的ServletContextAware * * @return 返回当前Servlet上下文 */ public static ServletContext getServletContext() { return getRequest().getServletContext(); } }

解决方案2:

我就想问你,你怎么注册的HttpServletRequest类型的bean,你是不是想问参数绑定啊

上一篇请问一下给有关kendoui grid方面的问题
下一篇关于增删改查里面的修改问题
明星图片
相关文章
《 SpringMVC request的获取问题》由码蚁之家搜集整理于网络,
联系邮箱:mxgf168#qq.com(#改为@)