日期:2014-05-16  浏览次数:20446 次

采用DWR、maven保存数据到数据库

一、原理: Ajax是时下比较流行的一种web界面设计新思路,其核心思想是从浏览器获取XMLHttp对象与服务器端进行交互. DWR(Direct Web Remoting)就是实现了这种Ajax技术的一种web框架. 最近做的项目中我也将它用上了,感觉很是方便,比如动态生成javascript代码,隐藏的http协议,java代码和javascript交互的是javascript的对象(或字符串). 

二、步骤:

1.导入DWR所需要的jar包,dwr.jar ,commons-loggong.jar(日志处理),pom.xml.

<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>cn.com.liveuc.dwr</groupId>
	<artifactId>dwr</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>dwr Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.7</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.geronimo.bundles</groupId>
			<artifactId>dwr</artifactId>
			<version>3.0.M1_1</version>
		</dependency>
		<dependency>
			<groupId>commons-logging</groupId>
			<artifactId>commons-logging</artifactId>
			<version>1.1.3</version>
		</dependency>
		<dependency>
			<groupId>tomcat</groupId>
			<artifactId>servlet</artifactId>
			<version>4.1.36</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet.jsp</groupId>
			<artifactId>javax.servlet.jsp-api</artifactId>
			<version>2.3.1</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.14</version>
		</dependency>
	</dependencies>
	<build>
		<finalName>dwr</finalName>
	</build>
</project>
2.在web.xml中配置dwr的拦截器

<!--配置dwr拦截器-->
	<servlet>
		<servlet-name>dwr-invoker</servlet-name>
		<display-name>DWR Servlet</display-name>
		<description>Direct Web Remoter Servlet</description>
		<servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
		<!--dwr调试-->
		<init-param>
			<param-name>debug</param-name>
			<param-value>true</param-value>
		</init-param>
		<init-param>
			<param-name>crossDomainSessionSecurity</param-name>
			<param-value>false</param-value>
		</init-param>
		<init-param>
			<param-name>allowScriptTagRemoting</param-name>
			<param-value>true</param-value>
		</init-param>
		<init-param>
			<param-name>scriptCompressed</param-name>
			<param-value>false</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>dwr-invoker</servlet-name>
		<url-pattern>/dwr/*</url-pattern>
	</servlet-mapping>
3.编写js要调用的Java类

/**
 * js访问方法
 * @author Administrator
 *
 */
public class UserDaoADwr {
	
	UserDao userDao = new UserDao();
	
	/**
	 * 保存用户
	 * @param user 保存对象
	 * @return 是否保存成功
	 */
	@SuppressWarnings("static-access")
	public boolean saveUser(User user) {
		return userDao.saveUser(user);
	}
	
	/**
	 * 查询
	 * @param id 查询条件用户id
	 * @return 查询出的用户
	 */
	@SuppressWarnings("static-access"