dwr的介绍和实例

说明:

它包含两个主要的部分:允许JavaScript从WEB服务器上一个遵循了AJAX原则的Servlet中获取数据.另外一方面一个JavaScript库可以帮助网站开发人员轻松地利用获取的数据来动态改变网页的内容.

DWR采取了一个类似AJAX的新方法来动态生成基于JAVA类的JavaScript代码。这样WEB开发人员就可以在JavaScript里使用Java代码,就像它们是浏览器的本地代码(客户端代码)一样;但是Java代码运行在WEB服务器端而且可以自由访问WEB 服务器的资源。出于安全的理由,WEB开发者必须适当地配置哪些Java类可以安全的被外部使用。
这个从JAVA到JavaScript的远程功能方法给DWR的用户带来非常像传统的RPC机制,就像RMI或者SOAP一样,而且拥有运行在WEB上但是不需要浏览器插件的好处.

DWR不认为浏览器/WEB服务器协议是重要的,而更乐于保证编程界面的简单自然.对此最大的挑战就是把AJAX的异步特性和正常JAVA方法调用的同步特性相结合.在异步模式下,结果数据在开始调用之后的一段时间之后才可以被异步访问获取到.DWR允许WEB开发人员传递一个回调函数,来异步处理Java函数调用过程.

基本步骤:

1,下载dwr的jar包.
2,将jar包复制到WEB-INF目录下的lib文件夹下.
3,在web.xml中注册dwr的一个servlet.
4,写相关的javaBean业务操作类及方法.
5,写配置文件,取名为dwr.xml,与web.xml同一个目录下,注册将要操作的javaBean.

6.页面引入相应js。

简单demo

在web.xml中注册servlet

<servlet>     
<servlet-name>dwr-invoker</servlet-name>     
<servlet-class>uk.ltd.getahead.dwr.DWRServlet<!--此处必须这样写-->    
</servlet-class>    
</servlet>    
<servlet-mapping>    
<servlet-name>dwr-invoker</servlet-name>     
<url-pattern>/dwr/*</url-pattern>    
</servlet-mapping> 

web.xml的同目中创建dwr.xml

<?xml version="1.0" encoding="UTF-8"?>     
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://www.getahead.ltd.uk/dwr/dwr20.dtd">    
<dwr>    
	<allow> 
		<!--此处将pojos包下的所有javaBean转换为javascript对象使用-->    
		<convert match="org.hj.pojos.*" converter="bean"></convert>
		<!-- javascript的对象是user,create=“new”是DWR自己创建UserDelegate这个类的实例,其他的还有spring方式,通过与IOC容器Spring进行集成 -->    
		<create javascript="user" creator="new">    
			<param name="class" value="org.hj.delegate.UserDelegate"></param> 
			<!-- include表示客户端可以通过user调用服务器对象的方法,如果不写,则表示可调用这个类的所有方法.可写可不写--> 
			<include method="isLogin"/> 
			<!--此处表示不允许调用的一个方法,可写可不写-->    
			<exclude method="save"/>
		</create>    
	</allow>    
</dwr> 
业务类:

public class UserDelegate {    
private static UsersDao dao=new UsersDao();    
public boolean isLogin(String name,String pwd){    
Users u=new Users();    
u.setUsername(name);    
u.setPwd(pwd);    
return dao.isLogin(u);//(略...)    
}}..............  
jsp页面调用:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>    
<html>    
<!--引入dwr根据dwr.xml中的javascript="user"此处,自动生动个user的js对象供使用-->    
<script type="text/javascript" src="${pageContext.request.contextPath}/dwr/interface/user.js"></script>    
<!--引入dwr的util.js-->    
<script type="text/javascript" src="${pageContext.request.contextPath}/dwr/util.js"></script>    
<!--引入dwr的engine.js-->    
<script type="text/javascript" src="${pageContext.request.contextPath}/dwr/engine.js"></script>    
</head>  
<body>    
用户名:<input type="text" id="usename" />    
密 码:<input type="password" id="pwd"/>    
<input type="button" value="登录" οnclick="login()" />    
</body>    
<script type="text/javascript">    
function login(){    
var usename=dwr.util.getValue("usename"); <!--通过dwr中的util得到文本框的值,必须是id的值-->  
var pwd=dwr.util.getValue("pwd");    
<!--此处为user对象远程调用服务器的isLogin方法,传参,还一个返回值,这里用一个匿名的回调函数来处理-->  
user.isLogin(usename,pwd,function(isLogin){    
if(isLogin)    
locatio.href="list.jsp"; (博客不支持,应该为location)    
else{    
alert("用户名或密码错误");    
return false;    
}    
});    
}    
</script>    
</html>

工程部分代码:

  <listener>
    <listener-class>org.directwebremoting.servlet.DwrListener</listener-class>
  </listener>

  <servlet>
    <servlet-name>dwr-invoker</servlet-name>
    <servlet-class>org.directwebremoting.spring.DwrSpringServlet</servlet-class>
	
	<init-param>
     <param-name>fileUploadMaxBytes</param-name>
     <param-value>25000</param-value>
    </init-param>
	
    <!-- This should NEVER be present in live -->
    <init-param>
      <param-name>debug</param-name>
      <param-value>true</param-value>
    </init-param>
  
	<init-param>
      <param-name>accessLogLevel</param-name>
      <param-value>runtimeexception</param-value>
    </init-param>
    
    <!-- Remove this unless you want to use active reverse ajax -->
    <init-param>
      <param-name>activeReverseAjaxEnabled</param-name>
      <param-value>true</param-value>
    </init-param>

    <!-- By default DWR creates application scope objects when they are first
    used. This creates them when the app-server is started -->
    <init-param>
      <param-name>initApplicationScopeCreatorsAtStartup</param-name>
      <param-value>true</param-value>
    </init-param>

    <!-- WARNING: allowing JSON-RPC connections bypasses much of the security
    protection that DWR gives you. Take this out if security is important -->
    <init-param>
      <param-name>jsonRpcEnabled</param-name>
      <param-value>true</param-value>
    </init-param>

    <!-- WARNING: allowing JSONP connections bypasses much of the security
    protection that DWR gives you. Take this out if security is important -->
    <init-param>
      <param-name>jsonpEnabled</param-name>
      <param-value>true</param-value>
    </init-param>

    <!-- data: URLs are good for small images, but are slower, and could OOM for
    larger images. Leave this out (or keep 'false') for anything but small images -->
    <init-param>
      <param-name>preferDataUrlSchema</param-name>
      <param-value>false</param-value>
    </init-param>

    <load-on-startup>1</load-on-startup>
  </servlet>
   
  <servlet-mapping>
    <servlet-name>dwr-invoker</servlet-name>
    <url-pattern>/dwr/*</url-pattern>
  </servlet-mapping> 
spring 文件

    <bean id="pushMonitorInfo" class="com.cnit.eciqintf.datamonitor.datamonitor.pushmonitorinfo.PushMonitorInfoImpl">   
    	
     <dwr:remote javascript="pushMonitorInfo">    
       <dwr:include method="pushMonitorInfo" />  
       <dwr:include method="addAttributeToScriptSession" />  
       <dwr:include method="removeAttributeToScriptSession" />      
     </dwr:remote>  
    </bean>  


dwr文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN" "http://getahead.org/dwr/dwr30.dtd">

<dwr>
  <allow>
	 <create creator="spring" javascript="pushMonitorInfo" scope="application">  
	   <param name="beanName" value="pushMonitorInfo"/>   
	 </create>    
  </allow>
</dwr>
页面引入js

  <script type='text/javascript' src='<%=request.getContextPath()%>/dwr/engine.js'> </script>
  <script type='text/javascript' src='<%=request.getContextPath()%>/dwr/util.js'> </script>
  <script type='text/javascript' src='<%=request.getContextPath()%>/dwr/interface/pushMonitorInfo.js'> </script>
</pre><pre code_snippet_id="1972298" snippet_file_name="blog_20161108_11_1566119" name="code" class="html">      window.οnlοad=function()
      {
      	   dwr.engine.setActiveReverseAjax(true);
		   dwr.engine.setNotifyServerOnPageUnload(true);
		   //pushMonitorInfo.pushMonitorInfo();
 	       addAttributeToScriptSession();
      }









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值