本文转自:http://www.blogjava.net/xylz/
DWR作为Ajax远程调用的服务端得到了很多程序员的追捧,在DWR的2.x版本中已经集成了Guice的插件。
老套了,我们还是定义一个HelloWorld的服务吧,哎,就喜欢HelloWorld,不怕被别人骂!
1 public interface HelloWorld {
2
3 String sayHello();
4
5 Date getSystemDate();
6 }
7
然后写一个简单的实现吧。
1 public class HelloWorldImpl implements HelloWorld {
2
3 @Override
4 public Date getSystemDate() {
5 return new Date();
6 }
7
8 @Override
9 public String sayHello() {
10 return "Hello, guice";
11 }
12 }
13
然后是与dwr有关的东西了,我们写一个dwr的listener来注入我们的模块。
1 package cn.imxylz.study.guice.web.dwr;
2
3 import org.directwebremoting.guice.DwrGuiceServletContextListener;
4
5 /**
6 * @author xylz (www.imxylz.cn)
7 * @version $Rev: 105 $
8 */
9 public class MyDwrGuiceServletContextListener extends DwrGuiceServletContextListener{
10
11 @Override
12 protected void configure() {
13 bindRemotedAs("helloworld", HelloWorld.class).to(HelloWorldImpl.class).asEagerSingleton();
14 }
15 }
16
这里使用bindRemotedAs来将我们的服务开放出来供dwr远程调用。
剩下的就是修改web.xml,需要配置一个dwr的Servlet并且将我们的listener加入其中。看看全部的内容。
1 <?xml version="1.0" encoding="UTF-8"?>
2 <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
4 version="2.5">
5
6 <display-name>guice-dwr</display-name>
7 <description>xylz study project - guice</description>
8
9 <listener>
10 <listener-class>cn.imxylz.study.guice.web.dwr.MyDwrGuiceServletContextListener
11 </listener-class>
12 </listener>
13 <servlet>
14 <servlet-name>dwr-invoker</servlet-name>
15 <servlet-class>org.directwebremoting.guice.DwrGuiceServlet</servlet-class>
16 <init-param>
17 <param-name>debug</param-name>
18 <param-value>true</param-value>
19 </init-param>
20 </servlet>
21 <servlet-mapping>
22 <servlet-name>dwr-invoker</servlet-name>
23 <url-pattern>/dwr/*</url-pattern>
24 </servlet-mapping>
25
26 </web-app>
27
非常简单,也非常简洁,其中DwrGuiceServlet的debug参数只是为了调试方便才开放的,实际中就不用写了。
好了,看看我们的效果。
1 <html>
2 <head><title>dwr - test (www.imxylz.cn) </title>
3 <script type='text/javascript' src='/guice-dwr/dwr/interface/helloworld.js'></script>
4 <script type='text/javascript' src='/guice-dwr/dwr/engine.js'></script>
5 <script type='text/javascript' src='/guice-dwr/dwr/util.js'></script>
6 <script type='text/javascript'>
7 var showHello = function(data){
8 dwr.util.setValue('result',dwr.util.toDescriptiveString(data,1));
9 }
10 var getSystemDate = function(data){
11 dwr.util.setValue('systime',dwr.util.toDescriptiveString(data,2));
12 }
13 </script>
14 <style type='text/css'>
15 input.button { border: 1px outset; margin: 0px; padding: 0px; }
16 span { background: #ffffdd; white-space: pre; padding-left:20px;}
17 </style>
18 </head>
19 <body onload='dwr.util.useLoadingMessage()'>
20 <p>
21 <h2>Guice and DWR</h2>
22 <input class='button' type='button' value="Call HelloWorld 'sayHello' service" onclick="helloworld.sayHello(showHello)" />
23 <span id='result' ></span>
24 </p>
25 <p>
26 <input class='button' type='button' value="Call HelloWorld 'getSystemDate' service" onclick="helloworld.getSystemDate(getSystemDate)" />
27 <span id='systime' ></span>
28 </P>
29 </body>
30 </html>
我们通过两个按钮来获取我们的远程调用的结果。