2.在pom.xml加入以下依赖:
<dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.3</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> <dependency> <groupId>org.directwebremoting</groupId> <artifactId>dwr</artifactId> <version>3.0.M1</version> </dependency>
3. 在web.xml中配置:
<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>
4. 在WEB-INF目录中创建dwr.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN" "http://directwebremoting.org/schema/dwr30.dtd"> <dwr> <allow> <!--create表示将一个Java对象公布为一个js对象,这样就会自动创建一个HelloDwr.js--> <create creator="new"> <!--可以加入javascript="hello",这样生成js对象名就是hello.js--> <param name="class" value="dwr.HelloDwr"/> </create> </allow> </dwr>
5. 创建dwr包下的类HelloDwr.java
package dwr; public class HelloDwr { public String say(String name){ System.out.println("name :" +name); return "hello"+name; } }
6. 创建dwr01.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'dwr01.jsp' starting page</title> <!-- 必须引入dwr的engine.js --> <script type="text/javascript" src="<%=path%>/dwr/engine.js"></script> <!-- 将java的类引入 --> <script type="text/javascript" src="<%=path%>/dwr/interface/HelloDwr.js"></script> <script type="text/javascript"> HelloDwr.say("Hello"); </script> </head> <body> This is my JSP page. <br> </body> </html>7.启动jetty容器。 打开jsp页面,便可在server console中看到name :Hello. 原文链接:https://www.f2er.com/ajax/165525.html