posts - 495,comments - 227,trackbacks - 0
          2005年Frank W. Zammetti擴(kuò)展了Struts的Tag庫(kù),使Struts具備了AJAX功能。
          ????? (注:下面的代碼引用自Frank W. Zammetti 的ajaxtags包)
          ?
          ????? Tag庫(kù)的擴(kuò)展
          ????? 1、FormTag
          ?????????? 增加了refAjax屬性以及setter和getter方法;
          ????? 2、BaseHandlerTag
          ?????????? 增加了refAjax屬性以及setter和getter方法;
          ?????????? 同時(shí)擴(kuò)展了prepare×××Events方法,使其加載Ajax屬性,如:
          ?????????? if (getOnkeydown() != null) {
          ??????????????????......
          ???????????}
          ???????????/* Frank W. Zammetti */
          ???????????else {
          ?????????????? ???prepareAjax("onkeydown", handlers);
          ???????????}
          ???????????/* Frank W. Zammetti */
          ????? 3、Struts-Html.tld
          ?????????? 增加了refAjax屬性;
          ?
          ??????應(yīng)用
          ????? 1、ajax-config.xml
          ?????????? ......
          ?????????? <form ajaxRef="example1">
          ????????????? ??<element ajaxRef="button">
          ?????????????????? ???<event type="onclick">
          ??????????????????????????? //發(fā)送請(qǐng)求的設(shè)置
          ????????????????????????????<requestHandler type="std:QueryString">
          ?????????????????????????????????<target>example1.do</target>
          ?????????????????????????????????<parameter>firstName=firstName,lastName=lastName</parameter>
          ????????????????????????????</requestHandler>
          ??????????????????????????? //回調(diào)函數(shù)(方法)及回寫(xiě)區(qū)域的設(shè)置
          ????????????????????????????<responseHandler type="std:InnerHTML">
          ??????????????????????????? ?????<parameter>example1_resultLayer</parameter>
          ????????????????????????????</responseHandler>
          ??????????????????????</event>
          ????????????? ??</element>
          ???????? ? </form>
          ?????????? ......
          ??????2、struts-config.xml
          ?????????? ......
          ?????????? //與其它Action的區(qū)別是:Ajax調(diào)用的Action沒(méi)有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客戶界面
          ?????????? ......
          ?????????? //對(duì)應(yīng)于ajax-config.xml中的form ajaxRef
          ?????????? <html:form action="example1" ajaxRef="example1">
          ????????????????......
          ??????????????? //對(duì)應(yīng)于ajax-config.xml中的Element ajaxRef
          ??????????????? <html:button property="button" value="Click to do Ajax!" ajaxRef="button" />
          ?????????? </html:form>
          ???????????Result:<br>
          ???????????//對(duì)應(yīng)于ajax-config.xml中的responseHandler
          ???????????<span id="example1_resultLayer">&nbsp;</span>
          ?????????? ......
          ?????????? //啟用Ajax(應(yīng)該放在Jsp的最后)
          ?????????? <html:enableAjax />
          ?????????? ......
          ????? 4、Action
          ?????????? ......
          ????????? ?public ActionForward execute( ...... )?throws Exception {
          ??????????????? try {
          ??????????????????? ......
          ??????????????????? //Ajax的Action需要輸出內(nèi)容到out
          ??????????????????? PrintWriter out = response.getWriter();
          ???????????????? ???out.println(......);
          ???????????????? ???out.flush();
          ???????????????? ???out.close();
          ????????????????} catch (Exception e) {
          ??????????????? ??? System.err.println("Exception in Example1Action.execute(): " + e);
          ????????????????}
          ??????????????? //由于Ajax的Action輸出的內(nèi)容由頁(yè)面的區(qū)域接收,不需要跳轉(zhuǎn)到其它頁(yè)面
          ??????????????? //所以,返回null
          ???????????? ?? return null;
          ???????????}
          ?????????? ......
          ?
          ??????Struts Ajax Tag工作原理
          ??????1、Struts啟動(dòng),加載AjaxInit;
          ????? 2、AjaxInit解析ajax-config.xml;
          ????? 3、Struts加載AjaxEnabled的Jsp;
          ????? 4、Struts-AjaxTag根據(jù)Form ajaxRef和elment ajaxRef找到ajax-config的設(shè)置;
          ????? 5、輸出AjaxEnabled的Html(Tag庫(kù)加入了Ajax需要的Script,如:XMLHttpRequest);
          ????? 6、用戶輸入數(shù)據(jù),提交數(shù)據(jù)(異步模式提交后,用戶可以繼續(xù)其它操作);
          ????? 7、XMLHttpRequest對(duì)象接管提交數(shù)據(jù),并向Struts提交do及參數(shù);
          ????? 8、Struts ActionServlet加載FormBean,和相應(yīng)的Action;
          ????? 9、Struts Action執(zhí)行相應(yīng)邏輯,得到返回的數(shù)據(jù),通過(guò)out輸出;
          ????? 10、XMLHttpRequest得到Struts返回的數(shù)據(jù);
          ??????11、XMLHttpRequest調(diào)用設(shè)置的回調(diào)函數(shù)(方法),輸出數(shù)據(jù)到指定的區(qū)域;
          posted on 2006-11-22 14:10 SIMONE 閱讀(1111) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): AJAXstruts
          主站蜘蛛池模板: 南岸区| 潢川县| 河北区| 四平市| 哈密市| 庆安县| 许昌市| 太原市| 莒南县| 敖汉旗| 平阳县| 米易县| 永平县| 定远县| 容城县| 南投县| 丹东市| 临漳县| 昌图县| 囊谦县| 石阡县| 亳州市| 承德县| 江北区| 虹口区| 奎屯市| 盐源县| 清水河县| 陆丰市| 新巴尔虎左旗| 正蓝旗| 万宁市| 新平| 河源市| 乌拉特中旗| 苏尼特左旗| 商河县| 天峨县| 江安县| 湟源县| 韩城市|