DWR is the last available AJAX-RPC toolkit on the Java platform to improve the interaction of Web page ends with Java classes. It makes Ajax development much simpler. Using the DWR framework, the client uses JavaScript to invoke Java methods directly on the server side and return the value to JavaScript as if it were a direct local client invocation. To put it more simply, DWR provides an engine file Engine.js that exposes the methods of server-side Java objects to JavaScript. Using DWR can effectively eliminate all of the AJAX request-response loops from the application code. This means that client code no longer needs to deal directly with the XMLHttpRequest object or the server's response. You no longer need to write serialization code for an object or use a Third-party tool to turn an object into XML. You don't even need to write a servlet code to adjust the Ajax request to a call to a Java domain object.
So how do you integrate DWR with spring to achieve front and back interaction? I'll go through an example below. This example is still divided into the following sections:
1. Introduce the relevant JAR package:
Commons-logging-1.1.1.jar
Dwr.jar
Spring.jar
2. Preparation of Web.xml documents:[HTML] View Plain Copy <?xml version= "1.0" encoding= "UTF-8"?> "2.4" xmlns= "HTTP://JAVA.SUN.COM/XML/NS/J2EE" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "http://java.sun.com/xml/ns/j2ee http:// Java.sun.com/xml/ns/j2ee/web-app_2_4.xsd "> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </ welcome-file-list> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:config/app*.xml</ param-value> &NBsp; </context-param> <listener> <listener-class> org.springframework.web.context.contextloaderlistener</listener-class> < /listener> <!-- Configuration DWR Front-End controller --> <servlet> <servlet-name>dwr-invoker</ servlet-name> <servlet-class> org.directwebremoting.servlet.dwrservlet</servlet-class> < init-param> < param-name>config</param-name> <!-- Load Dwr.xml file, which is located in the Config folder under src --> <param-value> /WEB-INF/classes/config/dwr.xml </param-value> </init-param> <!-- Open dwr debug mode --> < 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> </web-app>
3. Configure Dwr.xml Files:[HTML] View Plain Copy <?xml version= "1.0" encoding= "UTF-8"?> <! doctype dwr public "-//getahead limited//dtd direct web remoting 2.0// EN " " HTTP://GETAHEAD.ORG/DWR/DWR20.DTD "> <dwr> <allow> <create javascript= "T10" creator= "Spring" > <param name= " Beanname " value=" Dwrtest "></param> </create> <create javascript = "T11" creator= "Spring" > <param name= "Beanname" value= "Dwrtest2" ></param> </create> &nbSp </allow> </dwr>
4. Configure Applicationcontext-beans Files:[HTML] View Plain Copy <?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx= "Http://www.springframework.org/schema/tx" xsi:schemalocation= "http://www.springframework.org/schema/ beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http:// www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.sprIngframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd "> <bean Id= "Dwrtest" class= "com.bjsxt.dwrtest.Test10" ></bean> <bean id= "Dwrtest2" class= "com.bjsxt.dwrtest.Test11" ></bean> </beans>
5. Write the background Java class Code: [Java] view plain copy package com.bjsxt.dwrtest; public class Test11 {public String method1 () {return "call method 111111"; Public String Method2 () {return "call method 222222"; } }
[Java] view plain copy package com.bjsxt.dwrtest; public class Test10 {public String method1 () {return "call method 0001"; } }
6. Write JSP page code:[HTML] View plain copy <%@ 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 ">