posts - 495,comments - 227,trackbacks - 0
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">&nbsp;</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 阅读(1077) 评论(0)  编辑  收藏 所属分类: AJAXstruts

只有注册用户登录后才能发表评论。


网站导航: