当前位置: 代码迷 >> Web前端 >> 运用DWR完成HelloWorld
  详细解决方案

运用DWR完成HelloWorld

热度:302   发布时间:2012-10-18 13:46:55.0
使用DWR完成HelloWorld.

一、到DWR的官方网站上下载dwr.jar包。将其放到项目的lib目录下。开始做第一个dwr程序。

二、web.xml配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	
	<display-name>dwrtest</display-name>
	<servlet>
		<servlet-name>dwr-invoke</servlet-name>
		<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
		<init-param>
			<param-name>debug</param-name>
			<param-value>true</param-value>
		</init-param>
	</servlet>
	
	<servlet-mapping>
		<servlet-name>dwr-invoke</servlet-name>
		<url-pattern>/dwr/*</url-pattern>
	</servlet-mapping>
	
  <welcome-file-list>
    <welcome-file>Index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

?

三、在web.xml同目录下新建dwr.xml文件,内容如下:

<?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 javascript="JDate" creator="new">
			<param name="class" value="java.util.Date"/>
		</create>
		<create javascript="Hello" creator="new">
			<param name="class" value="com.dwr.HelloWorld"/>
		</create>
	</allow>
</dwr>  

?

四、Index.jsp内容如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>DWR's  HelloWorld</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<script type='text/javascript' src='dwr/interface/Hello.js'></script>
	<script type='text/javascript' src='dwr/engine.js'></script>
	<script type='text/javascript' src='dwr/util.js'></script>
	<script type='text/javascript' src='hello.js'></script>
	
  </head>
  
  <body>
   	<input id="user" type="text"/><input type="button" value="您好" onClick="hello()"><div id="result"></div>
  </body>
</html>

?

五、在Index.jsp同目录下新建hello.js文件,内容如下:

function hello(){
	var user = $('user').value;
	Hello.sayHello(user,callback);
}

function callback(msg){
	
	if (typeof window['DWRUtil'] == 'undefined')
    	window.DWRUtil = dwr.util; 
	DWRUtil.setValue('result',msg);
}

?六、部署运行程序,我的可以看到结果。

1 楼 bo_hai 2011-02-12  
接下来开始分析dwr的工作原理。
2 楼 bo_hai 2011-02-12  
DWR的工作原理是通过动态把JAVA生成为JavaScript。它的代码就像Ajax魔法一样,你感觉调用就像发生在浏览器端,但是实际上代码调用发生在服务端,DWR负责数据的传递和转换。这种从JAVA到JavaScript的远程调用功能的方式使用DWR用起来有种非常橡RMI或者SOAP的常规RPC机制,而且DWR的优点在于不需要任何的网页浏览器插件就能运行在网页上。
  相关解决方案