//Annotation configuration dwr servlet private void initializeDwrServlet(ServletContext container) { DwrServlet dwrServlet = new DwrServlet(); ServletRegistration.Dynamic dynamic = container.addServlet("dwr-invoker", dwrServlet ); dynamic.setLoadOnStartup(2); dynamic.setInitParameter("debug", "true"); dynamic.setInitParameter("pollAndCometEnabled", "true"); dynamic.setInitParameter("activeReverseAjaxEnabled", "true"); dynamic.setInitParameter("maxWaitAfterWrite", "1800000"); dynamic.setInitParameter("disconnectedTime", "60000"); dynamic.setInitParameter("classes", dwrAnnotationClasssConfiguration()); dynamic.addMapping("/dwr/*"); } // Configure Dwr Annotation Classes,comma separated private String dwrAnnotationClasssConfiguration() { StringBuffer config = new StringBuffer(); config.append("com.g360.bean.reconsole.opcost.AnnualOpCostRequestController"); config.append(","); config.append("com.g360.bean.security.SessionExpiredParam"); return config.toString(); }
上面個人配置是基於註解版的Dwr Servlet配置(將web.xml裏面的servlet配置轉換爲了純Java代碼配置),固然也包括前端和後端直接交互的類(javascript
com.g360.bean.reconsole.opcost.AnnualOpCostRequestController,com.g360.bean.security.SessionExpiredParam
前端
)也是使用註解配置的(Dwr註解配置,包括@RemoteProxy,@RemoteMethod),這對於有些註解配置基礎的同窗來講可能比較容易看懂,對於初次接觸的有什麼不懂的能夠給我留言哦;
java
com.g360.bean.security.SessionExpiredParam的註解配置以下所示:web
@Component("sessionExpiredParam") @RemoteProxy(creator = SpringCreator.class, scope = ScriptScope.APPLICATION, name = "remote", creatorParams = @Param(name = "beanName", value = "sessionExpiredParam")) public class SessionExpiredParam { private static final Logger LOGGER = LoggerFactory.getLogger(SessionExpiredParam.class); public boolean isSessionExpire (String sessionId) { return null == ConstantCacheService.getCacheConstant(sessionId); } @RemoteMethod public void setEnableDwrUpdate(String sessionId) { ScriptSession scriptSession = WebContextFactory.get().getScriptSession(); String jsessionId = scriptSession.getHttpSessionId(); String dwrSessionId = scriptSession.getId(); LOGGER.info(String.format("set jsessionId = [%s],dwrsession = [%s] push enabled",jsessionId,dwrSessionId)); ConstantCacheService.putCacheConstant(Constants.PUSH_ID+jsessionId, dwrSessionId); } }
這就是至關於dwr.xml配置文件中以下所示:後端
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN" "http://getahead.org/dwr/dwr30.dtd"> <dwr> <allow> <create creator="new" javascript="remote" scope="application"> <param name="class" value="com.g360.bean.security.SessionExpiredParam"/> </create> </allow> </dwr>
對於com.g360.bean.reconsole.opcost.AnnualOpCostRequestController來講道理同樣,這裏就很少作說明了;session
對於使用DwrServlet來講要使用純註解配置(不使用dwr.xml)必定要注意在初始化參數時必定要配置 classes這個參數,並且這個參數是這個註解類的全路徑名,有多個的話就使用逗號分隔,這個是特別重要的哦,由於Dwr3.0好像還不支持包掃描,不知道是須要什麼配置才能實現仍是怎麼的,若是有哪位仁兄正好看見,也正好知道的話請留言麻煩告訴一下,感激涕零啊!歡迎你們有什麼不明白的地方或者有什麼不對的地方,請隨時留言;
app