欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Tapestry5-如何在根目录下加载组件模板

程序员文章站 2022-03-14 09:01:24
...

      T5默认的组件模板是从WEB-INF目录加载的,实际开发中,希望组件模板可以放在根目录,例如组件类目录package.components.layout.DefaultLayout,对应的模板为layout/DefaultLayout.tml。

      T5的模板加载是通过PageTemplateLocator加载的,我们可以通过覆盖这个Service的实现来达到我们的目的,首先是实现类:

  1. import static java . lang . String . format ;
  2.  
  3. import   java . util . Locale ;
  4.  
  5. import   org . apache . tapestry . internal . InternalConstants ;
  6. import   org . apache . tapestry . internal . services . PageTemplateLocator ;
  7. import   org . apache . tapestry . ioc . Resource ;
  8. import   org . apache . tapestry . model . ComponentModel ;
  9.  
  10. public   class ContextRootTemplateLocator implements PageTemplateLocator {
  11.  
  12.     private   final Resource contextRoot ;
  13.  
  14.     private   final String compomentPackage ;
  15.  
  16.     private   final String pagePackage ;
  17.  
  18.     public   ContextRootTemplateLocator ( Resource contextRoot ,
  19.             String   appRootPackage ) {
  20.         this . contextRoot = contextRoot ;
  21.         this . compomentPackage = appRootPackage + " . "
  22.                 + InternalConstants . COMPONENTS_SUBPACKAGE ;
  23.         this . pagePackage = appRootPackage + " . "
  24.                 + InternalConstants . PAGES_SUBPACKAGE ;
  25.  
  26.     }
  27.  
  28.     public   Resource findPageTemplateResource ( ComponentModel model , Locale locale ) {
  29.  
  30.         String   className = model . getComponentClassName () ;
  31.         if   ( className . contains ( pagePackage )) {
  32.             return   findPageTemplateResourceDelegate ( className , locale ) ;
  33.         }
  34.         if   ( className . contains ( compomentPackage )) {
  35.             return   findComponentTemplateResourceDelegate ( className , locale ) ;
  36.         }
  37.         return   null ;
  38.     }
  39.  
  40.     private   Resource findPageTemplateResourceDelegate ( String className ,
  41.             Locale   locale ) {
  42.         String   logicalName = className . substring ( pagePackage . length () + 1 )
  43.                 . replace ( ' . ' , ' / ' ) ;
  44.         return   locateFile ( logicalName , locale ) ;
  45.     }
  46.  
  47.     private   Resource findComponentTemplateResourceDelegate ( String className ,
  48.             Locale   locale ) {
  49.         String   logicalName = className . substring ( compomentPackage . length () + 1 )
  50.                 . replace ( ' . ' , ' / ' ) ;
  51.         return   locateFile ( logicalName , locale ) ;
  52.     }
  53.  
  54.     private   Resource locateFile ( String logicalName , Locale locale ) {
  55.         String   path = format ( " %s.%s " , logicalName ,
  56.                 InternalConstants . TEMPLATE_EXTENSION ) ;
  57.         return   contextRoot . forFile ( path ) . forLocale ( locale ) ;
  58.     }
  59.  
  60. }

在module中设置覆盖掉默认的PageTemplateLocator实现

  1. public PageTemplateLocator buildContextRootTemplateLocator (
  2.             @ InjectService ( " ContextAssetFactory " )   AssetFactory contextAssetFactory ,
  3.             @ Inject @ Symbol ( InternalConstants . TAPESTRY_APP_PACKAGE_PARAM )   String appRootPackage ) {
  4.         return   new ContextRootTemplateLocator ( contextAssetFactory
  5.                 . getRootResource () , appRootPackage ) ;
  6.     }
  7.  
  8.     @ SuppressWarnings ( " unchecked " )
  9.     public   static void contributeAlias (
  10.             Configuration < AliasContribution > configuration ,
  11.             @ InjectService ( " ContextRootTemplateLocator " )   PageTemplateLocator contextRootTemplateLocator ) {
  12.         configuration . add ( AliasContribution . create ( PageTemplateLocator . class ,
  13.                 contextRootTemplateLocator )) ;
  14.     }

这样就可以在根目录下加载组件类的模板了。

转载请注明出处tapestry5.com。