DWR入门级实例

一个很好学习DWR的入门级实例

配置、运行成功,也就明白DWR框架的原理:在客户端的javaScript直接调用服务器上java类中的方法

 

业务类:DWRTest.java

package vivi.test.dwr;
/**
 * @author vivi
 * DWR入门级 实例
 */
public class DWRTest {
	//DWRTest类的无参构造函数
	public DWRTest(){		
	}
	//将要被JavaScript调用的java方法
	public String getMyName(){
		return "Hello DWR";
	}
}

web.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
id="WebApp_ID" version="2.5">
  
  <display-name>project-dwr</display-name>
  
  <!-- The DWR servlet uk.ltd.getahead.dwr.DWRServlet -->
  <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-app>

dwr.xml配置文件(与web.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/dwr10.dtd">

<dwr>
	<allow>
		<create creator="new" javascript="MyTest" scope="application">
			<param name="class" value="vivi.test.dwr.DWRTest" />
		</create>
	</allow>
</dwr>

index.jsp 页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>DWR入门级实例</title>
			<script type='text/javascript' src='dwr/interface/MyTest.js'></script>   
			<script type='text/javascript' src='dwr/engine.js'></script>  
			<script type='text/javascript' src='dwr/util.js'></script>  
		<script language="javascript">
			var mycall=function callBack(data){  
				if (typeof window['DWRUtil'] == 'undefined'){
  					window.DWRUtil = dwr.util;
 				}			
				DWRUtil.setValue("demo1",data);  
			}  
			function showMyName(){  
				MyTest.getMyName(mycall);  
			}  
			function clearName(){  
				demo1.value="";  
			}  
		</script>
	</head>
	<body>
		<h1> DWR 入门级 实例 </h1>
		<input type="button" value="显示" onclick="showMyName();"> &nbsp;
		<input type="button" value="隐藏" onclick="clearName();"> <br>
		
		<input type="text" id="demo1"><br>  
	</body>
</html>
 

你可能感兴趣的:(JavaScript,java,javaee,servlet,DWR)