0

我从一个网站上复制了这个例子,它不起作用,谁能告诉我是什么原因。我在 tomcat 的控制台日志中没有收到任何异常或错误。

以下是我的文件 dwrtest.jsp

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>DIRECT WEB REMOTING (DWR) TEST APPLICATION</title>

        <base href="${pageContext.request.contextPath}">

        <!-- You have to include these two JavaScript files from DWR -->
        <script type='text/javascript' src='dwr/engine.js'></script>
        <script type='text/javascript' src='dwr/util.js'></script>

        <!-- This JavaScript file is generated specifically for your application -->
        <script type='text/javascript' src='dwr/interface/Calculator.js'></script>

        <script type='text/javascript'>

        function dwrAjaxRequest() {
            var num1 = DWRUtil.getValue("num1");
            var num2 = DWRUtil.getValue("num2");
            Calculator.addNumbers(num1,num2,dwrAjaxResponse);
        }

        function dwrAjaxResponse(result) {
            DWRUtil.setValue("resultText",result);
        }
      </script>
</head>
    <body>
        <h2>DWR SAMPLE APPLICATION</h2>
        <b>Number 1 : </b><input type="text" value="5" name="num1"/><br>
        <b>Number 2 : </b><input type="text" value="6" name="num2"/><br>
        <b>Result : </b><input type="text" value="" name="resultText"/><br>
        <input type="button" value="Add" name="go"  onClick="dwrAjaxRequest();"/>
    </body>
</html>

下面是 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.org/dwr/dwr20.dtd">
<dwr>
    <allow>
        <create creator="new" javascript="Calculator">
            <param name="class" value="com.example.dwr.Calculator"/>
            <include method="addNumbers"/>
        </create>
    </allow>
</dwr>

下面是WEB-INF文件夹下的web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>

    <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>



</web-app>

下面是我的 Calculator.java 类,它位于包结构 com.example.dwr

package com.example.dwr;

public class Calculator {
      public int addNumbers(int a , int b) {
        return (a + b);
      }

}

我是 DWR 新手,请让我知道这个示例有什么问题 ** 当我单击添加按钮时没有任何反应,实际上结果应该出现在结果文本框中**

4

1 回答 1

1

尝试公开整个类而不是特定方法,您可以在此处下载一个工作示例

<?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>
<create creator="new" javascript="forms">  
<param name="class" value="com.stringpool.demo.Status"/>  
</create> 
</allow>
</dwr>
于 2013-10-14T10:22:43.663 回答