即日起在codingBlog上分享您的技术经验即可获得积分,积分可兑换现金哦。

DWR实例浅谈

编程语言 u013823429 44℃ 0评论
本文目录
[隐藏]

1.DWR实例浅析

@(DWR)


DWR(Direct Web Remoting),是Java和JavaScript想结合的开源库。能够采用看似调用浏览地本地代码的方法来调用服务器端的代码。

1.1.1.实例演示

1.1.1.1.1 pom.xml

完整maven的pom如下所示。

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0modelVersion>
  <groupId>com.dwr04groupId>
  <artifactId>DWRTest04artifactId>
  <packaging>warpackaging>
  <version>0.0.1-SNAPSHOTversion>
  <name>DWRTest04 Maven Webappname>
  <url>http://maven.apache.orgurl>
 <dependencies>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>3.8.1version>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>org.directwebremotinggroupId>
            <artifactId>dwrartifactId>
            <version>3.0.2-RELEASEversion>
        dependency>
        <dependency>
            <groupId>commons-logginggroupId>
            <artifactId>commons-loggingartifactId>
            <version>1.2version>
        dependency>
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>servlet-apiartifactId>
            <version>2.5version>
        dependency>
        <dependency>
            <groupId>org.apache.tomcatgroupId>
            <artifactId>tomcat-servlet-apiartifactId>
            <version>7.0.62version>
        dependency>
  dependencies>
  <build>
    <finalName>DWRTest04finalName>
  build>
project>

1.1.2.1.2 web.xml

关于dwr的init-param还有很多,详情见官方。


<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>DWRTest04display-name>
    <servlet>
        <servlet-name>dwr-invokerservlet-name>
        <servlet-class>
            org.directwebremoting.servlet.DwrServlet
        servlet-class>
        
        <init-param>
            <param-name>debugparam-name>
            <param-value>trueparam-value>
        init-param>
        
        <init-param>
            <param-name>scriptCompressedparam-name>
            <param-value>trueparam-value>
        init-param>
        
        <init-param>
            <param-name>crossDomainSessionSecurityparam-name>
            <param-value>falseparam-value>
        init-param>
    servlet>
    <servlet-mapping>
        <servlet-name>dwr-invokerservlet-name>
        <url-pattern>/dwr/*url-pattern>
    servlet-mapping>

  <welcome-file-list>
    <welcome-file>/page/indexdwr.jspwelcome-file>
  welcome-file-list>

web-app>

1.1.3.1.3 dwr.xml


<dwr>


    <allow>
        
        
        <convert  converter="bean" match="com.dwr.DWRTest04.ResultBean"/>

        
        
        <create creator="new" javascript="Add" scope="request">
            <param name="class" value="com.dwr.DWRTest04.Add" />
        create>                      

    allow>
dwr>

1.1.4.1.4 indexdwr.jsp

引入的是JavaScript文件名,对应的是Java类名。例如这段代码被解析和执行时,会定位到/dwr/ineerface/Add.js。然后Servlet动态检查Java类Add.java。生成代表该类的JavaScript,并返回所生成的JavaScript。

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
"-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">


"Content-Type" content="text/html; charset=UTF-8">




Insert title here



        
    参数为基本类型:  
    "text" id="a">
     +
    "text" id="b">
    "button" onClick="doAdd()" value="=">
    "resultDiv">



    
    

参数为JavaBean: "text" id="a2"> + "text" id="b2"> "button" onClick="doAddWithBean()" value="="> "resultDivWithBean">

批处理: "text" id="a3"> + "text" id="b3"> "button" onClick="doAddWithBatch()" value="="> "resultDivWithBatch1"> "resultDivWithBatch2">

1.1.5.1.5 Add.java

package com.dwr.DWRTest04;

public class Add {
    public int add(int a , int b){
        return a+b;
    }

    public ResultBean addWithBean(int a , int b){
        ResultBean resultBean = new ResultBean();
        resultBean.setResult(a+b);
        return resultBean;
    }   
}

1.1.6.1.6 ResultBean.java

package com.dwr.DWRTest04;

public class ResultBean {
    private int result;

    public int getResult() {
        return result;
    }

    public void setResult(int result) {
        this.result = result;
    }


}

1.1.7.1.7 执行效果

效果图


Alt text

生成的js


Alt text

1.1.8.1.8 test/debug

输入网址:http://localhost:8080/DWRTest04/dwr/index.html


Alt text


点击进去后,既能对Add中的方法进行调试


Alt text

1.2.2.小结

以上为DWR的一个简单例子。附上github的地址:https://github.com/JunliXia/DWRTest04

1.3.3.参考

DWR实战

转载请注明:CodingBlog » DWR实例浅谈

喜欢 (0)or分享 (0)
发表我的评论
取消评论

*

表情