dwr是属于Ajax框架的一种技术,其主要原理就是通过配置文件动态的将服务器端的java方法生成javascript方法,使客户端页面的js能方便的调用后台方法来处理数据,减轻服务器的压力而且实现了页面的局部刷新功能,下面就来简单的说一下Spring整合Dwr的步骤:
想要使用dwr必须需要这几个文件:dwr.jar , engine.js , util.js 这三个文件,这里没有上传 自己下载吧。
第一步:新建web工程,名称自己起(随便) 配置web.xml文件:
web.xml文件配置如下: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>
- <!-- Spring监听文件 -->
- <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>
- <!-- dwr配置文件 -->
- <servlet>
- <servlet-name>dwr</servlet-name>
- <servlet-class>
- org.directwebremoting.spring.DwrSpringServlet
- </servlet-class>
- <init-param>
- <description>调试DWR,发布系统时应将其设为false</description>
- <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>
- </servlet>
- <servlet-mapping>
- <servlet-name>dwr</servlet-name>
- <url-pattern>/dwr/*</url-pattern>
- </servlet-mapping>
- <!-- 如果你想在你的service中使用request 请在web.xml文件中加入 Spring中添加 request -->
- <!-- <listener>
- <listener-class>
- org.springframework.web.context.request.RequestContextListener
- </listener-class>
- </listener> -->
- </web-app>
复制代码
第二步:建立一个简单的测试类:test.java
- package com.web.utils;
- public class Test
- {
- public void test()
- {
- System.out.println("测试一下使用dwr框架在前台页面调用方法------------------");
- }
- }
复制代码
第三部:添加Spring配置文件:
applicationContext .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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.directwebremoting.org/schema/spring-dwr
http://www.directwebremoting.org/schema/spring-dwr-2.0.xsd">
<!-- mysql数据源 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>
jdbc:mysql://127.0.0.1:3306/mydata?useUnicode=true&characterEncoding=utf-8
</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>root</value>
</property>
</bean>
<!-- sql server 2005数据源 -->
<!-- <bean id="dbcpDataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.microsoft.sqlserver.jdbc.SQLServerDriver">
</property>
<property name="url"
value="jdbc:sqlserver://192.168.1.18:1433;databaseName=stcaimsTest;">
</property>
<property name="username" value="sa"></property>
<property name="password" value="123"></property>
<property name="defaultCatalog" value="stcaimsTest"></property>
<property name="initialSize" value="2"></property>
<property name="defaultAutoCommit" value="true"></property>
<property name="removeAbandoned" value="true"></property>
<property name="testOnBorrow" value="true"></property>
<property name="validationQuery" value="select 1 "></property>
</bean> -->
<!-- hibernate配置文件 -->
<!-- <bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:com/st/caims/lxj/db/hibernateSqlServer.cfg.xml">
</property>
<property name="dataSource" ref="dbcpDataSource"></property>
</bean> -->
<!--这里导入的是dwr的配置文件-->
<import resource="applicationContext-dwr.xml" />
</beans>
第四步:dwr的配置文件:applicationContext-dwr.xml:
第五步:建立一个jsp页面:test.jsp
- <%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
- <%
- 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>
- <title>test.jsp</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 type='text/javascript' src='/dwr/engine.js'></script>
- <script type='text/javascript' src='/dwr/util.js'></script>
- <script type='text/javascript' src='/dwr/interface/DSjavascript.js'></script>
- <!--
- <link rel="stylesheet" type="text/css" href="styles.css">
- -->
- </head>
- <script type="text/javascript">
- DSjavascript.test();
- </script>
- <body>
- 简单测试dwr
- </body>
- </html>
复制代码
页面使用的时候一定要注意引用dwr提供的js包,就是以下两个:
- <script type='text/javascript' src='/dwr/engine.js'></script>
- <script type='text/javascript' src='/dwr/util.js'></script>
复制代码
下面这个就是自动生成的javascript方法名,对应java方法:
- <script type='text/javascript' src='/dwr/interface/DSjavascript.js'></script>
复制代码
使用是直接调用即可:DSjavascript.test();
到此Spring 整合Dwr步骤就结束了,这里只是做个简单的配置,可作为参考,日后使用时可自行深入配置。
版权归作者所有,受法律保护,转载请注明出处:
http://dnew168.com/thread-1873-1-1.html