webwork的interceptor来实现ajax功能(buffalo)
已经很久没有来bolg了,现在有空,就把最近写的一个webwork的拦截器放上来给大家参考,如果有bug或有更好的实现办法,可以email给我。
下面是interceptor的实现代码:
1 package interceptor;
2
3 import java.io.IOException;
4
5 import org.apache.log4j.Logger;
6
7 import com.caucho.burlap.io.BurlapInput;
8 import com.caucho.burlap.io.BurlapOutput;
9 import com.caucho.burlap.server.BurlapSkeleton;
10 import com.opensymphony.webwork.ServletActionContext;
11 import com.opensymphony.xwork.ActionInvocation;
12 import com.opensymphony.xwork.interceptor.Interceptor;
13
14 public class BurlapInterceptor implements Interceptor {
15 private static Logger logger = Logger.getLogger(BurlapInterceptor.class);
16 public void destroy() {
17 // TODO Auto-generated method stub
18
19 }
20
21 public void init() {
22 // TODO Auto-generated method stub
23
24 }
25
26 public String intercept(ActionInvocation invocation) throws Exception {
27 BurlapInput in = new BurlapInput(ServletActionContext.getRequest()
28 .getInputStream());
29
30 BurlapOutput out = new BurlapOutput(ServletActionContext.getResponse().getOutputStream()) {
31 public void startReply() throws IOException {
32 print("<?xml version=\"1.0\" encoding=\"utf-8\"?><burlap:reply xmlns:burlap=\"http://www.buffalo.net/burlap/\">");
33 }
34 };
35
36 BurlapSkeleton _skeleton = new BurlapSkeleton(invocation.getAction());
37
38 try {
39 _skeleton.invoke(in, out);
40 }
41 catch (Throwable e) {
42 logger.error("Could not invoke burlap", e);
43 }
44 return null;
45 }
46
47 }
48
然后在xwork.xml中定义:2
3 import java.io.IOException;
4
5 import org.apache.log4j.Logger;
6
7 import com.caucho.burlap.io.BurlapInput;
8 import com.caucho.burlap.io.BurlapOutput;
9 import com.caucho.burlap.server.BurlapSkeleton;
10 import com.opensymphony.webwork.ServletActionContext;
11 import com.opensymphony.xwork.ActionInvocation;
12 import com.opensymphony.xwork.interceptor.Interceptor;
13
14 public class BurlapInterceptor implements Interceptor {
15 private static Logger logger = Logger.getLogger(BurlapInterceptor.class);
16 public void destroy() {
17 // TODO Auto-generated method stub
18
19 }
20
21 public void init() {
22 // TODO Auto-generated method stub
23
24 }
25
26 public String intercept(ActionInvocation invocation) throws Exception {
27 BurlapInput in = new BurlapInput(ServletActionContext.getRequest()
28 .getInputStream());
29
30 BurlapOutput out = new BurlapOutput(ServletActionContext.getResponse().getOutputStream()) {
31 public void startReply() throws IOException {
32 print("<?xml version=\"1.0\" encoding=\"utf-8\"?><burlap:reply xmlns:burlap=\"http://www.buffalo.net/burlap/\">");
33 }
34 };
35
36 BurlapSkeleton _skeleton = new BurlapSkeleton(invocation.getAction());
37
38 try {
39 _skeleton.invoke(in, out);
40 }
41 catch (Throwable e) {
42 logger.error("Could not invoke burlap", e);
43 }
44 return null;
45 }
46
47 }
48
1 <interceptors>
2 <interceptor name="burlap" class="interceptor.BurlapInterceptor"/>
3 </interceptors>
4
最好就可以使用这个拦截器定义自己的bean了,例如:2 <interceptor name="burlap" class="interceptor.BurlapInterceptor"/>
3 </interceptors>
4
1 <action name="classtree" class="classTreeAction" method="getClassList">
2 <interceptor-ref name="burlap" />
3 </action>
在页面上就可以用如下javascript代码去调用这个action:2 <interceptor-ref name="burlap" />
3 </action>
var bfl = new Buffalo("classtree.page");
bfl.remoteCall("getClassList",[acadyear,semester],function(reply){...});
如果你不了解buffalo,可以去他的网站看看:http://www.amowa.net/buffalo/