ajax|j2ee
話說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