2013-03-09 55 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> 
相关问题