将dwr集成到spring mvc(dwr的配置是基于xml)

前端之家收集整理的这篇文章主要介绍了将dwr集成到spring mvc(dwr的配置是基于xml)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

参考资料:http://directwebremoting.org/dwr/documentation/server/integration/spring.html和http://www.butterdev.com/dwr/2008/02/dwr-20x-spring-2x-with-spring-mvc/

至于想使用基于注解的dwr配置可参考http://directwebremoting.org/dwr/documentation/server/integration/spring.html.

用了几天去了解dwr,真的没有感受到dwr的强大.我现在的理解就是在本地使用javascript透明地远程调用服务器应用的java方法.现在好像用不上啊...至于什么无刷新页面的更新,由它去吧...


1.新建pom.xml,并导入此maven工程:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.test</groupId>
	<artifactId>testdwr</artifactId>
	<packaging>war</packaging>
	<version>1.0-SNAPSHOT</version>
	<name>testweb Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<spring.version>3.2.7.RELEASE</spring.version>
	</properties>
	<build>
		<pluginManagement>
			<plugins>
				<plugin>
					<groupId>org.apache.maven.plugins</groupId>
					<artifactId>maven-compiler-plugin</artifactId>
					<configuration>
						<source>1.7</source>
						<target>1.7</target>
					</configuration>
				</plugin>
				<plugin>
					<groupId>org.eclipse.jetty</groupId>
					<artifactId>jetty-maven-plugin</artifactId>
					<version>9.1.2.v20140210</version>
					<configuration>
						<scanIntervalSeconds>200</scanIntervalSeconds>
						<webApp>
							<contextPath>/testdwr</contextPath>
						</webApp>
						<httpConnector>
							<port>80</port>
							<idleTimeout>60000</idleTimeout>
						</httpConnector>
					</configuration>
				</plugin>
			</plugins>
		</pluginManagement>
	</build>
	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>2.3.1</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.1.0</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet.jsp.jstl</groupId>
			<artifactId>javax.servlet.jsp.jstl-api</artifactId>
			<version>1.2.1</version>
		</dependency>
		<dependency>
			<groupId>commons-logging</groupId>
			<artifactId>commons-logging</artifactId>
			<version>1.1.3</version>
		</dependency>
		<dependency>
			<groupId>org.directwebremoting</groupId>
			<artifactId>dwr</artifactId>
			<version>3.0.M1</version>
		</dependency>
	</dependencies>
</project>
2.配置web.xml.要注意两个servlet-mapping的顺序.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"
	Metadata-complete="true">
	<servlet>
		<servlet-name>dispatcher</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:applicationContext.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>dispatcher</servlet-name>
		<url-pattern>/dwr/*</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>dispatcher</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	<filter>
		<filter-name>encodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
		<init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>

</web-app>
3.spring的配置文件applicationContext.xml.(变态的现在是http://directwebremoting.org/schema/spring-dwr-3.0.xsd不能加www,估计域名www.directwebremoting.org没有解析到部署站点的主机ip,造成http://www.directwebremoting.org/schema/spring-dwr/spring-dwr-3.0.xsd不能访问)
<?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:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc" 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.xsd
         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
         http://www.directwebremoting.org/schema/spring-dwr http://directwebremoting.org/schema/spring-dwr-3.0.xsd">
	<!-- 过滤html文件,直接返回 -->
	<mvc:resources location="/" mapping="**/*.html"/>
	
	<!-- 创建一个dwr controller用来映射dwr请求 -->
	<dwr:controller id="dwrController" debug="true" />
	<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
		<property name="alwaysUseFullPath" value="true" />
		<property name="mappings">
			<props>
				<prop key="/dwr/**/*">dwrController</prop>
			</props>
		</property>
	</bean>
	<dwr:configuration>
		<!-- converters -->
		<dwr:convert type="bean" class="com.test.domain.User" />
	</dwr:configuration>
	<!-- 暴露服务 -->
	<bean id="dwrService" class="com.test.dwr.DwrService">
		<dwr:remote javascript="dwrService">
			<dwr:include method="testUser" />
		</dwr:remote>
	</bean>
	
	<!-- 以下是一般的spring mvc配置 -->
 	<context:component-scan base-package="com.test.web" />
	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
		<property name="messageConverters">
			<list>
				<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
					<property name="supportedMediaTypes">
						<list>
							<value>text/html;charset=UTF-8</value>
						</list>
					</property>
				</bean>
			</list>
		</property>
	</bean>
	<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
		<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
		<property name="prefix" value="/WEB-INF/" />
		<property name="suffix" value=".jsp" />
	</bean>
</beans>
4.写上pojo,dwr的暴露service等

5.运行mvn jetty:run,在google浏览器输入http://localhost/testdwr/index.html,查看开发工具的console.顺便测试http://localhost/testdwr/user/save和http://localhost/testdwr/user/get是否正常.

源码:http://download.csdn.net/detail/xiejx618/6980795

原文链接:https://www.f2er.com/ajax/165212.html

猜你在找的Ajax相关文章