DWR框架入門例子

Lessony發表於2015-05-14

關於dwr的概念,摘自百度百科:

DWR(Direct Web Remoting)是一個用於改善web頁面與Java類互動的遠端伺服器端Ajax開源框架,可以幫助開發人員開發包含AJAX技術的網站。它可以允許在瀏覽器裡的程式碼使用執行在WEB伺服器上的JAVA函式,就像它就在瀏覽器裡一樣。


DWR框架使用之一:單獨使用

步驟:

1、在pom.xml中配置依賴。(就是載入jar包)

2、在web.xml中配置servlet。

3、寫一個普通java類。

4、配置dwr.xml檔案,配置要公開的類或方法。

5、寫一個jsp頁面,主要匯入engine.js和相應的java類對映的js檔案,然後編寫js即可呼叫。


詳細:

1、pom.xml程式碼如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.lessony</groupId>
    <artifactId>dwr01</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>dwr01</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    
    <dependencies>
        <!--DWR的jar包-->
        <dependency>
            <groupId>org.directwebremoting</groupId>
            <artifactId>dwr</artifactId>
            <version>3.0.M1</version>
        </dependency>
        
        <!--由於使用jetty來執行web,jetty日誌輸出需要日誌jar包,可以使用common-logging的日誌框架-->
        <!--
        <dependency>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
                <version>1.2</version>
        </dependency>
        -->
        
        <!--slf4j日誌框架,jcl-over-slf4j是將common-logging的日誌呼叫到slf4j,maven會把其它依賴包匯入-->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>1.7.6</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>8.1.16.v20140903</version>
                <configuration>
                        <scanIntervalSeconds>10</scanIntervalSeconds>
                        <webApp>
                                <contextPath>/dwr01</contextPath>
                        </webApp>
                        <connectors>
                                <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
                                        <port>8888</port>
                                        <maxIdleTime>60000</maxIdleTime>
                                </connector>
                        </connectors>
                </configuration>
            </plugin>
            
            <!--打包外掛,把web打成zip包-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <descriptors>
                        <descriptor>assembly.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <!-- 當執行mvn package時才會打包 -->
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>



2、配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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">

    <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>
       
        <load-on-startup>1</load-on-startup>
    </servlet>

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


3、寫一個普通的java類

package com.lessony.dwr01;

/**
 *
 * @author Lessony
 */
public class Hello {
    public String sayHello(String name){
        System.out.println("hello "+name);
        return "hello "+name;
    }
}


4、配置dwr.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN" "http://getahead.org/dwr/dwr30.dtd">

<dwr>
    <allow>
          <create creator="new">
              <param name="class" value="com.lessony.dwr01.Hello"/>
          </create>
     </allow>
</dwr>



5、jsp檔案

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
        <script src="<%=request.getContextPath()%>/dwr/engine.js"></script>
        <script src="<%=request.getContextPath()%>/dwr/interface/Hello.js"></script>
        
        <script>
            Hello.sayHello("張三");
        </script>
    </head>
    <body>
        <h1>Hello World!</h1>
    </body>
</html>


全部編輯完成之後,開啟瀏覽器,輸入http://localhost:8888/dwr01/dwr01.jsp,就可以看到控制檯輸出了:hello 張三



最後附上zip包,連結:http://download.csdn.net/detail/lxn39830435731415926/8708077

相關文章