dwr快速入门:http://directwebremoting.org/dwr/introduction/getting-started.html
jetty-maven-plugin配置:http://www.eclipse.org/jetty/documentation/current/jetty-maven-plugin.html
一.安装并配置好maven.新建pom.xml,导入已存在工作空间的pom工程.
pom.xml
<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.0</modelVersion> <groupId>com.test</groupId> <artifactId>testdwr</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>testweb Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <build> <pluginManagement> <plugins> <plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.1.2.v20140210</version> <configuration> <scanIntervalSeconds>200</scanIntervalSeconds> <webApp> <contextPath>/testdwr</contextPath> </webApp> <httpConnector> <port>80</port> <idleTimeout>60000</idleTimeout> </httpConnector> </configuration> </plugin> </plugins> </pluginManagement> </build> <dependencies> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.3</version> </dependency> <dependency> <groupId>org.directwebremoting</groupId> <artifactId>dwr</artifactId> <version>3.0.M1</version> </dependency> </dependencies> </project>
二.
1.配置web.xml
<?xml version="1.0" encoding="UTF-8"?> <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>dwr</display-name> <servlet> <display-name>DWR Servlet</display-name> <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> </servlet> <servlet-mapping> <servlet-name>dwr-invoker</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
2.pojo:User.java
package com.test.domain; public class User { private int id; private String username; public User(int id,String username) { this.id=id; this.username=username; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } }3.service:DwrTest.java
package com.test.web; import com.test.domain.User; public class DwrTest { public User testUser(int id,String username){ return new User(id,username); } }
4.将bean暴露出去,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" javascript="DwrTest"> <param name="class" value="com.test.web.DwrTest" /> </create> <convert converter="bean" match="com.test.domain.User"></convert> </allow> </dwr>
5.页面:testdwr.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <Meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <script src="<%=request.getContextPath()%>/dwr/engine.js" type="text/javascript"></script> <script src="<%=request.getContextPath()%>/dwr/util.js" type="text/javascript"></script> <script src="<%=request.getContextPath()%>/dwr/interface/DwrTest.js" type="text/javascript"></script> <script type="text/javascript"> DwrTest.testUser(2,"xiejx618",function(data){ console.log(data); }); function testClick(){ alert(dwr.util.getValue("xxx")); } </script> </head> <body> <input type="text" id="xxx"/> <input type="button" value="text" onclick="testClick()"/> </body> </html>
6.在cmd或eclipse运行mvn jetty:run,打开chrome浏览器输入http://localhost/testdwr/testdwr.jsp 查看浏览器开发工具console