ITeye博客

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

tapestry · 2008-05-14 22:52 · Tapestry5 · Markdown

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

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

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

在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. @ SuppressWarnings ( " unchecked " )
  8. public   static void contributeAlias (
  9. Configuration < AliasContribution > configuration ,
  10. @ InjectService ( " ContextRootTemplateLocator " )   PageTemplateLocator contextRootTemplateLocator ) {
  11. configuration . add ( AliasContribution . create ( PageTemplateLocator . class ,
  12. contextRootTemplateLocator )) ;
  13. }

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

转载请注明出处tapestry5.com。

评论(3)

  1. #3 楼 · KorbenZhang · 2008-05-27 13:38

    o,是我看的不仔细。 不过我都是放在class相对应的资源包中。

  2. #2 楼 · tapestry · 2008-05-23 00:07

    文章提到的是组件类的模板,页面类默认是在根目录的。

  3. #1 楼 · KorbenZhang · 2008-05-18 14:47

    过时了,T5已经把tml文件放在根目录了。

评论功能维护中,当前仅展示历史评论。