DWR之DWR实例(1)

前端之家收集整理的这篇文章主要介绍了DWR之DWR实例(1)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

  1. 原文出处:http://www.jb51.cc/article/p-dnhaebgx-pp.html

DWR之DWR实例(1)

----------

1.首先向项目中加入dwr.jar文件,这里我们使用maven构建,同时dwr还要依赖commons-logging.jar包,如例(pom.xml):

[html] view plain copy
  1. <projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/maven-v4_0_0.xsd">
  3. modelVersion>4.0.0</>
  4. groupId>aaaartifactId>bbbpackaging>warversion>0.0.1-SNAPSHOTname>bbbMavenWebappurl>http://maven.apache.orgdependenciesdependency>junit>3.8.1scope>test <!--必需的依赖-->
  5. >org.directwebremoting>dwr>3.0.M1>commons-logging>1.1.1buildfinalNameproject>


2.在web.xml文件增加DWRSevlet的配置,DWR指定映射路径的js(如:带有/dwr)都由这个servlet处理,就像这些路径的js(带有/dwr的路径):

copy
    scriptsrc='/[YOUR-WEBAPP-CONTEXT]/dwr/interface/[YOUR-SCRIPT].js'>scriptscriptsrc='/[YOUR-WEBAPP-CONTEXT]/dwr/engine.js'>

这两个件都是虚拟文件,并不是真实的文件

web.xml的配置如下:

copy
    <?xmlversion="1.0"encoding="UTF-8"?>
  1. web-appxmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
  3. version="2.4"
  4. display-name>firstDWRwelcome-file-listwelcome-file>index.jsp <!--TheDWRServlet...-->
  5. servlet>DWRServletservlet-name>dwr-invokerservlet-class>org.directwebremoting.servlet.DwrServlet<!--初始化参数,增加调试功能-->
  6. init-paramparam-name>debugparam-value>true<!--跨域调用配置信息,不是必须的-->
  7. >crossDomainSessionSecurity>falseservlet-mappingurl-pattern>/dwr/*
  8. web-app>


3.创建dwr.xml文件(与web.xml在同一个目录):

copy
    <!DOCTYPEdwrPUBLIC
  1. "-//GetAheadLimited//DTDDirectWebRemoting3.0//EN""http://getahead.org/dwr/dwr30.dtd"dwrallowcreatecreator="new"javascript="MathDelegate"paramname="class"value="app.MathDelegate"/>
  2. create>


4.创建java文件,如:MathDelegate.java文件

[java] copy @H_605_502@
    packageapp;
  1. /**
  2. *数值计算
  3. *@authorfuhd
  4. */
  5. publicclassMathDelegate{
  6. /**加法*/
  7. intadd(inta,intb){
  8. returna+b;
  9. }
  10. /**减法*/
  11. intsubtract(returna-b;
  12. /**乘法*/
  13. intmultiply(returna*b;
  14. /**除法*/
  15. intdivide(returna/b;
  16. }

5.创建jsp文件(可以是html或其它模板的文件),如:index.jsp :

copy
    htmlheadtitle>firstdwrscripttype="text/javascript"src="dwr/interface/MathDelegate.js"scripttype="text/javascript"src="dwr/engine.js" vara=0;
  1. varb=0;
  2. varop="";
  3. functiondoMath(){
  4. a=document.getElementById("numA").value;
  5. b=document.getElementById("numB").value;
  6. op=document.getElementById("op").value;
  7. if(op=="add"){
  8. MathDelegate.add(a,b,doMathCallback);
  9. op="+";
  10. }elseif(op=="subtract"){
  11. MathDelegate.subtract(a,doMathCallback);
  12. op="-";
  13. }elseif(op=="multiply"){
  14. MathDelegate.multiply(a,248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> op="*";
  15. }elseif(op=="divide"){
  16. MathDelegate.divide(a,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> op="/";
  17. }
  18. vardoMathCallback=function(answer){
  19. document.getElementById("resultDiv").innerHTML="<h1>"+
  20. "Result:"+a+""+op+""+b+"="+answer+"h1>";
  21. };
  22. bodyspanid="resultDiv"span Pleaseentertwonumbers,selectanoperation,andclicktheequalsbutton:
  23. brinputtype="text"id="numA"size="4"/>
  24. selectid="op"optionvalue="add">+optionoptionvalue="subtract">-optionvalue="multiply">*optionvalue="divide">/selectinputtype="text"id="numB"size="4"inputtype="button"value="="onclick="doMath();">
  25. 原文出处:http://www.jb51.cc/article/p-dnhaebgx-pp.html
原文链接:https://www.f2er.com/ajax/165870.html

猜你在找的Ajax相关文章