频道直达 - 专题 - 新闻 - 技巧 - 组网 - 开发 - 安全 - web编程 - 图像 - 操作系统 - 数据库 - 教育 - 旅游 - 健康 - 时尚 - 驱动 - 软件 - 游戏 - 多媒体 - ERP - 讨论组

构建支持Ajax的轻量级J2EE框架

来源: 作者: 出处:巧巧读书 2006-11-22 进入讨论组

话说Buffalo是姓中的写的,支持下国产! 来先顶一个。
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: 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中增加方法:
/**
         * 获取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

文字:http://www.qqread.com/j2ee/d280281.html 更多文章 更多内容请看Ajax技术专题Ajax技术应用开发Ajax框架与实例专题,或进入讨论组讨论。
收藏此文】【 】【打印】【关闭
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
巧巧读书宗旨
相关专题
最新论坛文章
站内各频道最新更新文档
站内最新制作专题
热门关键字导读
Photoshop教 程照片处理 照片制作 PS快捷键 抠图
计 算 机 故 障XP系统修复
艺 术 与 设 计设计 流媒体 设计欣赏 边框
计 算 机 安 全ARP
站内频道文章精选
巧巧电脑频道编辑信箱  告诉我们您想看的专题或文章