dwr整合spring

dwr是一种非常方便的技术,本文简单介绍dwr整合spring的一点点知识。

例如:

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.jeecms.cms.entity.main.CmsSafeDay;
import com.jeecms.cms.manager.main.CmsSafeDayMng;

/**
* safe days dwr service
*
* @author Joshua
*
*/
public class SafeDwr {

@Autowired
private CmsSafeDayMng cmsSafeDayMng;

public List<CmsSafeDay> list() {
System.out.println(“DWRDWRDWR”);
return cmsSafeDayMng.getListForTag(6);
}

}

private CmsSafeDayMng cmsSafeDayMng;是SafeDwr类的一个属性,需要通过spring来动态注入。

web.xml配置dwr:

<!– dwr –>
<servlet>
<display-name>DWR Servlet</display-name>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
</servlet>

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

spring配置文件中配置:

<bean id=”safedwr” class=”com.casco.cms.servlet.SafeDwr”>
<!–采用注解方式 –> <!–
<property name=”cmsSafeDayDao” ref=”cmsSafeDayDao”>
</property> –>
</bean>

dwr.xml配置:

<dwr>
<allow>
<create creator=”spring” javascript=”safedwr”>
<param name=”beanName” value=”safedwr”></param>
</create>
<!–
<create creator=”new” javascript=”safedwr”>
<param name=”class” value=”com.casco.cms.servlet.SafeDwr”></param>
</create> –>
<convert converter=”bean” match=”com.jeecms.cms.entity.main.CmsSafeDay”></convert>
</allow>
</dwr>

—————-

参考示例(//from http://blog.csdn.net/llhwin2010/article/details/12193353):

1.当JSP页面的Ajax调用业务层方法时,业务需要DAO层对象,而DAO层对象我们是通过spring来注入的,如果我们直接用DWR暴露给js的对象去调用业务的方法时,就会出现错误。因为业务类DAO对象是空的。这时我们就要通过DWR整合的SPRING的形式来加载对象。
2、要用到SRPING的东西,就是必须加载SPRING的配置文件(applicationContext.xml),在这里我们通过服务启动时去加载,所以要配置修改web.xml文件。在原来的基础上添加以下配置:

Java代码 复制代码 收藏代码
  1.  <!— 这个就是用来加载SPRING配置文件的—->
  2.  <context-param>
  3.         <param-name>contextConfigLocation</param-name>
  4.         <param-value>
  5.             /WEB-INF/classes/applicationContext.xml
  6.         </param-value>
  7. </context-param>
  8. <listener>
  9.     <listener-class>
  10.           org.springframework.web.context.ContextLoaderListener
  11.     </listener-class>
  12. </listener>
  13. <!— 这个是DWR本身的配置—>
  14. <servlet>
  15.     <servlet-name>dwr</servlet-name>
  16.      <servlet-class>
  17.             org.directwebremoting.servlet.DwrServlet
  18.         </servlet-class>
  19.         <init-param>
  20.             <param-name>debug</param-name>
  21.             <param-value>true</param-value>
  22.         </init-param>
  23. </servlet>
  24. <servlet-mapping>
  25.     <servlet-name>dwr</servlet-name>
  26.     <url-pattern>/dwr/*</url-pattern>
  27. </servlet-mapping>
  1.  <!— 这个就是用来加载SPRING配置文件的—->
  2.  <context-param>
  3.         <param-name>contextConfigLocation</param-name>
  4.         <param-value>
  5.             /WEB-INF/classes/applicationContext.xml
  6.         </param-value>
  7. </context-param>
  8. <listener>
  9.     <listener-class>
  10.           org.springframework.web.context.ContextLoaderListener
  11.     </listener-class>
  12. </listener>
  13. <!— 这个是DWR本身的配置—>
  14. <servlet>
  15.     <servlet-name>dwr</servlet-name>
  16.      <servlet-class>
  17.             org.directwebremoting.servlet.DwrServlet
  18.         </servlet-class>
  19.         <init-param>
  20.             <param-name>debug</param-name>
  21.             <param-value>true</param-value>
  22.         </init-param>
  23. </servlet>
  24. <servlet-mapping>
  25.     <servlet-name>dwr</servlet-name>
  26.     <url-pattern>/dwr/*</url-pattern>
  27. </servlet-mapping>

3、SPRING 的配置不变,一样给业务类注入DAO对象

Java代码 复制代码 收藏代码
  1.    <bean id=”bidbiz” class=”com.byz.bizimpl.BidImpl”>
  2.     <property name=”biddao”>
  3.        <ref local=”BidDAO”/>
  4.     </property>
  5.     <property name=”goodsdao”>
  6.         <ref local=”GoodsDAO” />
  7.     </property>
  8.     <property name=”userdao”>
  9.         <ref local=”UsersDAO” />
  10.     </property>
  11. </bean>
  1.    <bean id=”bidbiz” class=”com.byz.bizimpl.BidImpl”>
  2.     <property name=”biddao”>
  3.        <ref local=”BidDAO”/>
  4.     </property>
  5.     <property name=”goodsdao”>
  6.         <ref local=”GoodsDAO” />
  7.     </property>
  8.     <property name=”userdao”>
  9.         <ref local=”UsersDAO” />
  10.     </property>
  11. </bean>

4、DWR配置文件配置,这时我们需要将原先的creator=”new” DWR去实例对象的,现在要改为creator=”spring” 由spring注入进来。 Dwr这有个参数 <param name=”beanName” value=”spring对象”/> 这个可以将对象接收到。

Java代码 复制代码 收藏代码
  1. <?xml version=”1.0″ encoding=”UTF-8″?>
  2. <!DOCTYPE dwr PUBLIC “-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN” “http://getahead.org/dwr/dwr20.dtd”&gt;
  3. <dwr>
  4.     <allow>
  5.         <create javascript=”BidImpl” creator=”spring”
  6.             scope=”application”>
  7.             <!– <param name=”class” value=”com.byz.bizimpl.BidImpl” /> –>
  8.             <!– 将Spring中的对象给到参数 –>
  9.             <param name=”beanName” value=”bidbiz” />
  10.             <include method=”add” />
  11.         </create>
  12.     </allow>
  13.     <!– 参数集合配置 –>
  14.     <signatures>
  15.         <![CDATA[
  16.              import java.util.List;
  17.              com.byz.bizimpl.BidImpl;
  18.              BidImpl.add(List);
  19.         ]]>
  20.     </signatures>
  21. </dwr>
This entry was posted in Computer, Java. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s