Spring+DWR 全注解版

1.Spring+DWR 全注解版 搭建 Spring3.1.1 DWR3

 1)普通调用

 2)异步文件上传

 3)反向 Ajax的全推及半推

 

2.部分配置

1)web.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>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<servlet>
		<servlet-name>dwr-invoker</servlet-name>
		<servlet-class>org.directwebremoting.spring.DwrSpringServlet</servlet-class>
		<init-param>
			<param-name>debug</param-name>
			<param-value>false</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>activeReverseAjaxEnabled</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>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

 2)applicationContext-dwr.xml

<?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:dwr="http://www.directwebremoting.org/schema/spring-dwr"
	xmlns:dwra="http://www.directwebremoting.org/schema/spring-dwr-annotations"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd      
    http://www.directwebremoting.org/schema/spring-dwr
    http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd
    http://www.directwebremoting.org/schema/spring-dwr-annotations    
  	http://www.directwebremoting.org/schema/spring-dwr-annotations.xsd">
    <!-- 	-->
    <context:annotation-config/>
	<context:component-scan base-package="com.j4t.demo" />
	<dwr:configuration>
		<!-- 
		<dwr:convert type="bean" class="com.demo.dwr3.spring3.bean.User" />
		<dwr:convert type="bean" class="java.lang.StackTraceElement" />
		<dwr:convert type="exception" class="java.lang.NullPointerException" />
		<dwr:signatures>  
            <![CDATA[ 
                import java.util.Map; 
                import java.util.List; 
                ]]>  
        </dwr:signatures>  
         -->
	</dwr:configuration>
	<dwr:annotation-scan base-package="com.j4t.demo" scanDataTransferObject="true" scanRemoteProxy="true" />
	<dwr:annotation-scan base-package="com.j4t.chat" scanDataTransferObject="true" scanRemoteProxy="true" />
	<dwr:annotation-scan base-package="com.j4t.file" scanDataTransferObject="true" scanRemoteProxy="true" />
</beans>
 
package com.j4t.demo.model;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.directwebremoting.annotations.DataTransferObject;
import org.directwebremoting.annotations.RemoteProperty;

@DataTransferObject
public class User {
	@RemoteProperty
	private String username;

	@RemoteProperty
	private String userpswd;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getUserpswd() {
		return userpswd;
	}

	public void setUserpswd(String userpswd) {
		this.userpswd = userpswd;
	}

	@Override
	public String toString() {
		/** 需要commons-langs-2.x.jar **/
		return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
	}

}
 
package com.j4t.demo.service;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.directwebremoting.spring.SpringCreator;
import org.springframework.beans.factory.annotation.Autowired;

import com.j4t.demo.dao.UserDao;
import com.j4t.demo.model.User;

@RemoteProxy(creator = SpringCreator.class, name = "userService")
public class UserService {

	@Autowired
	UserDao userDao;

	@RemoteMethod
	public void saveUser(User user)throws Exception {
		userDao.save(user);
	}
	
	@RemoteMethod
	public void saveUsers(List<User> ulist) {
		for(User user:ulist){
			userDao.save(user);
		}
	}
	@RemoteMethod
	public String sayHello(String msg,HttpServletRequest request)throws Exception {
		return msg+" "+request.getSession().getId();
	}
	
}

你可能感兴趣的:(spring)