webwork的interceptor來實現ajax功能(buffalo)

來源:互聯網
上載者:User

已經很久沒有來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中定義:1         <interceptors>
2               <interceptor name="burlap" class="interceptor.BurlapInterceptor"/>        
3          </interceptors>

最好就可以使用這個攔截器定義自己的bean了,例如:1    <action name="classtree" class="classTreeAction" method="getClassList">
2        <interceptor-ref name="burlap" />
3    </action>

在頁面上就可以用如下javascript代碼去調用這個action:
   var bfl = new Buffalo("classtree.page");
   bfl.remoteCall("getClassList",[acadyear,semester],function(reply){...});

如果你不瞭解buffalo,可以去他的網站看看:http://www.amowa.net/buffalo/

相關文章

Alibaba Cloud 10 Year Anniversary

With You, We are Shaping a Digital World, 2009-2019

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。