2016年6月24日 星期五

DWR3 整合 Spring3.x

官網連結

前端取資料庫的method為例



※pom.xml

<dependency>
    <groupId>org.directwebremoting</groupId>
    <artifactId>dwr</artifactId>
    <version>3.0.1-RELEASE</version>
</dependency>
    
<dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.2</version>
</dependency>
    
<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.3.2</version>
</dependency>
    
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>3.2.15.RELEASE</version>
</dependency>
    
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>3.2.15.RELEASE</version>
</dependency>
    
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>3.2.13.RELEASE</version>
</dependency>

※springMVC也要,因為listener是web的



※web.xml

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/applicationContext.xml
    </param-value>
</context-param>
    
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
    
<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>
</servlet>
    
<servlet-mapping>
    <servlet-name>dwr</servlet-name>
    <url-pattern>/dwr/*</url-pattern>
</servlet-mapping>

※注意官方寫的順序有錯

※context-param裡的param-value要記得改



※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:dwr="http://www.directwebremoting.org/schema/spring-dwr"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
    
    <dwr:url-mapping />
    <bean class="dao.impl.DaoImpl">
        <dwr:remote javascript="dao">
            <!-- <dwr:exclude method="add" /> -->
            <dwr:include method="getChess" />
        </dwr:remote>
        <aop:scoped-proxy proxy-target-class="false" />
    </bean>
    
    <dwr:controller id="dwrController" debug="true">
        <dwr:config-param name="activeReverseAjaxEnabled" value="true" />
    </dwr:controller>
    
    <dwr:configuration>
        <dwr:convert type="bean" class="vo.Chess" javascript="ch" />
    </dwr:configuration>
    
    <dwr:annotation-scan base-package="dao.impl.DaoImpl, vo.Chess"
        scanDataTransferObject="true" scanRemoteProxy="true" />
</beans>

※<dwr:url-mapping />有時候會報錯,但不影響執行

※這些設定可以參考上面的「前端取資料庫的method」連結裡面的設定,只是寫法不同而已

※bean class還是不能寫interface

※<dwr:remote>裡面全部不寫就是全部方法都可以用,可以用debug看,dwr會有紅字提示

※include和exclude是加方法和排除方法的意思,兩者只能選其一

※include和exclude可以寫沒有定義的方法
寫在include裡,又沒寫其他的方法,那就表示沒有方法可以用
寫在exclude裡,又沒寫其他的方法,那就表示所有方法都可以用,也就是等於不寫

※dwr.xml可以整個刪除了

※更詳細的要看官網,我只寫其中一個方法而已

※annotation的部分用最下面的即可,寫法和第六篇的annotation一樣

沒有留言:

張貼留言