//Annotation configuration dwr servletprivate 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 separatedprivate 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代碼配置),當然也包括前端和后端直接交互的類(
com.g360.bean.reconsole.opcost.AnnualOpCostRequestController,com.g360.bean.security.SessionExpiredParam
)也是使用注解配置的(Dwr注解配置,包括@RemoteProxy,@RemoteMethod),這對于有些注解配置基礎的同學來說可能比較容易看懂,對于初次接觸的有什么不懂的可以給我留言哦;
com.g360.bean.security.SessionExpiredParam的注解配置如下所示:
@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來說道理一樣,這里就不多做說明了;
對于使用DwrServlet來說要使用純注解配置(不使用dwr.xml)一定要注意在初始化參數時一定要配置 classes這個參數,而且這個參數是這個注解類的全路徑名,有多個的話就使用逗號分隔,這個是特別重要的哦,因為Dwr3.0好像還不支持包掃描,不知道是需要什么配置才能實現還是怎么的,如果有哪位仁兄正好看見,也正好知道的話請留言麻煩告訴一下,感激不盡啊!歡迎大家有什么不明白的地方或者有什么不對的地方,請隨時留言;
新聞熱點
疑難解答