dwr的基本使用,异常处理,和spring集成方法

以前oracle的ilearning是使用applet实现SCORM,我们系统主要使用了ajax方法,需要使用dwr:

dwr基本示例:

需要使用dwr-2.0.8.jar和commons-logging-1.1.1.jar两个包

新建一个web工程

新建java类:

package com.lai.sutdy.dwr;

import java.io.IOException;  

import javax.servlet.ServletException;  
  
import org.directwebremoting.WebContext;  
import org.directwebremoting.WebContextFactory;  
  
/** 
 * Some simple text demos 
 * @author Joe Walker [joe at getahead dot ltd dot uk] 
 */  
public class Demo  
{  
    /** 
     * Return a server side string to display on the client in real time 
     * @param name The name of person to say hello to 
     * @return A demo string 
     * @throws Exception  
     */  
    public String sayHello(String name) throws SessionTimeOutException,Exception  
    {  
      
    /*  System.out.println("342455=");
      
      正常处理
      Integer value = Integer.valueOf(name);  
      return "Hello4, " + value.toString();  */
      
     /* 
      *  代码异常
      Integer value = null;  
        System.out.println("rs2="+value.intValue());
        int id = value.intValue()+100;  
        return "Hello2223, " + id; */
      
      //手动指定抛出异常
      throw new SessionTimeOutException("超时错误..");
    }  
  
}  



web.xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<display-name>Dwr</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
	
	
	
	<servlet>  
    <servlet-name>dwr-invoker</servlet-name>  
    <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>  
  
    <!-- This should NEVER be present in live -->  
    <init-param>  
      <param-name>debug</param-name>  
      <param-value>true</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>  
  
    <!-- This enables full streaming mode. It's probably better to leave this  
    out if you are running across the internet -->  
    <init-param>  
      <param-name>maxWaitAfterWrite</param-name>  
      <param-value>-1</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>  
  
  
</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://getahead.org/dwr/dwr20.dtd">  
  
<dwr>  
  
  <allow>  
  
    <!-- 类转化 -->  
    <create creator="new" javascript="Demo">  
      <param name="class" value="com.lai.sutdy.dwr.Demo"/>  
    </create>  
    
    <!-- 异常处理 -->  
    <convert converter="exception" match="com.lai.sutdy.dwr.SessionTimeOutException"> 
        <param name='include' value='message,lineNumber'/> 
   </convert> 

    <!-- 异常处理  -->  
    <convert converter="exception" match="java.lang.Exception">  
        <param name='include' value='message,lineNumber'/>
    </convert>  
    <convert converter="bean" match="java.lang.StackTraceElement"/>  
  
  </allow>  
  
</dwr>  


jsp页面:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />  


<script type='text/javascript' src='/dwrStudy/dwr/engine.js'> </script>  
<script type='text/javascript' src='/dwrStudy/dwr/util.js'> </script>  
<script type='text/javascript' src='/dwrStudy/dwr/interface/Demo.js'> </script>  

<script type="text/javascript">

//下面为同一异常提示处理
DWREngine.setErrorHandler(dispatchErr);  
function dispatchErr( msg , ex )  
{  
	if(msg!=""){
      alert("info="+ msg );  
    }else{
      alert("空指针错误");
    }
}  



function update() {  
	  var name = dwr.util.getValue("demoName");  
	  Demo.sayHello(name,{   
	            callback:getData
	        /* 这里为另外一种异常处理方法
	         ,errorHandler:errorHandler,  
        exceptionHandler:exceptionHandler  
	        function(errorString,exception) { 
	            alert('error:'+errorString); 
	        }, 
	        exceptionHandler:function(errorString,exception) { 
	            alert('exception:'+errorString + ":" + exception.message); 
	        }*/  
	});  
	}  
	  
	  
	function getData(data){  
	    dwr.util.setValue("demoReply", data);  
	}  
	  
	function errorHandler(errorString, exception){ 
		//alert("lai excption!");
	 alert(exception.message);  
	}  
	function exceptionHandler(exceptionString, exception){  
		//alert("lai excption2222!");
	 alert(exception.message);  
	}  

</script>  
  
<div id="demoDiv">  
  
  <p>  
    Name:  
    <input type="text" id="demoName" value="Joe"/>  
    <input value="Send" type="button" οnclick="update()"/>  
    <br/>  
    Reply: <span id="demoReply" style="background:#eeffdd; padding-left:4px; padding-right:4px;"></span>  
  </p>  
</div>  



和spring集成方法:

编写远程访问类

复制代码
package com.dwr3.annotation;

import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.directwebremoting.spring.SpringCreator;

@RemoteProxy(creator
=SpringCreator.class)
public class SayHello {
@RemoteMethod()
public String sayHello(){
return "say hello";
}
@RemoteMethod
public String sayHello(String name){
return name+" say hello";
}
}
复制代码

2.配置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"
>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>dwr</servlet-name>
<servlet-class>org.directwebremoting.spring.DwrSpringServlet</servlet-class>
<init-param>
<param-name>debug</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>
</servlet>
<servlet-mapping>
<servlet-name>dwr</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
复制代码

3.配置Spring配置文件(applicationContext.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"
xsi:schemaLocation
="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.directwebremoting.org/schema/spring-dwr
http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd"
>

<dwr:configuration>
<dwr:convert type="bean" class="org.uk.ltd.dwr.dev.model.Address" />
</dwr:configuration>
<bean id="sayHello" class="com.dwr3.annotation.SayHello">
<dwr:remote javascript="sayHello">
<dwr:include method="sayHello" />
</dwr:remote>
</bean>
</beans>
复制代码

4.编写测试代码

复制代码
<%@ 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>Insert title here</title>
<script type='text/javascript' src='/MyBatis/dwr/interface/sayHello.js'></script>
<script type='text/javascript' src='/MyBatis/dwr/engine.js'></script>

<script type="text/javascript">
function SayHelloTest(){
var str = document.getElementById("name").value;
sayHello.sayHello(str,
function(value){
alert(value);
});
}
</script>
</head>
<body>
<input type="button" value="无参数" onclick="sayHello.sayHello(function(value){alert(value);});">
<br>
<input type="text" id="name" name="name" />
<input type="button"T value="有参数" onclick="SayHelloTest();"/>
</body>
</html>
复制代码



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值