DWR3反转引擎
DWR3 入门:
DWR3使用Ajax反转引擎(Reverse Ajax) 进行消息传递:
--------------------------------
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"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"
default-autowire="byName" default-lazy-init="true">
<description>Spring公共配置</description>
<!-- 定义受环境影响易变的变量 -->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName"
value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<!-- 标准配置 -->
<value>
classpath*:/config/db/application.properties
</value>
</list>
</property>
</bean>
<!-- 数据源配置,使用应用内的DBCP数据库连接池 -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<!-- Connection Info -->
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- Connection Pooling Info -->
<property name="initialSize" value="5" />
<property name="maxActive" value="100" />
<property name="maxIdle" value="30" />
<property name="maxWait" value="1000" />
<property name="poolPreparedStatements" value="false" />
<property name="defaultAutoCommit" value="false" />
</bean>
<!-- Hibernate配置 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="namingStrategy">
<bean class="org.hibernate.cfg.ImprovedNamingStrategy" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
${hibernate.dialect}
</prop>
<prop key="hibernate.show_sql">
${hibernate.show_sql}
</prop>
<prop key="hibernate.format_sql">
${hibernate.format_sql}
</prop>
<prop key="hibernate.cache.provider_class">
org.hibernate.cache.EhCacheProvider
</prop>
<prop
key="hibernate.cache.provider_configuration_file_resource_path">
${hibernate.ehcache_config_file}
</prop>
</props>
</property>
<property name="packagesToScan">
<list>
<value>com.wpms.entity.*</value>
</list>
</property>
</bean>
<!-- 事务管理器配置,单数据源事务 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 使用annotation定义事务 -->
<tx:annotation-driven transaction-manager="transactionManager"
proxy-target-class="true" />
<!-- 启用 annotation 配置模式 -->
<context:annotation-config />
<!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 -->
<context:component-scan base-package="com.wpms.user.*" />
<!-- DWR 配置 BEGIN -->
<!-- 启动 DWR 注解配置模式 -->
<dwr:configuration />
<dwr:annotation-config />
<dwr:url-mapping />
<!-- 开启dubug状态 -->
<dwr:controller debug="true" />
<!-- DWR 配置 END-->
</beans>
--------------------------------
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">
<!-- spring config url start-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:config/dwr3/applicationContext.xml
</param-value>
</context-param>
<!-- spring config url end-->
<!-- 著名的 Character Encoding filter -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<!--Hibernate Open Session in View Filter-->
<!-- 假设在你的应用中Hibernate是通过spring 来管理它的session.如果在你的应用中没有使用OpenSessionInViewFilter
或者OpenSessionInViewInterceptor。session会在transaction结束后关闭,此时会抛出session is close 的异常-->
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
</filter>
<!-- struts2 start-->
<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>
<!-- struts2 end -->
<!-- 过滤器映射 -->
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- dwr配置 -->
<servlet>
<servlet-name>dwr</servlet-name>
<servlet-class>
org.directwebremoting.spring.DwrSpringServlet
</servlet-class>
<init-param>
<param-name>activeReverseAjaxEnabled</param-name>
<param-value>true</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>
<!--Spring ApplicationContext 载入 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- 扩展spring bean的作用域有request,session,global session等-->
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
<!-- Spring 刷新Introspector防止内存泄露 -->
<listener>
<listener-class>
org.springframework.web.util.IntrospectorCleanupListener
</listener-class>
</listener>
<!-- 开始页面 -->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<error-page>
<error-code>404</error-code>
<location>/pagenotfound.jsp</location>
</error-page>
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/error.jsp</location>
</error-page>
</web-app>
--------------------------------
--------------------------------
HTML source:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<script type='text/javascript'
src='${pageContext.request.contextPath}/dwr/interface/putpageinfo.js'></script>
<script type='text/javascript'
src='${pageContext.request.contextPath}/dwr/engine.js'></script>
<script type='text/javascript'
src='${pageContext.request.contextPath}/dwr/util.js'></script>
<script type='text/javascript'>
function putstart(){
alert("begin");
putpageinfo.begin();
}
function reverseFunction(data){
document.all.backtext.value=data;
}
</script>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body onload="dwr.engine.setActiveReverseAjax(true);">
<input id="submit" type="submit" value="begin" onclick="putstart()" />
<input type="text" id="backtext" name="backtext" value="" />
</body>
</html>
--------------------------------
Javascript source:
--------------------------------
Java source:
package com.wpms.user.action.dwr;
import org.directwebremoting.Browser;
import org.directwebremoting.ScriptSessions;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;
import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.springframework.stereotype.Controller;
@Controller
@RemoteProxy(name = "putpageinfo")
public class PutSomethingToPage extends Thread {
private static WebContext wctx = null;
@RemoteMethod
public void begin() {
if (wctx == null) {
wctx = WebContextFactory.get();
}
System.out.println("put begin!");
String page = wctx.getCurrentPage();
Browser.withPage(page, new Runnable() {
public void run() {
ScriptSessions.addScript("reverseFunction(12)");
}
});
}
}
分享到:
相关推荐
- 配置:首先,需要在Spring配置文件中声明DWR的相关bean,包括DWR引擎、配置信息等。接着,配置Hibernate的数据源、SessionFactory,并将SessionFactory注入到需要进行数据库操作的bean中。 - 整合DWR与Spring:DWR...
4. **启动DWR**:在Web应用的初始化阶段,加载DWR的配置,并启动DWR引擎。 5. **前端调用**:在JavaScript中,通过DWR生成的接口直接调用后端JavaBean的方法。 **文件名称列表分析** 由于只给出了jt_dwr_spring...
Spring是一个轻量级的Java开发框架,它提供了控制反转(IoC)和面向切面编程(AOP)的功能,可以有效地管理和组织Java应用程序中的依赖关系。Spring 2框架增加了对AspectJ的支持,提供了更丰富的事务管理功能,并...
8. dwr.jar:Direct Web Remoting (DWR)框架的库,允许在浏览器和服务器之间进行实时的JavaScript到Java的调用。 9. javassist.jar:Java编程辅助工具,提供了运行时修改字节码的能力,常用于AOP框架。 10. antlr-...
- Spring是一个全面的企业级应用开发框架,实现了控制反转(IoC)和面向切面编程(AOP),负责业务层的逻辑处理。 - Hibernate是对象关系映射(ORM)工具,负责数据持久层的操作,实现了Java对象与数据库表之间的...
3. Spring和Java EE的整合: 文档显示了Spring如何与Java EE技术如Servlets和JSP整合,形成了MVC(Model-View-Controller)架构,以实现分离的业务逻辑层、数据访问层和表现层。 4. Hibernate和ORM: Hibernate是一个...
- **DWR (Direct Web Remoting)**:允许JavaScript直接调用Java方法,实现前后端通信。 4. **设计模式**: - **MVC**:模型-视图-控制器架构模式,分离业务逻辑、用户界面和数据存储。 - **DAO (Data Access ...
- **Struts、Hibernate、Spring**:流行的企业级Java开发框架,Struts负责控制层,Hibernate处理数据持久化,Spring提供全面的AOP(面向切面编程)和IOC(控制反转)支持。 - **AJAX**:用于提高用户体验,实现...
18. Velocity模板引擎使用:Velocity是一个模板引擎,可以用于Web开发中分离视图和数据模型。本题询问考生是否使用过Velocity模板。 19. AJAX框架使用:AJAX是一种在无需重新加载整个页面的情况下,能够更新部分...