構(gòu)建支持Ajax的輕量級(jí)J2EE框架

字號(hào):

話說Buffalo是姓中的寫的,支持下國(guó)產(chǎn)! 來先頂一個(gè)。
    Buffalo處理Ajax有多牛,嘿嘿 我還真沒有正兒八經(jīng)的整過。慚愧啊,爭(zhēng)取在最近好好研究下吧。
    Buffalo支持和Spring整合。嘿嘿 這也是一個(gè)亮點(diǎn)。亮的有些不自在。為啥?假如你用  Spring+Struts+Hibernate來構(gòu)建的輕量級(jí)J2EE框架,Spring和Struts整合有好幾種方式,有一種方式不要要論論了。
       
       
       

        上面的代碼還熟悉吧。問題就在這了,Buffalo 支持Spring的是
       
        contextConfigLocation
       
        /WEB-INF/applicationContext.xml
       

    

       
     context
        org.springframework.web.context.ContextLoaderServlet
        1
       

    上面的部分代碼是Buffalo支持Spring必須的。如果Spring和Struts整合用PlugIn方式,也就是Spring來管理Action(管理Action好壞先不說).Buffalo和Spring整合再在Web.xml配置SPring的東東,總感覺怪怪的。Buffalo獲取WebApplicationContext是通過  WebApplicationContextUtils.getWebApplicationContext(context);來獲取的。感覺浪費(fèi)。。。。。。所以想讓Buffalo用Spring放在ServletContext里面的WebApplicationContext  (ContextLoaderPlugIn 實(shí)現(xiàn)把WebApplicationContext的實(shí)例放在了ServletContext中)。所以就在Buffalo中實(shí)現(xiàn)了一個(gè)BuffaloPlugIn。代碼如下:(寫的不規(guī)范,湊合看吧)
    。。。。
    /**
     *
     * @Descripted Buffalo+Spring+Struts+Hibernate/B>
     *
       * @author meconsea
     * @Company
       * @IM: MSN: meconsea@hotmail.com
       * QQ: 75147664
       * E-mail: meconsea@163.com
       * @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中增加方法:
    /**
     * 獲?。祝澹猓粒穑穑欤椋悖幔簦椋铮睿茫铮睿簦澹?BR>     * @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中增加就OK了。Web.xml中只配置
    
        bfapp
        net.buffalo.web.servlet.ApplicationServlet
       

    就完全支持SPring了。當(dāng)然是建立在Spring+Struts構(gòu)建了輕量級(jí)框架的基礎(chǔ)上的。哈哈 不知道我自己能不能看懂。就此擱筆! 閃?。。。?!給自己留的紀(jì)念。