DWR 操作(-) Hello World 的学习!

从今天开始 我会把DWR在Web程序中的基本使用完全用代码的形式展现出来包括在Spring2.0、Ibatis、Hibernate、Struts1.2 的整合!
关于DWR的理论,网络中随处可见..
首先。可以去网络中找一个版本比较高的DWR包
创建好一个WEB工程后。
把dwr.jar包 copy 到WEB-INF目录下的lib中.在这个目录下创建一个dwr.xml文件

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>  
<!-- without allow, DWR isn't allowed to do anything -->  
<allow>     
    <create creator="new" javascript="Hello" scope="application">       
        <param name="class" value="com.xzj.service.HelloWorldService"/>       
    </create>  
</allow>  
</dwr> 
 
 
web.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
在这个包中写两个方法
代码如下:
package com.xzj.service;   
  
public class HelloWorldService {   
    private static final String msg="请输入姓名!";   
       
    public String helloWorld(){   
        return "Hello DWR World!";   
    }   
       
    public String hello(String name){   
        if(!"".equals(name)&&null!=name){   
                msg="Hello "+name+" 先生!";   
        }   
        return msg;   
    }}  
 
然后我们回到前台index.jsp页面中 代码如下:
<%@ 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.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(){   
            Hello.hello(txtName.value,showMessage);   
            function showMessage(msg){   
                alert(msg);   
            }   
        }   
    </script>   
  </head>   
     
  <body>   
    <center>   
        <input type="button" name="btnHello" value="HelloWord" onclick="helloWorld()"/>   
        <br><br><br><br>   
        Please Enter You Name:<input type="text" name="name" id="txtName" value="小屁孩"/>   
        <input type="button" name="btnHello" value="HelloWord" onclick="hello()"/>   
    </center>   
  </body>   
</html>  
  

你可能感兴趣的:(JavaScript,Web,xml,servlet,DWR)