首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

Java中施用DWR的简单例子

2012-11-23 
Java中使用DWR的简单例子1.首先将dwr.jar放到lib目录下面2.在web.xml中添加关于dwr的Servlet配置信息serv

Java中使用DWR的简单例子
1.首先将dwr.jar放到lib目录下面
2.在web.xml中添加关于dwr的Servlet配置信息

  <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>  </servlet>  <servlet-mapping>    <servlet-name>dwr-invoker</servlet-name>    <url-pattern>/dwr/*</url-pattern>  </servlet-mapping> 

3.添加dwr.xml的配置文件
在WEB-INF下面添加dwr.xml文件
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://getahead.ltd.uk/dwr/dwr20.dtd"><dwr>  <allow>      <create javascript="DwrTest" creator="new">          <param name="class" value="com.wl.dwr.test.FirstDwrTest"></param>      </create>  </allow></dwr>

这个配置的意思是,要创建的是DwrTest对象的javascript库,而且这个库的名字叫DwrTest,同时,这也是我们在JSP页面上调用这个对象的时候所使用的名称。
4.具体的FirstDwrTest代码如下
package com.wl.dwr.test;public class FirstDwrTest {public String sayHello(String name){return "Hello,"+name;}}

5.前台的页面代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">        <title>My JSP 'DwrTest.jsp' starting page</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"><!--<link rel="stylesheet" type="text/css" href="styles.css">--><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/DwrTest.js"></script> <script type="text/javascript">   function Say(){      var inputName=document.getElementById("name").value;      DwrTest.sayHello(inputName,function(data){alert(data);});   }</script>  </head>    <body>    This is my JSP page. <br>    TestDwr Start:<input type="text" id="name" value=""/>&nbsp;&nbsp;&nbsp;<input type="button" onclick="Say()" value="Test_DWR"/>  </body></html>

6.测试的结果如下:


热点排行