//Annotationconfigurationdwrservlet privatevoidinitializeDwrServlet(ServletContextcontainer){ DwrServletdwrServlet=newDwrServlet(); ServletRegistration.Dynamicdynamic=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/*"); } //ConfigureDwrAnnotationClasses,commaseparated privateStringdwrAnnotationClasssConfiguration(){ StringBufferconfig=newStringBuffer(); config.append("com.g360.bean.reconsole.opcost.AnnualOpCostRequestController"); config.append(","); config.append("com.g360.bean.security.SessionExpiredParam"); returnconfig.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")) publicclassSessionExpiredParam { privatestaticfinalLoggerLOGGER=LoggerFactory.getLogger(SessionExpiredParam.class); publicbooleanisSessionExpire(StringsessionId) { returnnull==ConstantCacheService.getCacheConstant(sessionId); } @RemoteMethod publicvoidsetEnableDwrUpdate(StringsessionId) { ScriptSessionscriptSession=WebContextFactory.get().getScriptSession(); StringjsessionId=scriptSession.getHttpSessionId(); StringdwrSessionId=scriptSession.getId(); LOGGER.info(String.format("setjsessionId=[%s],dwrsession=[%s]pushenabled",jsessionId,dwrSessionId)); ConstantCacheService.putCacheConstant(Constants.PUSH_ID+jsessionId,dwrSessionId); } }
这就是相当于dwr.xml配置文件中如下所示:
<!DOCTYPEdwrPUBLIC "-//GetAheadLimited//DTDDirectWebRemoting3.0//EN" "http://getahead.org/dwr/dwr30.dtd"> <dwr> <allow> <createcreator="new"javascript="remote"scope="application"> <paramname="class"value="com.g360.bean.security.SessionExpiredParam"/> </create> </allow> </dwr>
对于com.g360.bean.reconsole.opcost.AnnualOpCostRequestController来说道理一样,这里就不多做说明了;
对于使用DwrServlet来说要使用纯注解配置(不使用dwr.xml)一定要注意在初始化参数时一定要配置 classes这个参数,而且这个参数是这个注解类的全路径名,有多个的话就使用逗号分隔,这个是特别重要的哦,因为Dwr3.0好像还不支持包扫描,不知道是需要什么配置才能实现还是怎么的,如果有哪位仁兄正好看见,也正好知道的话请留言麻烦告诉一下,感激不尽啊!欢迎大家有什么不明白的地方或者有什么不对的地方,请随时留言;
原文链接:https://www.f2er.com/ajax/162247.html