DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> JavaScript入門知識 >> AJAX入門 >> AJAX詳解
建構支援 Ajax 的輕量級 J2EE 框架(Buffalo 整合 Spring)
編輯:AJAX詳解     

本文記錄如何讓 Buffalo 這個 Ajax 框架與 Spring、Struts、Hibernate 整合,並解決 Spring 管理 Action 時的 WebApplicationContext 取得問題。

如果你用 Spring + Struts + Hibernate 建構輕量級 J2EE 框架,Spring 與 Struts 的整合方式有好幾種,其中一種是透過 Struts 的 plug-in 由 Spring 來管理 Action:

<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 的方式,是要求在 web.xml 中透過 context-param 與 ContextLoaderServlet 來載入 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>

若 Spring 與 Struts 用 PlugIn 方式整合(也就是由 Spring 管理 Action),卻又在 web.xml 另外配置一次 Spring,總感覺彆扭。於是作者實作了一個 BuffaloPlugIn,讓 Buffalo 能取得由 ContextLoaderPlugIn 放在 ServletContext 中的 WebApplicationContext。核心程式如下:

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;
  protected final String getModuleConfigPrefix(){ return this.moduleConfig.getPrefix(); }
  public String getServletContextAttributeNameForBuffalo(){ return BUFFALO_CONTEXT_PREFIX; }
  public String getServletContextAttributeNameForSCP(){ return SERVLET_CONTEXT_PREFIX+getModuleConfigPrefix(); }
  public void init(ActionServlet as, ModuleConfig mc) throws ServletException {
   log.info("BuffaloPlugIn init begin ......");
   this.actionServlet = as;
   this.moduleConfig = mc;
   as.getServletContext().setAttribute(getServletContextAttributeNameForBuffalo(),getServletContextAttributeNameForSCP());
   log.info("init  end ");
  }
}

接著修改 Buffalo 中 SpringUtil 取得 WebApplicationContext 的方式,優先從 BuffaloPlugIn 設定的屬性名稱取出 wac,取不到時再退回用 WebApplicationContextUtils 取得:

public static WebApplicationContext getWebApplicationContext(ServletContext context){
  WebApplicationContext wac = null;
  if(context == null){ return null; }
  String wacAttrName = (String)context.getAttribute(BuffaloPlugIn.BUFFALO_CONTEXT_PREFIX);
  if(wacAttrName != null){
   wac = (WebApplicationContext)context.getAttribute(wacAttrName);
  }
  if(wac == null){
   wac = WebApplicationContextUtils.getWebApplicationContext(context);
  }
  return wac;
}

最後在 struts-config.xml 中加入:<plug-in className="net.buffalo.service.spring.BuffaloPlugIn" />,web.xml 中只需配置 Buffalo 的 ApplicationServlet 即可完全支援 Spring。此方案基於 Spring 2.0、Struts 1.3、Hibernate 3.0。

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