2013-03-09 33 views
0

我想知道什麼是獲得HttpServletRequest對象在DWR和Spring應用程序的正確方法 - 我嘗試做如下:獲得的HttpServletRequest在DWR和Spring應用程序

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它會告訴,這樣我就可以直接返回空值:

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; 
    } 
    ... 

回答

0

您可以使用DWR類如春豆,所以你並不需要一個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> 

這是你的Spring bean

<bean name="reportController" class="com.repsys.ajax.ReportController"> 
     <property name="reportService" ref="reportService"></property> 
    </bean> 
相關問題