Java的Web项目使用DWR简单配置说明

首先使用Maven引入DWR的Jar包

<dependency>
    <groupId>org.directwebremoting</groupId>
    <artifactId>dwr</artifactId>
    <version>3.0.1-RELEASE</version>
</dependency>

其次建立一个测试用的Java类

package com.k.ctc.web.dwr;

/**
 * @author Kai
 * @Date 2016-1-7 14:32:08
 */
public class TestDwr {

    public String getName(String name) {
        System.out.println("Call GetName Method!");
        return "Hello:" + name;
    }
}

然后配置dwr.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN" "http://directwebremoting.org/schema/dwr30.dtd">
<dwr>
    <allow>
        <create creator="new" javascript="test" scope="page">
            <param name="class" value="com.k.ctc.web.dwr.TestDwr" />
        </create>
    </allow>
</dwr>


配置web.xml

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

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <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>
        <init-param>
            <!-- 如果改为false将会能接收其它域的请求,存在安全风险 -->
            <param-name>crossDomainSessionSecurity</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <!-- 设置超时时间 -->
            <param-name>scriptSessionTimeout</param-name>
            <param-value>1800000</param-value>
        </init-param>
        <init-param>
            <param-name>maxCallCount</param-name>
            <param-value>20</param-value>
        </init-param>
        <init-param>
            <param-name>config-xml</param-name>
            <param-value>/WEB-INF/conf/dwr.xml</param-value>
        </init-param>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>dwr-invoker</servlet-name>
        <url-pattern>/dwr/*</url-pattern>
    </servlet-mapping>
    
</web-app>


最后在HTML页面中使用

<!DOCTYPE html>
<html>
    <head>
        <title>Dwr Test Demo</title>
        <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/interface/test.js'></script>
        <script>
            var reply = function (data) {
                document.getElementById("result").innerText = data;
            };
            function btn_click() {
                var name = document.getElementById("name").value;
                test.getName(name, reply);
            }
        </script>
    </head>
    <body>
        <input type="text" id="name"/> <input type="button" value="GetName" onclick="btn_click();"/>
        <div id="result"></div>
    </body>
</html>


注意:js的路径需要引用正确,附带说明截图

↓ 写好后的测试页面 ↓

Java的Web项目使用DWR简单配置说明_第1张图片

↓ DWR生成的模块列表页面 

Java的Web项目使用DWR简单配置说明_第2张图片

↓ 模块详情页面和需要引用的js路径说明等 

Java的Web项目使用DWR简单配置说明_第3张图片


你可能感兴趣的:(JavaScript,java,DWR)