dwr和spring結合的例子
applicationContext.xml檔案
<bean id="Hello" class="com.zscqj.common.Hello"> </bean>
Java類
package com.zscqj.common;
public class Hello {
public String say(String infor){
return "您發送的訊息是:" + infor;
}
}
JSP頁面:
<%@ page language="java" pageEncoding="utf-8"%>
<script type='text/javascript' src='dwr/engine.js'> </script>
<script type='text/javascript' src='dwr/util.js'> </script>
<script type='text/javascript' src='dwr/interface/hello.js'> </script>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<base href="<%=basePath%>">
<title>這是測試dwr</title>
<script type="text/javascript">
function tijiao()
{
var info=document.all["infor"].value;
alert(info);
hello.say(info,callback);
}
function callback(data)
{
document.all["con"].innerHTML=data;
}
</script>
</head>
<body>
<br><br><br><br><br><br><br>
<center>
哈哈,看到地址沒有啊!此頁面的basePath是: <%=basePath%><br>
哈哈,看到地址沒有啊!此頁面的path是: <%=path%><br><br><br><br>
<input type="text" name="infor">
<input type="button" value="看看我" onclick="tijiao()"><br>
提交後返回的內容如下:
<br><font color="red"> <span id="con"></span> </font>
</center>
</body>
</html>
web.xml檔案:
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
<init-param>
<description></description>
<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>
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>
<!-- resources not in this war file: java.util.Date -->
<create creator="spring" javascript="hello">
<param name="beanName" value="Hello"/>
</create>
</allow>
</dwr>
配置好後,測試回合,出現如下錯誤:
INFO - retrieved system configuration file: java.io.ByteArrayInputStream@1145cc
INFO - Creator 'pageflow' not loaded due to ClassNotFoundException. This is only an problem if you wanted to use it. Cause: Beehive/Weblogic Creator not available.
INFO - Converter 'jdom' not loaded due to NoClassDefFoundError. This is only an problem if you wanted to use it. Cause: org/jdom/Document
INFO - Found Hibernate3 class: org.hibernate.Hibernate
INFO - Type 'org.jdom.Document' is not convertable due to missing converter 'jdom'. This is only an problem if you wanted to use it.
INFO - Type 'org.jdom.Element' is not convertable due to missing converter 'jdom'. This is only an problem if you wanted to use it.
INFO - retrieved system configuration file: java.io.ByteArrayInputStream@21e5f0
INFO - Creator 'pageflow' not loaded due to ClassNotFoundException. This is only an problem if you wanted to use it. Cause: Beehive/Weblogic Creator not available.
INFO - Converter 'jdom' not loaded due to NoClassDefFoundError. This is only an problem if you wanted to use it. Cause: org/jdom/Document
INFO - Found Hibernate3 class: org.hibernate.Hibernate
INFO - Type 'org.jdom.Document' is not convertable due to missing converter 'jdom'. This is only an problem if you wanted to use it.
INFO - Type 'org.jdom.Element' is not convertable due to missing converter 'jdom'. This is only an problem if you wanted to use it.
INFO - retrieved system configuration file: java.io.ByteArrayInputStream@618b08
INFO - Creator 'pageflow' not loaded due to ClassNotFoundException. This is only an problem if you wanted to use it. Cause: Beehive/Weblogic Creator not available.
INFO - Converter 'jdom' not loaded due to NoClassDefFoundError. This is only an problem if you wanted to use it. Cause: org/jdom/Document
INFO - Found Hibernate3 class: org.hibernate.Hibernate
INFO - Type 'org.jdom.Document' is not convertable due to missing converter 'jdom'. This is only an problem if you wanted to use it.
INFO - Type 'org.jdom.Element' is not convertable due to missing converter 'jdom'. This is only an problem if you wanted to use it.
在網上找了一大堆資料,說少了jdom包,加上以下的網址的jdom-1.0.jar包
http://mirrors.ibiblio.org/pub/mirrors/maven/jdom/jars/jdom-1.0.jar
(添加相應的包,
http://www.findjar.com/jar/jdom/jars/jdom-1.0.jar.html
http://www.java2s.com/Code/Jar/Spring-Related/Downloadjsfapijar.htm)
運行還是出現錯誤,不過這次少了不少。錯誤如下:
INFO - retrieved system configuration file: java.io.ByteArrayInputStream@adb24
INFO - Creator 'pageflow' not loaded due to ClassNotFoundException. This is only an problem if you wanted to use it. Cause: Beehive/Weblogic Creator not available.
INFO - Found Hibernate3 class: org.hibernate.Hibernate
INFO - retrieved system configuration file: java.io.ByteArrayInputStream@1f23ca4
INFO - Creator 'pageflow' not loaded due to ClassNotFoundException. This is only an problem if you wanted to use it. Cause: Beehive/Weblogic Creator not available.
INFO - Found Hibernate3 class: org.hibernate.Hibernate
INFO - retrieved system configuration file: java.io.ByteArrayInputStream@9ced84
INFO - Creator 'pageflow' not loaded due to ClassNotFoundException. This is only an problem if you wanted to use it. Cause: Beehive/Weblogic Creator not available.
INFO - Found Hibernate3 class: org.hibernate.Hibernate
這個錯誤真麻煩,我在網上轉了一圈又一圈,老是找不到錯誤的原因,有人說是xalan包問題,我照著做了,還是不行,可憐死我了。浪費了好幾個小時,最後一次,我試著將DOCTYPE標頭檔換了一個,呵呵,問題居然解決了。這下晚上可以睡好覺了!以下是我正確的確良dwr.xml檔案:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC
"-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN"
"http://www.getahead.ltd.uk/dwr/dwr10.dtd">
<dwr>
<allow>
<!-- resources not in this war file: java.util.Date -->
<create creator="spring" javascript="hello">
<param name="beanName" value="Hello"/>
</create>
</allow>
</dwr>