DWR是Direct Web Remoting 的缩写。
是一个WEB远程调用框架.。页面可通过java业务方法来实现ajax的一个开源框架。
最初接触dwr,时,感觉最大的方便之处就是,它可以调用java的业务方法和映射java实体类。
好吧,现在来看看怎么配置dwr框架。dwr也是可以支持注解配置的。通过配置文件和注解两种方式来简单介绍一下dwr在项目的使用。
我写了俩个demo ,配置文件和注解两种方式各一个 demo,点击下载(eclipse 开发的,使用myeclipse 导入 稍作修改)
首先,加入dwr.jar包。配置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>
<convert match="com.zuxiang.entity.*" converter="bean">
</convert> <!-- 这个节点是做转换,指定那些类可转换成在页面可通过对象点属性方式使用的js对象 -->
<create creator="spring" javascript="customerService">
<!-- creator 由spring创建, 在页面使用的js,为 customerService.js -->
<param name="beanName" value="customerService" /> <!-- 由spring管理的业务类对象 customerService -->
<!--指定可供远程调用的以为方法-->
<include method="findAll" />
</create>
</allow>
</dwr>
当然,使用注解是不需要dwr.xml文件的。xml文件里 convert 指定那些类可转换,
注解是通过@DataTransferObject,标识那些类可转换,@DataTransferObject是标注类的,就是说只要这个类标注了@DataTransferObject,那么这个这个类就可供js使用,
但是有时我只想用到某两个属性怎么办?那就在属性上标注@RemoteProperty,如果标注了@RemoteProperty,那没js,可使用的就只有标了@RemoteProperty的字段了
package com.zuxiang.entity;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.directwebremoting.annotations.DataTransferObject;
/**
* CusCustomer entity. @author MyEclipse Persistence Tools
*/
@Entity
@Table(name = "customer" ,schema = "cc")
@DataTransferObject //这是个实体类,标注了@DataTransferObject ,就可供js,使用
public class Customer implements java.io.Serializable {
// Fields
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@Column(name = "cus_id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long cusId;
@Column(name = "cus_name" ,length = 20 )
// @RemoteProperty 如果只要该属性s
private String cusName;
@Column(name = "cus_addr",length = 250)
private String cusAddr;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "customer" ,fetch = FetchType.EAGER)
private List<Product> products;
// Constructors
/** default constructor */
public Customer() {
}
/** full constructor */
public Customer(String cusName, String cusAddr) {
this.cusName = cusName;
this.cusAddr = cusAddr;
}
// Property accessors
public Long getCusId() {
return this.cusId;
}
public void setCusId(Long cusId) {
this.cusId = cusId;
}
public String getCusName() {
return this.cusName;
}
public void setCusName(String cusName) {
this.cusName = cusName;
}
public String getCusAddr() {
return this.cusAddr;
}
public void setCusAddr(String cusAddr) {
this.cusAddr = cusAddr;
}
public List<Product> getProducts() {
return products;
}
public void setProducts(List<Product> products) {
this.products = products;
}
}
那么业务方法又怎么标注呢。。使用@RemoteProxy标注可远程调用的业务类,@RemoteMethod标注远程调用的方法。
如果使用Spring中逻辑层则需要进行如下的设置:
@RemoteProxy(creator = SpringCreator.class,
creatorParams = @Param(name = "beanName" ,value ="customerService"),
name = "customerService")
package com.zuxiang.biz;
import java.util.List;
import javax.annotation.Resource;
import org.directwebremoting.annotations.Param;
import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.directwebremoting.spring.SpringCreator;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.zuxiang.dao.CustomerDao;
import com.zuxiang.entity.Customer;
@Service
@Transactional
@RemoteProxy(creator = SpringCreator.class,
creatorParams = @Param(name = "beanName" ,value ="customerService"),
name = "customerService")
public class CustomerServiceImpl implements CustomerService{
@Resource
private CustomerDao customerDao;
@RemoteMethod
public List<Customer> findAll() {
// TODO Auto-generated method stub
return customerDao.findAll();
}
}
下面配置,spring,配置文件,
使用 xml.文件的spring配置,就要注意dwr.xml里的 红色部分要对应 spring 里 红色部分;
<param name="beanName"
value="customerService" />
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:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
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.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<!-- session工厂 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>
<!-- 事务管理 -->
<bean id="myHibTransactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- dao类 -->
<bean id="customerDao" class="com.zuxiang.dao.CustomerDaoImpl">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!-- 业务类 -->
<bean id="customerService" class="com.zuxiang.biz.CustomerServiceImpl">
<property name="customerDao" ref="customerDao"/>
</bean>
<!-- struts1 aciton -->
<bean name="/cus" class="com.zuxiang.struts.action.CustomerAction">
<property name="customerService" ref="customerService"></property>
</bean>
<!-- 事务的切面 -->
<tx:advice id="txAdvice"
transaction-manager="myHibTransactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="do*" propagation="REQUIRED" />
<tx:method name="*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>
<!--注入事务 -->
<aop:config>
<aop:pointcut id="bizMethods"
expression="execution(* com.zuxiang.biz.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="bizMethods" />
</aop:config>
</beans>
使用注解的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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.directwebremoting.org/schema/spring-dwr
http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd">
<context:annotation-config />
<!-- 使用注解配置 -->
<dwr:configuration />
<dwr:annotation-config />
<dwr:url-mapping />
<!-- 自动扫描指定包下的类 -->
<context:component-scan base-package="com.zuxiang">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<!-- jpa实体管理类 -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="zuxiang"/>
</bean>
<!-- 事务管理 -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<!-- dwr 扫描转换类和远程调用类 -->
<dwr:annotation-scan scanDataTransferObject="true" scanRemoteProxy="true" base-package="com.zuxiang"/>
<!-- debug 模式 -->
<dwr:controller id="dwrController" debug="true"></dwr:controller>
<!-- 使用注解事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
下面是 web.xml 配置文件,
配置文件方式的的web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>dwr2</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<description>register spring listener</description>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>MyFilter</filter-name>
<filter-class>com.zuxiang.filter.MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>MyFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<servlet>
<!-- struts1的aciton -->
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet>
<!-- struts1的aciton -->
<!-- dwr 配置 -->
<servlet-name>dwr</servlet-name>
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
<init-param>
<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>
<!-- dwr 配置 -->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
注解方式的web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>dwr3</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<description>register spring listener</description>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- struts2 配置 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- struts2 配置 -->
<!-- dwr 配置 -->
<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>
<!-- 解决CSRF Security Error 加入下面 加入跨域调用配置信息 -->
<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>
</servlet>
<servlet-mapping>
<servlet-name>dwr</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
<!-- dwr 配置 -->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
这里只是说一下相关配置,到我资源里下载demo,运行一下。更容易看懂