dwr在spring容器中的应用举例1
2013-01-07 13:24
232 查看
DWR(Direct Web Remoting)是一个用于改善web页面与Java类交互的远程服务器端Ajax开源框架(客户端ajax的原理),可以帮助开发人员开发包含AJAX技术的网站。
DWR采取了一个类似AJAX的新方法来动态生成基于JAVA类的JavaScript代码。这样WEB开发人员就可以在JavaScript里使用Java代码。
在spring容器中如何使用(案例如下):
一、引入dwr相关的jar包。
二、在需要的页面引入必备的js
三、在web.xml中的配置(配置DWR自动加载哪些Java类:这里是自己需要的类,其他都不变)
四、可在WEB-INF下配置一个controller-servlet.xml文件
五、在相应的controller类中(如一中的com.lwy.chat.controller.ChatController)注解
六、将相应的controller类作为一个js引入到需要的jsp中(以第四步中@RemoteProxy的name作为js的名称)
path为系统路径,<%=path%>/dwr/interface/*.js,*处替代掉即可。
七、在controller类中的方法若要用在js中需为其注解(@RemoteMethod)
八、上述中的方法saveMsg中可调用js中的function函数。
调用dwr框架的controller中的@RemoteMethod中的service接口不能通过注解得到。
可通过ApplicationContextUtil.getBean("chatService");如下注释掉的代码
九、上述几步都完成后,就可在js中通过调用java中的方法:如Chat.saveMsg。
Chat的由来:@RemoteProxy(creator=NewCreator.class, name="Chat")
saveMsg的由来: @RemoteMethod public ChatView saveMsg( ) {}
DWR采取了一个类似AJAX的新方法来动态生成基于JAVA类的JavaScript代码。这样WEB开发人员就可以在JavaScript里使用Java代码。
在spring容器中如何使用(案例如下):
一、引入dwr相关的jar包。
二、在需要的页面引入必备的js
<script type="text/javascript" src="dwr/engine.js"></script> <script type="text/javascript" src="dwr/util.js"></script>
三、在web.xml中的配置(配置DWR自动加载哪些Java类:这里是自己需要的类,其他都不变)
<servlet> <!-- 指定DWR核心Servlet的名字 --> <servlet-name>dwr-invoker</servlet-name> <!-- 指定DWR核心Servlet的实现类 --> <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class> <!-- 指定DWR核心Servlet处于调试状态 --> <init-param> <param-name>debug</param-name> <param-value>false</param-value> </init-param> <init-param> <param-name>activeReverseAjaxEnabled</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>crossDomainSessionSecurity</param-name> <param-value>false</param-value> </init-param> <init-param> <param-name>allowScriptTagRemoting</param-name> <param-value>true</param-value> </init-param> <!-- 配置DWR自动加载哪些Java类 --> <init-param> <param-name>classes</param-name> <param-value> com.lwy.msg.domain.model.Msgtips, com.lwy.msg.controller.MsgtipsController, com.lwy.chat.view.ChatView, com.lwy.chat.controller.ChatController </param-value> </init-param> </servlet> <!-- 指定核心Servlet的URL映射 --> <servlet-mapping> <servlet-name>dwr-invoker</servlet-name> <!-- 指定核心Servlet映射的URL --> <url-pattern>/dwr/*</url-pattern> </servlet-mapping>
四、可在WEB-INF下配置一个controller-servlet.xml文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:annotation-config /> </beans>
五、在相应的controller类中(如一中的com.lwy.chat.controller.ChatController)注解
@Controller//控制类必须的注解 @RequestMapping("/chat")//请求路径的一部分 @RemoteProxy(creator=NewCreator.class, name="Chat")//dwr public class ChatController { }
六、将相应的controller类作为一个js引入到需要的jsp中(以第四步中@RemoteProxy的name作为js的名称)
path为系统路径,<%=path%>/dwr/interface/*.js,*处替代掉即可。
<script type='text/javascript' src='<%=path%>/dwr/interface/Chat.js'></script>
七、在controller类中的方法若要用在js中需为其注解(@RemoteMethod)
@RemoteMethod public ChatView saveMsg( ) {}
八、上述中的方法saveMsg中可调用js中的function函数。
调用dwr框架的controller中的@RemoteMethod中的service接口不能通过注解得到。
可通过ApplicationContextUtil.getBean("chatService");如下注释掉的代码
@RemoteMethod public ChatView saveMsg( ) { //略。可添加自己所需的功能代码 WebContext wctx = WebContextFactory.get(); //ChatService chatService = (ChatService) ApplicationContextUtil.getBean("chatService"); //chatService.saveMessage(chatView); String currentPage = wctx.getCurrentPage(); Collection<ScriptSession> sessions = wctx.getScriptSessionsByPage(currentPage); ScriptProxy s = new ScriptProxy(sessions); s.addFunctionCall("Chat.ReceiveMsg", chatView);//调用js中的函数Chat.ReceiveMsg。chatView为传到函数中的参数 return chatView; }
九、上述几步都完成后,就可在js中通过调用java中的方法:如Chat.saveMsg。
Chat的由来:@RemoteProxy(creator=NewCreator.class, name="Chat")
saveMsg的由来: @RemoteMethod public ChatView saveMsg( ) {}
相关文章推荐
- spring容器获取DataSource对象,进行简单数据库应用
- Spring.NET在非Web中的应用,以及封装Spring容器。
- Spring基础篇——Spring容器和应用上下文理解
- 解决spring-cloud应用无法部署在同一个容器中的问题
- Docker容器及Spring Boot微服务应用
- ServletContext与Web应用以及Spring容器启动
- 在Web应用中创建Spring容器的两种方式
- Spring容器高级主题(精通Spring+4.x++企业应用开发实战 五)
- SpringBoot之在Servlet2.5容器中部署war应用
- Spring在非web应用中关闭IoC容器 (registerShutdownHook)
- 在Web应用中创建Spring容器的两种方式
- SpringJdbc的应用举例
- Spring在非web应用中优雅地关闭Spring IoC容器
- 在Web应用中创建Spring容器的两种方式
- 学习《spring 3.x企业应用开发实战》之IOC容器概述
- Spring.NET在非Web中的应用,以及封装Spring容器。
- 《Spring 3.x 企业应用开发实战》学习笔记 第三章 IoC容器概述 3.5 Bean的生命周期
- Docker容器及Spring Boot微服务应用
- Bean(应用组件)在Spring容器中的生命周期
- 小试牛刀~关于(struts2 + spring + hibernate + ibatis + dwr + memcached +mysql + jboss集群)的小实践应用~