DWR HelloWorld第一步

前端之家收集整理的这篇文章主要介绍了DWR HelloWorld第一步前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1 首先下载dwr的jar包加入classpath

在web.xml中加入

<servlet>
    <servlet-name>dwr-invoker</servlet-name>
   
    <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>

    <!-- This should NEVER be present in live -->
    <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>
2 编写java文件
package dwr;

public class HelloWorld {
	public String sayHello(String name){
		return name+",你好";
	}
}

3 编写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>

    <!--
    <filter class="org.getahead.dwrdemo.monitor.MonitoringAjaxFilter"/>
    <filter class="org.directwebremoting.filter.ExtraLatencyAjaxFilter">
      <param name="delay" value="200"/>
    </filter>
    -->

    <!-- resources not in this war file: java.util.Date -->
    <create creator="new" javascript="JDate">
      <param name="class" value="java.util.Date"/>
      <include method="sayHello"/>
     
    </create>
 	<create creator="new" javascript="HelloWorld">
      <param name="class" value="dwr.HelloWorld"/>
     
    </create>

  </allow>

</dwr>

4 编写测试jsp文件


<%@ page language="java" contentType="text/html; charset=UTF-8"
    %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<Meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 <script type='text/javascript' src='/DWR2/dwr/interface/HelloWorld.js'></script>
  <script type='text/javascript' src='/DWR2/dwr/engine.js'></script>
   <script type='text/javascript' src='/DWR2/dwr/util.js'></script>
    <script type='text/javascript'>
    function firstDwr(){
    	HelloWorld.sayHello("世界",callbackHello);
    }
    function callbackHello(data){
    	alert(data)
    }
    </script>
<title>dwr测试</title>
</head>
<body>
dwr测试1
<input type="button" name="button" value="测试" onclick="firstDwr()"/>
</body>
</html>
注意将这几行需要添加
<script type='text/javascript' src='/DWR2/dwr/interface/HelloWorld.js'></script>
  <script type='text/javascript' src='/DWR2/dwr/engine.js'></script>
   <script type='text/javascript' src='/DWR2/dwr/util.js'></script>

部署到tommcat下 查看效果



至此完成了dwr的HelloWorld小程序

猜你在找的Ajax相关文章