The integration of DWR push technology with spring MVC

Source: Internet
Author: User

The previous Web project is already based on the Spring MVC framework and now needs to do a Dynamic Web page refresh feature, using DWR push Technology, server-side updates, immediately displayed on the Web page.

1. Import Dwr.jar Package

2. Configure the Web. xml file, and all the mappings are intercepted by the spring MVC controller and then forwarded by the controllers.

<servlet> 
		<servlet-name>testDispatcher</servlet-name>
		<servlet-class> org.springframework.web.servlet.dispatcherservlet</servlet-class>
		<init-param>
			< Param-name>contextconfiglocation</param-name>
			<param-value>/web-inf/config.xml</ param-value>	
 		</init-param>
		</servlet>

	<servlet-mapping>
		<servlet-name >testDispatcher</servlet-name>
		<url-pattern>*.html</url-pattern>
	</ servlet-mapping>
	
	<servlet-mapping>
		<servlet-name>testDispatcher</servlet-name>
		<url-pattern>/dwr/*</url-pattern>
	</servlet-mapping>

3. Configure the information about DWR in the container in the Web tier.

<!--scanning annotations--
<context:component-scan base-package= "Springmvc" ></context:component-scan>
<!--View Resolver--
	<bean
		class= " Org.springframework.web.servlet.view.InternalResourceViewResolver "
		p:prefix="/view/"p:suffix=". jsp "/>
<!--through annotations, the URL is mapped to the controller, which registers defaultannotationhandlermapping and Annotationmethodhandleradapter by default-- >  
	<mvc:annotation-driven/>
<!--requires DWR to map util.js and engine.js to Dwrcontroller--  
	<dwr: url-mapping/>
<!--Configuring the DWR controller, if you want to use push, you must set the activereverseajaxenabled-->  
	<dwr:controller id= "Dwrcontroller" debug= "true" >
		<dwr:config-param name= "activereverseajaxenabled"
			value= "true"/ >
	</dwr:controller>
<dwr:configuration></dwr:configuration>

4. After the above configuration, the Spring MVC controller will be able to connect to the Web page (DWR push), for example

ServerContext ServerContext = Servercontextfactory.get ();
		collection<scriptsession> scriptsessions = ServerContext
				. getallscriptsessions ();
		Util Util = new Util (scriptsessions);
		Scriptbuffer Scriptbuffer = new Scriptbuffer ();
		Pass the object data to the Showconvertonetime () function
		scriptbuffer.appendscript ("Showconvertonetime (") in JavaScript. AppendData (Object)
		. Appendscript (");");
		Util.addscript (Scriptbuffer);

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.