关于dwr的概念,摘自百度百科:
DWR(Direct Web Remoting)是一个用于改善web页面与Java类交互的远程服务器端Ajax开源框架,可以帮助开发人员开发包含AJAX技术的网站。它可以允许在浏览器里的代码使用运行在WEB服务器上的JAVA函数,就像它就在浏览器里一样。
DWR框架使用之一:单独使用
步骤:
1、在pom.xml中配置依赖。(就是加载jar包)
2、在web.xml中配置servlet。
3、写一个普通java类。
5、写一个jsp页面,主要导入engine.js和@H_403_54@相应的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>
<?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>
<%@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 原文链接:https://www.f2er.com/ajax/163398.html