dwr入门

搭建环境:

1.创建一个web项目,下载以下jar包

commons-logging-1.1.1.jar: (dwr依赖于该jar)

dwr.jar

mysql-connector-java-5.1.6-bin.jar

web.xml中配置dwr拦截器

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

2.在WEB-INF下,配置dwr.xml(从dwr的提供的压缩包中找出该文件.)

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

  <init>
    <creator id="none" class="org.directwebremoting.create.NullCreator"/>
    <creator id="new" class="org.directwebremoting.create.NewCreator"/>
    <creator id="static" class="org.directwebremoting.create.StaticCreator"/>
    <creator id="pageflow" class="org.directwebremoting.beehive.PageFlowCreator"/>
    <creator id="spring" class="org.directwebremoting.spring.SpringCreator"/>
    <creator id="script" class="org.directwebremoting.create.ScriptedCreator"/>
    <creator id="struts" class="org.directwebremoting.struts.StrutsCreator"/>
    <!--
    These are now in the noncla module
    <creator id="jsf" class="org.directwebremoting.faces.JsfCreator"/>
    <creator id="jsf2" class="org.directwebremoting.faces.Jsf2Creator"/>
    <creator id="ejb3" class="org.directwebremoting.create.Ejb3Creator"/>
    -->

    <converter id="null" class="org.directwebremoting.convert.NullConverter"/>
    <converter id="enum" class="org.directwebremoting.convert.EnumConverter"/>
    <converter id="primitive" class="org.directwebremoting.convert.PrimitiveConverter"/>
    <converter id="bignumber" class="org.directwebremoting.convert.BigNumberConverter"/>
    <converter id="string" class="org.directwebremoting.convert.StringConverter"/>
    <converter id="array" class="org.directwebremoting.convert.ArrayConverter"/>
    <converter id="map" class="org.directwebremoting.convert.MapConverter"/>
    <converter id="collection" class="org.directwebremoting.convert.CollectionConverter"/>
    <converter id="date" class="org.directwebremoting.convert.DateConverter"/>
    <converter id="jodaLocalDateTime" class="org.directwebremoting.convert.LocalDateTimeConverter"/>
    <converter id="dom" class="org.directwebremoting.convert.DOMConverter"/>
    <converter id="dom4j" class="org.directwebremoting.convert.DOM4JConverter"/>
    <converter id="jdom" class="org.directwebremoting.convert.JDOMConverter"/>
    <converter id="xom" class="org.directwebremoting.convert.XOMConverter"/>
    <converter id="servlet" class="org.directwebremoting.convert.ServletConverter"/>
    <converter id="bean" class="org.directwebremoting.convert.BeanConverter"/>
    <converter id="object" class="org.directwebremoting.convert.ObjectConverter"/>
    <converter id="hibernate2" class="org.directwebremoting.hibernate.H2BeanConverter"/>
    <converter id="hibernate3" class="org.directwebremoting.hibernate.H3BeanConverter"/>
    <converter id="url" class="org.directwebremoting.convert.URLConverter"/>
    <converter id="exception" class="org.directwebremoting.convert.ExceptionConverter"/>
    <converter id="miniException" class="org.directwebremoting.convert.MinimalistExceptionConverter"/>
    <converter id="file" class="org.directwebremoting.convert.FileConverter"/>
    <converter id="context" class="org.directwebremoting.convert.ContextConverter"/>
    <converter id="stringWrapper" class="org.directwebremoting.convert.StringWrapperConverter"/>
    <converter id="raw" class="org.directwebremoting.convert.RawConverter"/>
    <converter id="proxyFunction" class="org.directwebremoting.convert.JavascriptFunctionConverter"/>
    <converter id="proxyObject" class="org.directwebremoting.convert.JavascriptObjectConverter"/>
    <converter id="proxyInterface" class="org.directwebremoting.convert.ProxyInterfaceConverter"/>
    <converter id="locale" class="org.directwebremoting.convert.LocaleConverter"/>
    <converter id="currency" class="org.directwebremoting.convert.CurrencyConverter"/>

    <converter id="jsx3uri" class="jsx3.net.URIResolverConverter"/>
    <converter id="jsx3doc" class="jsx3.xml.CdfDocumentConverter"/>
  </init>

  <allow>
    <convert converter="null" match="void"/>
    <convert converter="null" match="java.lang.Void"/>

    <convert converter="miniException" match="java.lang.Throwable"/>

    <convert converter="primitive" match="boolean"/>
    <convert converter="primitive" match="byte"/>
    <convert converter="primitive" match="short"/>
    <convert converter="primitive" match="int"/>
    <convert converter="primitive" match="long"/>
    <convert converter="primitive" match="float"/>
    <convert converter="primitive" match="double"/>
    <convert converter="primitive" match="char"/>
    <convert converter="primitive" match="java.lang.Boolean"/>
    <convert converter="primitive" match="java.lang.Byte"/>
    <convert converter="primitive" match="java.lang.Short"/>
    <convert converter="primitive" match="java.lang.Integer"/>
    <convert converter="primitive" match="java.lang.Long"/>
    <convert converter="primitive" match="java.lang.Float"/>
    <convert converter="primitive" match="java.lang.Double"/>
    <convert converter="primitive" match="java.lang.Character"/>

    <convert converter="bignumber" match="java.math.BigInteger"/>
    <convert converter="bignumber" match="java.math.BigDecimal"/>

    <convert converter="string" match="java.lang.String"/>
    <convert converter="date" match="java.util.Date"/>
    <convert converter="date" match="java.sql.Date"/>
    <convert converter="date" match="java.sql.Time"/>
    <convert converter="date" match="java.sql.Timestamp"/>
    <convert converter="date" match="java.util.Calendar"/>
    <convert converter="url" match="java.net.URL"/>
    <convert converter="locale" match="java.util.Locale"/>
    <convert converter="currency" match="java.util.Currency"/>

    <convert converter="array" match="[Z"/>
    <convert converter="file" match="[B"/>
    <convert converter="array" match="[S"/>
    <convert converter="array" match="[I"/>
    <convert converter="array" match="[J"/>
    <convert converter="array" match="[F"/>
    <convert converter="array" match="[D"/>
    <convert converter="array" match="[C"/>
    <convert converter="array" match="[L*"/>

    <!--
    The catch for the next 2 is that we really mean java.util.Collection<String>
    and java.util.Map<String, String> but we need to do more work before this
    syntax is enabled
    -->
    <convert converter="collection" match="java.util.Collection"/>
    <convert converter="map" match="java.util.Map"/>

    <convert converter="dom" match="org.w3c.dom.Node"/>
    <convert converter="dom" match="org.w3c.dom.Element"/>
    <convert converter="dom" match="org.w3c.dom.Document"/>
    <convert converter="dom4j" match="org.dom4j.Document"/>
    <convert converter="dom4j" match="org.dom4j.Element"/>
    <convert converter="dom4j" match="org.dom4j.Node"/>
    <convert converter="jdom" match="org.jdom.Document"/>
    <convert converter="jdom" match="org.jdom.Element"/>
    <convert converter="xom" match="nu.xom.Document"/>
    <convert converter="xom" match="nu.xom.Element"/>
    <convert converter="xom" match="nu.xom.Node"/>

    <convert converter="servlet" match="javax.servlet.ServletConfig"/>
    <convert converter="servlet" match="javax.servlet.ServletContext"/>
    <convert converter="servlet" match="javax.servlet.http.HttpServletRequest"/>
    <convert converter="servlet" match="javax.servlet.http.HttpServletResponse"/>
    <convert converter="servlet" match="javax.servlet.http.HttpSession"/>

    <convert converter="file" match="java.io.InputStream"/>
    <convert converter="file" match="java.awt.image.BufferedImage"/>

    <convert converter="jsx3uri" match="jsx3.net.URIResolver"/>
    <convert converter="jsx3doc" match="jsx3.xml.CdfDocument"/>

    <convert converter="exception" match="org.directwebremoting.io.DwrConvertedException"/>
    <convert converter="file" match="org.directwebremoting.io.FileTransfer"/>
    <convert converter="bean" match="org.directwebremoting.io.Item"/>
    <convert converter="bean" match="org.directwebremoting.io.ItemUpdate">
      <param name="constructor" value="String itemId, String attribute, org.directwebremoting.io.RawData newValue"/>
    </convert>
    <convert converter="bean" match="org.directwebremoting.io.MatchedItems"/>
    <convert converter="proxyFunction" match="org.directwebremoting.io.JavascriptFunction"/>
    <convert converter="proxyObject" match="org.directwebremoting.io.JavascriptObject"/>
    <convert converter="raw" match="org.directwebremoting.io.RawData"/>
    <convert converter="bean" match="org.directwebremoting.io.QueryOptions">
      <param name="constructor" value="boolean deep, boolean ignoreCase" />
    </convert>
    <convert converter="bean" match="org.directwebremoting.io.StoreRegion">
      <param name="constructor" value="int start, int count, java.util.List sort, java.util.Map query, org.directwebremoting.io.QueryOptions queryOptions"/>
    </convert>
    <convert converter="proxyInterface" match="org.directwebremoting.io.StoreChangeListener"/>
    <convert converter="null" match="org.directwebremoting.datasync.StoreProvider"/>
    <convert converter="bean" match="org.directwebremoting.jsonrpc.io.JsonRpcError"/>
    <convert converter="bean" match="org.directwebremoting.jsonrpc.io.JsonRpcResponse"/>
    <convert converter="bean" match="org.directwebremoting.io.SortCriterion">
      <param name="constructor" value="String attribute, boolean descending"/>
    </convert>
    <convert converter="stringWrapper" match="org.directwebremoting.io.StringWrapper"/>

    <!--
    <convert converter="context" match="org.directwebremoting.io.Context"/>
     <convert converter="bean" match="java.lang.StackTraceElement" />  
    <convert converter="exception" match="java.lang.Exception"/>  
    -->
<!— 以上信息不用改—>
      
  <create creator="new"  javascript="grantService">
          <param name="class"  value="com.dwrtest.service.GrantForCompany" />
  </create>

  <!--配置实体类存放路径用于传递对象时使用-->
  <convert match="com.dwrtest.entity.*" converter="bean"></convert>
 </allow>
        
</dwr>

3. 创建企业实体

import java.sql.Date;

public class Company
{
    private int companyId;//企业id
    private String companyName;//企业名
    private String domain;//域名
    private int  maxDomain;//最大域名数
    private int maxUsers;//最大用户数
    private String  ipAdd; //授权ip地址
    private  String macAdd; //授权mac地址
    private  Date startServiceDate; //服务开始日期
    private Date endServiceDate; //服务结束日期
    
    private String publicEmlpath; //系统公共邮件保存路径
    private String dataSavePath;//数据存储路径
    private String dataBackup;//数据备份路径
    private String  logSavePath;//日志存储路径
    
    
    public Company(){
        
    }
     //set,get方法

}

4. 编写测试类

mport java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Timestamp;
import org.apache.log4j.Logger;
import com.espeed.dao.impl.InitDao;
import com.espeed.entity.Company;
import com.espeed.entity.ModuleCompanyBox;
import com.espeed.entity.ModuleCustomerManager;
import com.espeed.entity.ModuleEmailMarkit;
import com.espeed.util.DBUtils;
import com.espeed.util.DES_Encrypt;
import com.espeed.util.Md5Code;

public class GrantForCompany
{
    private InitDao dao = null;
    private Logger log = Logger.getLogger(GrantForCompany.class);

    private Connection conn;
    private ResultSet rs;
    private PreparedStatement pst;

    //前端调用该方法
    public  String  test(Company company)
    {
        
            return company.getCompanyName();
    }
    
}

5.编写jsp页面及js

jsp页面中,引入以下:

<!--引入以下三个js,这些路径是隐含存在的,你不需要手动去找,前提是你已经导入dwr包—>

    <script type='text/javascript' src='${pageContext.request.contextPath}/dwr/engine.js'></script> 
    <script type='text/javascript' src='${pageContext.request.contextPath}/dwr/util.js'></script>
    <script type='text/javascript' src='${pageContext.request.contextPath}/dwr/interface/grantService.js'></script>

<!--引入js,jquery—>
    <script type="text/javascript" src="js/jquery-1.4.2.js"></script>
    <script type="text/javascript" src="js/index.js"></script>

测试  <input type="button" value="确认" onclick="getCompanyName()">

index.js中调用java方法:

function getCompanyName()
{
    var companyObj= new Object();
    companyObj.companyName='华为中兴"; //companyName对应java企业实体中的属性.
     //grantService对应于dwr.xml配置
     grantService.test(companyObj,function(msg)
    {
      alert("返回数据:"+msg);

    });


}

允许项目得到后台test方法返回的结果.

原文地址:https://www.cnblogs.com/david-rui/p/3604514.html