0

DWRとSpringApplicationで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クラスをSpringBeanとして使用できるため、 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 に答える