1.web.xml中加载DWR框架:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="" xmlns:xsi="" xsi:schemaLocation=" /web-app_2_5.xsd"> <servlet> <servlet-name>dwr</servlet-name> <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class> <init-param> <param-name>debug</param-name> <param-value>false</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dwr</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
|
其中debug属性是测试用的,输入 可以看出生成的util.js,engine.js的路径,在jsp页面引入,发布时候要设置为false!
java类:
package Hello;
public class Hello { public String sayHello(String yourName) { if (yourName == null || "".equals(yourName)) { yourName = "World"; } return "Hello " + yourName + "!"; } }
|
配置dwr.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "">
<dwr> <allow> <create javascript="Hello" creator="new"> <param name="class" value="Hello.Hello"></param> </create> </allow> </dwr>
|
jsp页面:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <script type="text/javascript" src="/DWR/dwr/engine.js"></script> <script type="text/javascript" src="/DWR/dwr/util.js"></script> <script type="text/javascript" src="/DWR/dwr/interface/Hello.js"></script> <script type="text/javascript"> function sayHello(yourName) { var yourName = $("yourName").value; Hello.sayHello(yourName, callBack); } function callBack(data) { DWRUtil.setValue("show",data); } </script> <title>My JSP 'index.jsp' starting page</title> </head> <body> Please input your name:<input type="text" id="yourName" /> <input type="button" value="Click Me!" onClick="sayHello();"/> <br /> <span id="show" style="background-color:#ffff00"></span> </body> </html>
|
阅读(3913) | 评论(0) | 转发(0) |