2005年
Frank W. Zammetti擴展了Struts的Tag庫,使Struts具備了AJAX功能。
????? (注:下面的代碼引用自Frank W. Zammetti 的ajaxtags包)
?
????? Tag庫的擴展
????? 1、FormTag
?????????? 增加了refAjax屬性以及setter和getter方法;
????? 2、BaseHandlerTag
?????????? 增加了refAjax屬性以及setter和getter方法;
?????????? 同時擴展了prepare×××Events方法,使其加載Ajax屬性,如:
?????????? if (getOnkeydown() != null) {
??????????????????......
???????????}
???????????/* Frank W. Zammetti */
???????????else {
?????????????? ???prepareAjax("onkeydown", handlers);
???????????}
???????????/* Frank W. Zammetti */
????? 3、Struts-Html.tld
?????????? 增加了refAjax屬性;
?
??????應用
????? 1、ajax-config.xml
?????????? ......
?????????? <form ajaxRef="example1">
????????????? ??<element ajaxRef="button">
?????????????????? ???<event type="onclick">
??????????????????????????? //發送請求的設置
????????????????????????????<requestHandler type="std:QueryString">
?????????????????????????????????<target>example1.do</target>
?????????????????????????????????<parameter>firstName=firstName,lastName=lastName</parameter>
????????????????????????????</requestHandler>
??????????????????????????? //回調函數(方法)及回寫區域的設置
????????????????????????????<responseHandler type="std:InnerHTML">
??????????????????????????? ?????<parameter>example1_resultLayer</parameter>
????????????????????????????</responseHandler>
??????????????????????</event>
????????????? ??</element>
???????? ? </form>
?????????? ......
??????2、struts-config.xml
?????????? ......
?????????? //與其它Action的區別是:Ajax調用的Action沒有input和forward
?????????? <action path="/example1" name="example1Form" scope="request"
??????????????????????????? ?type="com.omnytex.ajaxtags.Example1Action" />
?????????? ......
?????????? //Ajax Tag Plugin
?????????? <plug-in className="org.apache.struts.taglib.html.ajax.AjaxInit" />
?????????? ......
????? 3、Jsp客戶界面
?????????? ......
?????????? //對應于ajax-config.xml中的form ajaxRef
?????????? <html:form action="example1" ajaxRef="example1">
????????????????......
??????????????? //對應于ajax-config.xml中的Element ajaxRef
??????????????? <html:button property="button" value="Click to do Ajax!" ajaxRef="button" />
?????????? </html:form>
???????????Result:<br>
???????????//對應于ajax-config.xml中的responseHandler
???????????<span id="example1_resultLayer"> </span>
?????????? ......
?????????? //啟用Ajax(應該放在Jsp的最后)
?????????? <html:enableAjax />
?????????? ......
????? 4、Action
?????????? ......
????????? ?public ActionForward execute( ...... )?throws Exception {
??????????????? try {
??????????????????? ......
??????????????????? //Ajax的Action需要輸出內容到out
??????????????????? PrintWriter out = response.getWriter();
???????????????? ???out.println(......);
???????????????? ???out.flush();
???????????????? ???out.close();
????????????????} catch (Exception e) {
??????????????? ??? System.err.println("Exception in Example1Action.execute(): " + e);
????????????????}
??????????????? //由于Ajax的Action輸出的內容由頁面的區域接收,不需要跳轉到其它頁面
??????????????? //所以,返回null
???????????? ?? return null;
???????????}
?????????? ......
?
??????Struts Ajax Tag工作原理
??????1、Struts啟動,加載AjaxInit;
????? 2、AjaxInit解析ajax-config.xml;
????? 3、Struts加載AjaxEnabled的Jsp;
????? 4、Struts-AjaxTag根據Form ajaxRef和elment ajaxRef找到ajax-config的設置;
????? 5、輸出AjaxEnabled的Html(Tag庫加入了Ajax需要的Script,如:XMLHttpRequest);
????? 6、用戶輸入數據,提交數據(異步模式提交后,用戶可以繼續其它操作);
????? 7、XMLHttpRequest對象接管提交數據,并向Struts提交do及參數;
????? 8、Struts ActionServlet加載FormBean,和相應的Action;
????? 9、Struts Action執行相應邏輯,得到返回的數據,通過out輸出;
????? 10、XMLHttpRequest得到Struts返回的數據;
??????11、XMLHttpRequest調用設置的回調函數(方法),輸出數據到指定的區域;
posted on 2006-11-22 14:10
SIMONE 閱讀(1103)
評論(0) 編輯 收藏 所屬分類:
AJAX 、
struts