几经周折,SSH集成DWR终于成功了。
struts2.0,spring2.5,hibernate3.0,dwr3.0.
下面,贴下我的配置文件。
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"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 对Spring容器进行实例化 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <!-- 指定DWR核心Servlet的名字 --> <servlet-name>dwr-invoker</servlet-name> <servlet-class> <!-- 指定DWR核心Servlet的实现类 --> <!-- org.directwebremoting.servlet.DwrServlet --> org.directwebremoting.spring.DwrSpringServlet </servlet-class> <!-- 指定DWR核心Servlet处于调试状态 --> <init-param> <param-name>debug</param-name> <param-value>true</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- 指定核心Servlet的URL映射 --> <servlet-mapping> <servlet-name>dwr-invoker</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping> </web-app>
dwr.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN" "http://getahead.org/dwr/dwr30.dtd"> <dwr> <allow> <create creator="spring" javascript="login" > <param name="beanName" value="com.lc.action.OperatorAction" /> </create> <convert converter="bean" match="com.lc.domain.Operator" /> </allow> </dwr>
注意:dwr.xml的 <create creator="spring" javascript="login" > 要与页面引入的js文件名一致,比如<script src="dwr/interface/login.js"></script>,切要与spring配置文件里的<dwr:remote>标签引入的js名一致,见如下的配置。
applicationcontext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd"> <context:annotation-config /> <!-- DWR 配置开始 --> <dwr:configuration></dwr:configuration><!-- 必须要configuration --> <dwr:controller id="dwrController" debug="true" /> <!-- DWR 配置结束 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@172.16.40.132:1521:localorc" /> <property name="username" value="hr" /> <property name="password" value="hr" /> <!-- 连接池启动时的初始值 --> <property name="initialSize" value="1" /> <!-- 连接池的最大值 --> <property name="maxActive" value="500" /> <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 --> <property name="maxIdle" value="2" /> <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 --> <property name="minIdle" value="1" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="mappingResources"> <list> <value>com/lc/domain/operator.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <value> hibernate.dialect=org.hibernate.dialect.Oracle9Dialect hibernate.hbm2ddl.auto=update hibernate.show_sql=false hibernate.format_sql=false hibernate.cache.use_second_level_cache=true hibernate.cache.use_query_cache=false hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider </value> </property> </bean> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <tx:annotation-driven transaction-manager="txManager" /> <bean id="opeDaoImp" class="com.lc.dao.imp.OperatorDaoImp"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> <bean id="loginAction" class="com.lc.action.OperatorAction"> <property name="opeDao"> <ref bean="opeDaoImp"/> </property> <dwr:remote javascript="login"></dwr:remote> </bean> </beans>
在需要用到dwr的页面引入下面三个JS
<script src="dwr/engine.js"></script>
<script src="dwr/util.js"></script>
<script src="dwr/interface/login.js"></script>
JSP页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <script src="dwr/engine.js"></script> <script src="dwr/util.js"></script> <script src="dwr/interface/login.js"></script> </head> <script type="text/javascript"> function check() { var id = document.getElementById("ope.user_id").value; var password = document.getElementById("ope.user_pass").value; login.find(id,checkFun); } function checkFun(data) { if(data) alert("ok"); else alert("failed"); } </script> <body> <form action="login.action" method="post"> <input type="text" name="ope.user_id" id="ope.user_id" ><br> <input type="password" name="ope.user_pass" onblur="check()"><br/> <input type="submit" value="提交"> </form> </body> </html>
我的demo是用MyEclipse8.5开发的,下面把源代码,和所用到的jar包截图传上来,希望对大家有所帮助。