//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代码配置),当然也包括前端和后端直接交互的类(
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好像还不支持包扫描,不知道是需要什么配置才能实现还是怎么的,如果有哪位仁兄正好看见,也正好知道的话请留言麻烦告诉一下,感激不尽啊!欢迎大家有什么不明白的地方或者有什么不对的地方,请随时留言;