Tapestry3 与 Spring 集成

 本文来自 Spring In Action
以及在项目中的一些使用

其原理很简单,
在T的global对象中装入 Spring的  ApplicationContext.

所以我们首先要重写写应用的 Engine
因为 .application的 默认  Engine为 BaseEngine为以我们需要
写一个 新的 Engine用来装载 Spring的 WebApplicationContext
我的应用中名字为 IbEngine:
public   class  IbEngine  extends  BaseEngine  {

  
//"appContext"  指 Spring ApplicationContext 在global 这个Map中的Key
    public static final String APPLICATION_CONTEXT_KEY = "appContext";
    
    
    
/**
     * 
@see org.apache.tapestry.engine.AbstractEngine#setupForRequest(org.apache.tapestry.request.RequestContext)
     
*/

   
//在setupForRequest这个方法中装载
    protected void setupForRequest(RequestContext context) {
        
super.setupForRequest(context);

        Map global 
= (Map) getGlobal();
        
        ApplicationContext ac 
= (ApplicationContext) global
                .get(APPLICATION_CONTEXT_KEY);
        
if(ac==null){
        ac 
= WebApplicationContextUtils.getWebApplicationContext(context
                .getServlet().getServletContext());
        global.put(APPLICATION_CONTEXT_KEY, ac);
        }


    }


}

然后在 .application中指定 应用的 Engine对象

< application  name ="ibank"  engine-class ="com.ibank.tapestry.presentation.IbEngine" >


接下来我们在 页面中调用 Spring分发的Bean

Home.Page
< property-specification  name ="InfoService"  type ="com.ibank.spring.services.IbInfoService" >
    global.appContext.getBean("IbInfoService")
</ property-specification >
其中  appContext  为我们在IbEngine中定义的的 ApplicationContext 的Key

其中  global.appContext.getBean("IbInfoService") 中ognl表达
相当于你在Home,java 中调用  this.getGlobal(),get("
appContext. ").getBean(" IbInfoService    
")

如果在 component 中使用 ,有一点不同

Info.jwc
     < property-specification  name ="InfoService"  type ="com.ibank.spring.services.IbInfoService" >
           page.global.appContext.getBean("IbInfoService")
    
</ property-specification >
因为 component被包含在page中
需要调用this.getPage().getGlobal().....

在程序中使用
Home.java
     public   abstract  IbInfoService getInfoService();
    
public   abstract   void  setInfoService(IbInfoService InfoService);
     
      
public  List getInfoList()  {
        infoList
=new ArrayList();
        infoList
=getInfoService().getSimInfoList(1,Integer.parseInt(getInfoMount()));
        
return infoList;
    }

完成了..
:)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值