Dwr 简易配置

前端之家收集整理的这篇文章主要介绍了Dwr 简易配置前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

(1) 一个servlet 配置多个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">

	<servlet>
		<display-name>DWR Servlet</display-name>
		<servlet-name>dwr-invoker</servlet-name>
		<servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
		<init-param>
			<param-name>debug</param-name>
			<param-value>true</param-value>
		</init-param>
		<init-param>
			<param-name>config-1</param-name>
			<param-value>WEB-INF/dwr1.xml</param-value>
		</init-param>
		<init-param>
			<param-name>config-2</param-name>
			<param-value>WEB-INF/dwr2.xml</param-value>
		</init-param>
	</servlet>
	<servlet-mapping>
		<servlet-name>dwr-invoker</servlet-name>
		<url-pattern>/dwr/*</url-pattern>
	</servlet-mapping>
	
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>
在这种情况下,param-name的规范是必须以config开头,如果你没有配置param-name的话,那么将读取web下面的dwr.xml

(2) 多个servlet 配置一个或者多个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">


    <servlet>  
       <servlet-name>dwr-invoker</servlet-name>  
        <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>  
    </servlet>  
    <servlet>  
       <servlet-name>dwr-invoker1</servlet-name>  
       <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>  
       <init-param>  
         <param-name>config-admin</param-name>  
         <param-value>WEB-INF/dwr1.xml</param-value>  
       </init-param>  
       <init-param>  
         <param-name>debug</param-name>  
         <param-value>true</param-value>  
       </init-param>  
    </servlet>  
    <servlet-mapping>  
    <servlet-name>dwr-invoker</servlet-name>  
       <url-pattern>/dwr/*</url-pattern>  
    </servlet-mapping>  
    <servlet-mapping>  
       <servlet-name>dwr-invoker1</servlet-name>  
       <url-pattern>/dwr1/*</url-pattern>  
    </servlet-mapping>  

</web-app>

(3) Dwr 配置

<!DOCTYPE dwr PUBLIC
    "-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN"
    "http://www.getahead.ltd.uk/dwr/dwr10.dtd">


<dwr>
	<allow>
		<create creator="new" javascript="testClass">
			<param name="class" value="com.dwr.TestClass" />
			<include method="testMethod1" />
		</create>
	</allow>
</dwr>  

<allow> 标签包括可以暴露给 javascript 访问的东西。
<create> 标签中指定 javascript 中可以访问的 java 类,并定义 DWR 应当如何获得要进行远程的类的实例。 creator="new" 属性指定 java 类实例的生成方式, new 意味着 DWR 应当调用类的默认构造函数来获得实例,其他的还有 spring 方式,通过与 IOC 容器 Spring 进行集成来获得实例等等。 javascript=" testClass " 属性指定 javascript 代码访问对象时使用的名称
<param> 标签指定要公开给 javascript 的 java 类名。
<include> 标签指定要公开给 javascript 的方法。不指定的话就公开所有方法
<exclude> 标签指定要防止被访问的方法

(4) JavaScript 中传递参数是对象的dwr配置

a dwr.xml

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

<dwr>
  <allow>
  <convert converter="bean" match="com.bean.User"/>
      <create creator="new" javascript="DwrService">
      <param name="class" value="com.service.DwrService"/>
    </create>
 </allow>
</dwr>
<!-- 上下两种配置都是可行的 -->
<!-- 
    <dwr>  
     <allow>  
      <create creator="new" javascript="DwrService" >  
       <param name="class" value="com.service.DwrService" />  
       <include method="addUser"/>  
      </create>  
      <convert converter="bean" match="com.bean.User">  
       <param name="include" value="userName,passWord" />  
      </convert>  
     </allow>  
    </dwr>    -->
b 页面传递 user
<html>
<head>
<script type='text/javascript' src='/DWR/dwr/interface/DwrService.js'></script>
<script type='text/javascript' src='/DWR/dwr/engine.js'></script>
<script type='text/javascript' src='/DWR/dwr/util.js'></script>

<script LANGUAGE=javascript>
<!--
function addUser() {
 var user={userName:null,passWord:null};
 dwr.util.getValues(user);
 var userName = dwr.util.getValue("userName");
 var passWord = dwr.util.getValue("passWord");
 alert(userName+passWord);
 //调用java方法,并获得返回值;
  DwrService.addUser(user,function(data) {
  dwr.util.setValue("userInfo",data);
  });
}//-->
</script>
</head>
<body>
<hr>
name:<input type="text" id="userName"/>
<br>
passWord:<input type="text" id="passWord"/>
<br>
<input value="submit" type="button" onclick="addUser()" />
<br>
<span id="userInfo"></span>
</body>
</html> 
说明:dwr.util.getValues(Object);如同dwr.util.getValue("id")方法,用来将页面中的元素内容复制到变量中,与getValue不同getValues方法不返回值内容而是修改参数Object中的信息。Object 中的内容是一些键值对,键对应到页面元素的id属性,值对应元素的内容。我们可以通过将页面元素的id设置为Bean的属性,然后通过该方法轻松的获取用户的输入信息。
与读取属性相对的是设置页面元素的属性:setValue()和setValues()

(5) 完整的一个dwr项目

(a) 项目清单


(b) Class 源码

package cn.utl.dwr;

public class Dwr01 {
public void getName(String demo){
	System.out.println(demo);
}
public String getUrl(String name){
	System.out.println("*************");
	return name;
}
}
package com.bean;

public class User {
private String userName;
private String passWord;
public String getUserName() {
	return userName;
}
public void setUserName(String userName) {
	this.userName = userName;
}
public String getPassWord() {
	return passWord;
}
public void setPassWord(String passWord) {
	this.passWord = passWord;
}
public User() {
}
}
package com.service;

import java.io.IOException;

import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;

import com.bean.User;

import javax.servlet.ServletException;


public class DwrService {
 public String check(String name){
  //访问数据库验证信息
  if(name.startsWith("aa")){
   return "用户已经存在";
  }
  else{
   return "可以使用该用户";
  }
 }
 public String getInclude() throws ServletException,IOException
 {
     return WebContextFactory.get().forwardToString("/test.jsp");
 }
 
 public String addUser(User user){
	 System.out.println("addUser!");
	 System.out.println(user.getUserName()+user.getPassWord());
  //将用户信息添加数据库中
     return "Name:"+user.getUserName()+";Password="+user.getPassWord();
 }
 

}

(c) dwr 配置

dwr.xml (舍弃了)

<!DOCTYPE dwr PUBLIC
    "-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN"
    "http://www.getahead.ltd.uk/dwr/dwr10.dtd">
 
<dwr>
  <allow>
      <create creator="new" javascript="JDate">
      <param name="class" value="cn.utl.dwr.Dwr01"/>
      <include method="getUrl"/>
      <include method="getName"/>
    </create>
  </allow>
</dwr>
dwr1.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE dwr PUBLIC
    "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN"
    "http://getahead.org/dwr/dwr20.dtd">

<dwr>
  <allow>
  <convert converter="bean" match="com.bean.User"/>
      <create creator="new" javascript="DwrService">
      <param name="class" value="com.service.DwrService"/>
    </create>
 </allow>
</dwr>
<!-- 上下两种配置都是可行的 -->
<!-- 
    <dwr>  
     <allow>  
      <create creator="new" javascript="DwrService" >  
       <param name="class" value="com.service.DwrService" />  
       <include method="addUser"/>  
      </create>  
      <convert converter="bean" match="com.bean.User">  
       <param name="include" value="userName,passWord" />  
      </convert>  
     </allow>  
    </dwr>    -->

dwr2.xml

<!DOCTYPE dwr PUBLIC
    "-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN"
    "http://www.getahead.ltd.uk/dwr/dwr10.dtd">
 
<dwr>
  <allow>
      <create creator="new" javascript="JDate">
      <param name="class" value="cn.utl.dwr.Dwr01"/>
      <include method="getUrl"/>
      <include method="getName"/>
    </create>
  </allow>
</dwr>

(d) web 配置

<?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">

	<servlet>
		<display-name>DWR Servlet</display-name>
		<servlet-name>dwr-invoker</servlet-name>
		<servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
		<init-param>
			<param-name>debug</param-name>
			<param-value>true</param-value>
		</init-param>
		<init-param>
			<param-name>config-1</param-name>
			<param-value>WEB-INF/dwr1.xml</param-value>
		</init-param>
		<init-param>
			<param-name>config-2</param-name>
			<param-value>WEB-INF/dwr2.xml</param-value>
		</init-param>
	</servlet>
	<servlet-mapping>
		<servlet-name>dwr-invoker</servlet-name>
		<url-pattern>/dwr/*</url-pattern>
	</servlet-mapping>
	
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

(e) 页面

① 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>My JSP 'index.jsp' starting page</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 language="javascript" src="/DWR/dwr/interface/JDate.js"></script>
<script language="javascript" src='/DWR/dwr/engine.js'></script>
<script language="javascript" src='/DWR/dwr/util.js'></script>
<script language="javascript">
function doTest() {
    //JDate.getName('123');
    JDate.getUrl('传入参数',callbackJdte);
}
function callbackJdte(date){
document.getElementById("div").innerHTML+='传出参数:'+date;
}
function load(data) {
    window.alert("Current Time:"+data);
}
</script>
  </head>
  <body>
   <input type="button" name="count" value="cont" onClick="doTest()">
   <div id="div"></div>
  </body>
</html>

说明:js文件中的使用

做完以上工作我们可以在js中使用它远程调用方法了如下所示:

图中前3行代码的意思分别是:

<script type='text/javascript' src='/somcdemo/dwr/interface/DwrTime.js'></script>

表示引入/somcdemo/dwr/interface/DwrTime.js 这个js是根据你所写的类自动生成的,也是必须引入的。

<script type='text/javascript' src='/somcdemo/dwr/engine.js'></script>

表示引入/somcdemo/dwr/engine.js 这个js是dwr的jar文件自带的,如果你想程序正常运行就必须引入。

<script type='text/javascript' src='/somcdemo/dwr/util.js'></script>

表示引入/somcdemo/dwr/util.js,这个js也是dwr的jar文件自带的,它提供了一些好用的方法,如果你想使用也要引入.


② Convertor.html

<html>
<head>
<script type='text/javascript' src='/DWR/dwr/interface/DwrService.js'></script>
<script type='text/javascript' src='/DWR/dwr/engine.js'></script>
<script type='text/javascript' src='/DWR/dwr/util.js'></script>

<script LANGUAGE=javascript>
<!--
function addUser() {
 var user={userName:null,data);
  });
}//-->
</script>
</head>
<body>
<hr>
name:<input type="text" id="userName"/>
<br>
passWord:<input type="text" id="passWord"/>
<br>
<input value="submit" type="button" onclick="addUser()" />
<br>
<span id="userInfo"></span>
</body>
</html> 

(6) 相关资源文章

dwr 传递参数类型大全

DWR中的Convert配置学习

猜你在找的Ajax相关文章