DWRで、AJAXアプリ
JavaのAJAXアプリを作るフレームワークのDWRとSpringとの連携に関して!
DWRには、2系と3系がありますが、今回は、3系を使うことにします。
DWRは、3系になってから、AJAXアプリとして、
登録する部分にアノテーションが使えるようになりました。
それと、Springとの連携にかんしても一部?アノテーションになっているぽいです。
JavaのクラスをAJAXアプリとて登録する方法は、クラスのメソッドに@RemoteMethodを定義します。
それと、Springにクラスを登録する部分の定義として、@RemoteProxyを使います。
実装例としては、下記のように記述します。
import org.directwebremoting.annotations.RemoteMethod; import org.directwebremoting.annotations.RemoteProxy; import org.directwebremoting.annotations.Param; import org.directwebremoting.spring.SpringCreator; @RemoteProxy( creator = SpringCreator.class, creatorParams = @Param(name = "beanName", value = "searchPersonDwr"), name = "SearchPersonDwr") public class PersonSearchDwr { @RemoteMethod public String searchPerson(String name) { String personName = ""; return personName; } }
あとは、Springにクラスを読ませてあげます。
applicationContext.xmlにその定義を追加します。
下の2行を追加するだけです。
<dwr:configuration /> <dwr:annotation-scan base-package="<パッケージ名>"/>
annotation-scan部分のパッケージ配下を読み込んでくれます。
また、当然ですが、dwrのスキーマを読ませてあげる必要があります。
追加する名前空間は、
xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
になり、スキーマのパスは、下のパスになります。
http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd
全体的なapplicationContext.xmlは、下みたいな感じです。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2011/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:context="http://www.springframework.org/schema/context" xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-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.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd"> ...... <dwr:configuration /> <dwr:annotation-scan base-package="<パッケージ名>"/> </beans>
最後に、web.xmlにDWRのサーブレットの定義をします。
<servlet> <servlet-name>dwr-invoker</servlet-name> <servlet-class>org.directwebremoting.spring.DwrSpringServlet</servlet-class> <load-on-startup>30</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dwr-invoker</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping>
あ!それと、JSで使う場合は、下のようにJSを指定します。
<script type="text/javascritp" src="/<context>/dwr/engine.js"></script> <script type="text/javascript" src="/<context>/dwr/interface/SearchPersonDwr.js"></script>