按照文档的说明,自己写了一个简单的demo(简直就是照抄,嘿嘿),但是,就这样也会出毛病,我真晕啊!
第一步:引入:dwr.jar
第二步:在dwr-2.0.3-src\web\WEB-INF\classes\org\directwebremoting目录下找到engine.js和util.js,并引入
第三步:在web.xml中注册servlet如下:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
1
<
servlet
>
2
<
servlet-name
>
dwr-invoker
</
servlet-name
>
3
<
servlet-class
>
org.directwebremoting.servlet.DwrServlet
</
servlet-class
>
4
<
init-param
>
5
<
param-name
>
debug
</
param-name
>
6
<
param-value
>
true
</
param-value
>
7
</
init-param
>
12
</
servlet
>
13
14
<
servlet-mapping
>
15
<
servlet-name
>
dwr-invoker
</
servlet-name
>
16
<
url-pattern
>
/dwr/*
</
url-pattern
>
17
</
servlet-mapping
>
第四步:在WEB-INFO下建立dwr.xml,内容如下:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
<?
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/dwr20.dtd"
>
<
dwr
>
<
allow
>
<
create
creator
="new"
javascript
="helloWord"
>
<
param
name
="class"
value
="on08_04_19.HelloWord"
/>
</
create
>
</
allow
>
</
dwr
>
第五步:在package:on08_04_19下编写HelloWord.java,代码如下:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
package
on08_04_19;
public
class
HelloWord {
public
String sayHello(String name){
return
"
Hello
"
+
name;
}
}
按照文档所说的方法测试,通过
第六步:编写JSP代码如下:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
<
script
type
="text/javascript"
src
="<%=request.getContextPath() %>/js/engine.js"
></
script
>
<
script
type
="text/javascript"
src
="<%=request.getContextPath() %>/js/util.js"
></
script
>
<
script
type
="text/javascript"
src
="<%=request.getContextPath() %>/dwr/interface/helloWord.js"
></
script
>
<
script
type
="text/javascript"
>
function
firstDwr(){
helloWord.sayHello(
"
杨凯
"
,callBackMethod);
}
function
callBackMethod(data){
alert(data);
}
</
script
>
</
head
>
<
body
>
<
form
action
=""
>
<
input
type
="button"
value
="欢迎"
onclick
="firstDwr()"
>
</
form
>
</
body
>
第一遍完整测试,发现点击按钮之后无反应,但是后台却打印
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
A request has been denied as a potential CSRF attack
无奈GOOGLE了半天,得到法子修改web.xml如下:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
<
servlet
>
<
servlet-name
>
dwr-invoker
</
servlet-name
>
<
servlet-class
>
org.directwebremoting.servlet.DwrServlet
</
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
>
</
servlet
>
<
servlet-mapping
>
<
servlet-name
>
dwr-invoker
</
servlet-name
>
<
url-pattern
>
/dwr/*
</
url-pattern
>
</
servlet-mapping
>
然后继续测试,后台打印 Exec: helloWord.sayHello(),说明后台方法已经被调用,,但是Firefox下依然无反应,在IE下却出现JS错误:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
例外被抛出且未被接住
超晕,无奈继续GOOGLE,有文章竟然说官方的JAR包有BUG,让换成他的包,我晕,真是牛人,更牛的是换成他的包之后,还真是正常了,我靠!
牛人全文:http://yahaitt.iteye.com/blog/147795