0

我想知道在 DWR 和 Spring Application 中获取 HttpServletRequest 对象的正确方法是什么 - 我正在尝试执行以下操作:

private HttpServletRequest getRequest() {
    ServletRequestAttributes servletAttributes =
            (ServletRequestAttributes) RequestContextHolder
                    .getRequestAttributes();

    // this is Spring application's DispatcherServlet call
    if (servletAttributes != null) {
        return servletAttributes.getRequest();
    } else {
        if (WebContextFactory.get() == null) {
            // non-HttpRequest call
            return null;
        } else {
            // dwr call
            return WebContextFactory.get().getHttpServletRequest();
        }
    }
}

我问这个是因为当这个方法用完任何 http 上下文时,方法 WebContextFactory 会记录以下警告:

  WARN org.directwebremoting.WebContextFactory:39 - Missing WebContextBuilder. Is DWR setup properly?

我可能缺少可以判断此方法调用是否在 HttpServletRequest 中的方法,因此我可以直接返回 null 值:

 private HttpServletRequest getRequest() {
     // something like this would be ideal:
     if (!ServletContextFactory.isInServletContext()) {
         // method was not called from ServletRequest, so there is none to be found
         return null;
     }
     ...
4

1 回答 1

0

您可以将DWR类用作 spring bean,因此您不需要HttpServletRequest

例如,这是您的 dwr.xml

<?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE dwr PUBLIC
"-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN"
 "http://getahead.org/dwr/dwr30.dtd">
  <dwr>
   <allow>
    <create creator="spring" javascript="Report">
     <param name="beanName" value="reportController" />
    </create>
   </allow> 
 </dwr>

这是你的春豆

    <bean name="reportController" class="com.repsys.ajax.ReportController">
      <property name="reportService" ref="reportService"></property>
    </bean>
于 2013-03-14T05:25:53.587 回答