Ajax-------------Spring+dwr Integration __ajax

Source: Internet
Author: User

Two different ways

First, configure directly in the spring configuration file Applictioncontext

Second, separate dwr.xml file configuration

Test class:

Package com.test; public class Test {public string test (string name) {System.out.println ("---------"), return "Hello" + name + "!!!!!"; } }

The first way:

Applicationcontext.xml

<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:dwr= "Http://www.directwebremoting.org/schema/spring-dwr" xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-2.0.xsd Http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/ Spring-dwr-2.0.xsd "> <bean id=" Test "class=" Com.test.Test "> <dwr:remote javascript=" Testdwr "> <dwr : Include method= "test"/> </dwr:remote> </bean> </beans>

DWR Forwarding with Org.directwebremoting.spring.DwrSpringServlet

Xml

<?xml version= "1.0" encoding= "UTF-8"?> <web-app version= "2.5" 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_2_5.xsd "> <context-param> <param-name>contextconfiglocation </param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param > <!--Spring applicationcontext load--> <listener> <listener-class> Org.springframework.web.context.contextloaderlistener</listener-class> </listener> <servlet> <servlet-name>dwr</servlet-name> <servlet-class>org.directwebremoting.spring.dwrspringservlet </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</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </ Web-app>


Second Kind

Applicationcontext.xml (as with the general Spring bean Configuration)

<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans-2.0.xsd dwr-2.0.xsd "> <bean id=" Test "class=" Com.test.Test "> </bean> </beans>

Dwr.xml

<?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 creator= "Spring" javascript= "Testdwr (JavaScript Call name)" > <param name= "Beanname" Value= "Test (ID of the corresponding bean in spring configuration)"/> </create> </allow> </dwr>

Xml

DWR forwarding with

Org.directwebremoting.servlet.DwrServlet

<?xml version= "1.0" encoding= "UTF-8"?> <web-app version= "2.5" 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_2_5.xsd "> <context-param> <param-name>contextconfiglocation </param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param > <!--Spring applicationcontext load--> <listener> <listener-class> Org.springframework.web.context.contextloaderlistener</listener-class> </listener> <servlet> <servlet-name>dwr</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</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping> < welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app >

Call

<mce:script type= "Text/javascript" src= "/dwr/interface/testdwr.js" mce_src= "Dwr/interface/testdwr.js" >< /mce:script> <mce:script type= "Text/javascript src="/dwr/engine.js "mce_src=" Dwr/engine.js: script> <mce:script type= "Text/javascript src="/dwr/util.js "mce_src=" ></mce:script> <mce:script type= "Text/javascript" ><!--testdwr.test ("Parkingfo", {callback:function (data) {alert (data);}} ); --></mce:script>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.