DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> AJAX入門 >> AJAX詳解 >> 構建支持Ajax的輕量級J2EE框架
構建支持Ajax的輕量級J2EE框架
編輯:AJAX詳解     
Buffalo處理AJax有多牛,嘿嘿 我還真沒有正兒八經的整過。慚愧啊,爭取在最近好好研究下吧。
Buffalo支持和Spring整合。嘿嘿 這也是一個亮點。亮的有些不自在。為啥?
假如你用Spring+Struts+Hibernate來構建的輕量級J2EE框架,Spring和Struts整合有好幾種方式,有一種方式不要要論論了。
   <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
    <set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml,/WEB-INF/business-context.XML" />
  </plug-in>
  上面的代碼還熟悉吧。問題就在這了,Buffalo 支持Spring的是
  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>
           /WEB-INF/applicationContext.XML
      </param-value>
 </context-param>
 <servlet>
    <servlet-name>context</servlet-name>
    <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
 </servlet>
上面的部分代碼是Buffalo支持Spring必須的。
如果Spring和Struts整合用PlugIn方式,也就是Spring來管理Action(管理Action好壞先不說).Buffalo和Spring整合再在Web.XML配置SPring的東東,總感覺怪怪的。
Buffalo獲取WebApplicationContext是通過WebApplicationContextUtils.getWebApplicationContext(context);來獲取的。
感覺浪費。。。。。。
所以想讓Buffalo用Spring放在ServletContext裡面的WebApplicationContext(ContextLoaderPlugIn 實現把WebApplicationContext的實例放在了ServletContext中)。
所以就在Buffalo中實現了一個BuffaloPlugIn。代碼如下:(寫的不規范,湊合看吧)
。。。。
/**
 *
 * @Descripted <B>Buffalo+Spring+Struts+Hibernate/B>
 *             <P></P>
 * @author meconsea
 * @Company 
 * @IM:   MSN: [email protected]
 *        QQ:  75147664
 *        E-mail: [email protected]
 * @date 2006-11-16
 * @Version 1.0Beta
 */
public class BuffaloPlugIn implements PlugIn {
       
        public static final Logger log = Logger.getLogger(BuffaloPlugIn.class);
       
        public static final String SERVLET_CONTEXT_PREFIX = ContextLoaderPlugIn.class.getName() + ".CONTEXT.";
        public static final String BUFFALO_CONTEXT_PREFIX = BuffaloPlugIn.class.getName()+".CONTEXT.";
       
        private ModuleConfig moduleConfig;
        private ActionServlet actionServlet;
        public void destroy() {
                // TODO Auto-generated method stub

        }
       
        protected final String getModuleConfigPrefix(){
                return this.moduleConfig.getPrefix();
        }
        protected final ActionServlet getActionServlet(){
                return this.actionServlet;
        }
       
        public String getServletContextAttributeNameForBuffalo(){
                return BUFFALO_CONTEXT_PREFIX;
        }
        public String getServletContextAttributeNameForSCP(){
                return SERVLET_CONTEXT_PREFIX+getModuleConfigPrefix();
        }

        public void init(ActionServlet as, ModuleConfig mc)
                        throws ServletException {
                // TODO Auto-generated method stub
                log.info("BuffaloPlugIn init begin ......");
                this.actionServlet = as;
                this.moduleConfig = mc;
                as.getServletContext().setAttribute(getServletContextAttributeNameForBuffalo(),getServletContextAttributeNameForSCP());
                log.info("init  end ");
        }
}

把Buffalo中的SpringFactory和SpringUtil的獲得WebApplicationContext的方式修改如下:
在SpringUtil中增加方法:
/**
         * 獲取WebApplicationContext
         * @param context
         * @return
         */
        public static WebApplicationContext getWebApplicationContext(ServletContext context){
                WebApplicationContext wac = null;
                log.info("獲取Wac begin......");
               
                if(context == null){
                        log.info("context is null  reutrn null");
                        return null;
                }
                String wacAttrName = (String)context.getAttribute(BuffaloPlugIn.BUFFALO_CONTEXT_PREFIX);
                log.info("wacAttrName == "+wacAttrName);
               
                if(wacAttrName != null){
                        wac = (WebApplicationContext)context.getAttribute(wacAttrName);
                }
               
                if(wac == null){
                        log.info("wac is null , obtain WebApplicationContext by WebApplicationContextUtils again");
                        wac = WebApplicationContextUtils.getWebApplicationContext(context);
                }
                return wac;
        }

....//
WebApplicationContext appCtx = getWebApplicationContext(context);//WebApplicationContextUtils.getWebApplicationContext(context);
好了,修改完畢了。
在Struts-config.XML中增加
<plug-in className="net.buffalo.service.spring.BuffaloPlugIn" />
就OK了。Web.XML中只配置
<servlet>
                <servlet-name>bfapp</servlet-name>
                <servlet-class>net.buffalo.web.servlet.ApplicationServlet</servlet-class>
  </servlet>
就完全支持SPring了。
當然是建立在Spring+Struts構建了輕量級框架的基礎上的。

哈哈 不知道我自己能不能看懂。就此擱筆! 閃!!!!!
給自己留的紀念。
 注: Spring2.0 Struts1.3 Hibernate3.0

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved