---
title: "Tapestry5-如何在根目录下加载组件模板"
author: "tapestry"
date: "2008-05-14T22:52:31Z"
tags: []
slug: "tapestry5"
draft: false
lastmod: "2008-05-14T22:52:31Z"
id: 193114
url: "https://tapestry.iteye.com/blog/193114"
---

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

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

01. import static java . lang . String . format ;
02. import   java . util . Locale ;
03. import   org . apache . tapestry . internal . InternalConstants ;
04. import   org . apache . tapestry . internal . services . PageTemplateLocator ;
05. import   org . apache . tapestry . ioc . Resource ;
06. import   org . apache . tapestry . model . ComponentModel ;
07. public   class ContextRootTemplateLocator implements PageTemplateLocator {
08. private   final Resource contextRoot ;
09. 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实现

01. public PageTemplateLocator buildContextRootTemplateLocator (
02. @ InjectService ( " ContextAssetFactory " )   AssetFactory contextAssetFactory ,
03. @ Inject @ Symbol ( InternalConstants . TAPESTRY\_APP\_PACKAGE\_PARAM )   String appRootPackage ) {
04. return   new ContextRootTemplateLocator ( contextAssetFactory
05. . getRootResource () , appRootPackage ) ;
06. }
07. @ SuppressWarnings ( " unchecked " )
08. public   static void contributeAlias (
09. Configuration &lt; AliasContribution &gt; configuration ,
10. @ InjectService ( " ContextRootTemplateLocator " )   PageTemplateLocator contextRootTemplateLocator ) {
11. configuration . add ( AliasContribution . create ( PageTemplateLocator . class ,
12. contextRootTemplateLocator )) ;
13. }

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

转载请注明出处tapestry5.com。