关于dwr2与spring2集成

一、uk.ltd.getahead.dwr.DWRServlet,使用dwr.xml文件
(1)、web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.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</servlet-name>
        <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
	    <init-param>
	      <param-name>config-admin</param-name>
	      <param-value>WEB-INF/classes/dwr.xml</param-value>
	    </init-param>
        <init-param>
            <param-name>debug</param-name>
            <param-value>true</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>dwr</servlet-name>
        <url-pattern>/dwr/*</url-pattern>
    </servlet-mapping>

</web-app>

(2)、applicationContext.xml文件
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"
       default-autowire="byName">

	<bean id="dataSource"
		class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName"
			value="com.mysql.jdbc.Driver" />
		<property name="url"
			value="jdbc:mysql://localhost:3306/springapp" />
		<property name="username" value="root" />
		<property name="password" value="root" />
	</bean>	

	<bean id = "transactionManager"    
         class = "org.springframework.jdbc.datasource.DataSourceTransactionManager">    
         <property name = "dataSource" ref="dataSource"/>    
    </bean>   

	<bean id = "jdbcTemplate"    
         class = "org.springframework.jdbc.core.JdbcTemplate">    
         <property name = "dataSource" ref="dataSource"/>    
    </bean>   
	
	<bean id="baseTransactionProxy" lazy-init="true" abstract="true"
		class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
		<property name="transactionManager" ref="transactionManager" />
		<property name="transactionAttributes">
			<props>
	            <prop key="delete*">PROPAGATION_REQUIRED,-Exception</prop>   
	            <prop key="update*">PROPAGATION_REQUIRED,-Exception</prop>      
	            <prop key="save*">PROPAGATION_REQUIRED,-Exception</prop>      
				<prop key="is*">PROPAGATION_REQUIRED,readOnly</prop>
	            <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
	    	</props>
		</property>
	</bean>   	

   	<bean id="userService" parent="baseTransactionProxy">
		<property name="target">
			<bean class="com.logcd.service.impl.UserService"/>
		</property>
	</bean>

</beans>

(3)、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="dwrUser" scope="application">    
			<param name="beanName" value="userService"/>
			<include method="findUserCount"/>
		</create>
		
	</allow>    
</dwr> 

(4)、接口和实现
public interface IUserService {

    public int findUserCount();
	
}

import org.springframework.jdbc.core.JdbcTemplate;
import com.logcd.service.IUserService;

public class UserService implements IUserService{

	private JdbcTemplate jdbcTemplate;

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	
	
	public int findUserCount(){
		
		return getJdbcTemplate().queryForInt(" SELECT COUNT(*) FROM LOG_USER ");
		
	}
	
}

(5)、测试页面
<%@ page language="java" contentType="text/html; charset=UTF-8"  
  pageEncoding="UTF-8"%>   
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">   
<html>   
<head>   
<title>DWR Test</title>
<script type='text/javascript' src='dwr/interface/dwrUser.js'></script>
<script type='text/javascript' src='dwr/engine.js'></script>   
<script type='text/javascript' src='dwr/util.js'></script>   
<script language="javascript">

   function showTotal(){
	dwrUser.findUserCount(function(data){
	    DWRUtil.setValue("userCnt",data); 
	});
   }

    function clearInput(){   
        userCnt.value="";   
    }      
    
</script>    
</head>    
<body>   
    <input type="button" value="显示总人数" onclick="showTotal();">
    <input type="button" value="清空" onclick="clearInput()"><br>            
    <input type="text" id="userCnt"><br>       
</body>    
</html> 

二、org.directwebremoting.spring.DwrSpringServlet,去掉dwr.xml
(1)web.xml
<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >

    ......

    <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>
    </servlet>

    <servlet-mapping>
        <servlet-name>dwr</servlet-name>
        <url-pattern>/dwr/*</url-pattern>
    </servlet-mapping>

</web-app>

(2)applicationContext.xml。要在名称空间中增加xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
       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
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
       http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-2.0.xsd"
       default-autowire="byName">

    ......    
   
    <bean id="userService" parent="baseTransactionProxy">
	<property name="target">
		<bean class="com.logcd.service.impl.UserService"/>
	</property>
    </bean>


    <bean id="dwrUser" class="com.logcd.ajax.DWRUser">
        <dwr:remote javascript="dwrUser">
        	<dwr:include method ="findUserCount"/>
        </dwr:remote>
    </bean>

</beans>

(3)、注入Service的Java Bean。
import com.logcd.service.IUserService;

public class DWRUser {

    private IUserService userService;

    public IUserService getUserService() {
	return userService;
    }

    public void setUserService(IUserService userService) {
	this.userService = userService;
    }
	
    public int findUserCount(){
    	return this.getUserService().findUserCount();
    }
	
}

    测试页面与前面的index.jsp相同。
疑问:
    如果不写dwr.xml文件,要写一个AJAX操作类(如上面的DWRUser)。这样还没有直接用dwr.xml方便!初次接触,不大了解这玩意儿。要是可以直接下面这样写就好了。
<bean id="userService" parent="baseTransactionProxy">
    <property name="target">
        <bean class="com.logcd.service.impl.UserService"/>
    </property>
    <dwr:remote javascript="dwrUserService"/>
</bean>

你可能感兴趣的:(spring,Ajax,xml,Web,DWR)