WEB-INF目录下创建一个dwr.xml文件
dwr.xml 配置代码:
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>
<!-- without allow, DWR isn't allowed to do anything -->
<allow>
<!--creator設定為new,表示使用Hello的無參數建構子來生成物件,javascript設定為Hello,表示客戶端JavaScript程式可以使用Hello來呼叫對應的onlyfun.caterpillar.Hello物件-->
<create creator="new" javascript="Hello" scope="application">
<param name="class" value="com.xzj.service.HelloWorldService"/>
</create>
</allow>
</dwr>
web.xml中的配置如下:
Xml代码
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee <A href="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
target=_blank>http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd</A>">
<!-- DWR配置开始 -->
<servlet>
<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>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
<!-- DWR配置结束 -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
然后我们在src目录下创建一个com.xzj.service包在次包中创建一个类名为:HelloWorldService
在这个包中写两个方法
在这个包中写两个方法
代码如下:
Java代码
package com.xzj.service;
public class HelloWorldService {
private static String msg="请输入姓名!";
public String helloWorld(){
return "Hello DWR World!";
}
public String hello(String name){
if("".equals(name)||null==name){
return msg;
}
if(!"".equals(name)||null==name){
msg="Hello"+name+" 先生";
}
return msg;
}
}
然后我们回到前台index.jsp页面中 代码如下:
Java代码
<%@ page language="java" pageEncoding="UTF-8"%>
<html>
<head>
<title>DWR Hello World!</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">
<!--dwr/interface/Hello.js是由DWRServlet根據dwr.xml中的設定生成的,engine.js負責客戶端伺服端溝通,util.js是一些好用的JavaScript程式,可以讓您少寫很多JavaScript。-->
<!--下面三个必须要第一个和你dwr.xml配置的javascript="Hello"一样-->
<script type="text/javascript" src='dwr/interface/Hello.js'></script>
<script type="text/javascript" src='dwr/engine.js'></script>
<script type="text/javascript" src='dwr/util.js'></script>
<script type="text/javascript">
function helloWorld(){
Hello.helloWorld(showMessage);
function showMessage(msg){
alert(msg);
}
}
function hello(){
// var name = $('txtName').value;
//Hello.hello(name,showMessage());
Hello.hello(txtName.value,showMessage);
function showMessage(msg){
alert(msg);
}
}
</script>
</head>
<body>
<center>
<input type="button" name="btnHello" value="HelloWord" οnclick="helloWorld()"/>
<br><br><br><br>
Please Enter You Name:<input type="text" name="name" id="txtName"/>
<input type="button" name="btnHello" value="HelloWord" οnclick="hello()"/>
</center>
</body>
</html>
另外版本需要的是5.5的,6.0的配置变了
dwr入门代码一
最新推荐文章于 2015-08-02 20:23:00 发布