您的位置:首页 > 编程语言 > Java开发

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

<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( ) {}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: