DWR:未定义dwr

时间:2012-10-17 09:46:16

标签: java html tomcat web-applications dwr

我是DWR的先驱。根据(http://directwebremoting.org/dwr-demo/simple/text.html)中的教程,我将dwr脚本放在我的html文件中。但是当我运行应用程序时,它显示以下消息。

ReferenceError: dwr is not defined
[Break On This Error]       
var name = dwr.util.getValue("demoName");

我的HTML文件是:

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type='text/javascript' src='/dwr/engine.js'>    
</script>
<script type='text/javascript' src='/dwr/util.js'>

</script>
<title>Hello</title>
<script type="text/javascript">
    function update() {
        var name = dwr.util.getValue("demoName");
        Demo.sayHello(name, function(data) {
            dwr.util.setValue("demoReply", data);
        });
    }
</script>
</head>

<body>
    <p>
        Name: <input type="text" id="demoName" /> <input value="Send"
            type="button" onclick="update()" /> <br /> Reply: <span
            id="demoReply"></span>
    </p>
</body>
</html>

我的dwr.xml文件是:

<!DOCTYPE dwr PUBLIC
    "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN"
    "http://getahead.org/dwr/dwr30.dtd">

<dwr>
    <allow>
        <create creator="new" javascript="Demo">
            <param name="class" value="com.dwr.my.Demo" />
        </create>
    </allow>
</dwr>

类文件是Demo.java

package com.dwr.my;

public class Demo {

    public String sayHello(String name) {
        return "Hello, " + name;
    }

}

3 个答案:

答案 0 :(得分:1)

请确保在web.xml中添加dwr servlet映射

<servlet>
        <servlet-name>dwr-invoker</servlet-name>
        <display-name>DWR Servlet</display-name>
        <description>Direct Web Remoter Servlet</description>
        <servlet-class>
            org.directwebremoting.servlet.DwrServlet
        </servlet-class>

</servlet>
<servlet-mapping>
        <servlet-name>dwr-invoker</servlet-name>
        <url-pattern>/dwr/*</url-pattern>
    </servlet-mapping>

并在html文件中添加

<script type='text/javascript' src='/dwr/demo.js'>    
</script>

答案 1 :(得分:1)

错误告诉您,JavaScript上下文中不存在变量dwr

看起来您没有正确设置DWR。您对上一个答案的评论证实了这一点: Shows message "NetworkError: 404 Not Found - localhost:8080/dwr/engine.js"。如果找不到engine.js那么你就不会走得太远了!

您是否按照此处列出的所有步骤设置环境,以便可以调用DWR?: http://directwebremoting.org/dwr/introduction/getting-started.html

答案 2 :(得分:1)

在实现dwr.xml,web.xml中相应的java类和dwr servlet映射后运行localhost:8080 / DWR_tomcat / dwr /。然后它将显示DWR已知的类。然后单击我们的类名,它将显示类中声明的方法并生成要添加到html文件中的java脚本标记。