Tapestry5-如何在根目录下加载组件模板
程序员文章站
2022-03-14 08:57:24
...
T5默认的组件模板是从WEB-INF目录加载的,实际开发中,希望组件模板可以放在根目录,例如组件类目录package.components.layout.DefaultLayout,对应的模板为layout/DefaultLayout.tml。
T5的模板加载是通过PageTemplateLocator加载的,我们可以通过覆盖这个Service的实现来达到我们的目的,首先是实现类:
- import static java . lang . String . format ;
- import java . util . Locale ;
- import org . apache . tapestry . internal . InternalConstants ;
- import org . apache . tapestry . internal . services . PageTemplateLocator ;
- import org . apache . tapestry . ioc . Resource ;
- import org . apache . tapestry . model . ComponentModel ;
- public class ContextRootTemplateLocator implements PageTemplateLocator {
- private final Resource contextRoot ;
- private final String compomentPackage ;
- private final String pagePackage ;
- public ContextRootTemplateLocator ( Resource contextRoot ,
- String appRootPackage ) {
- this . contextRoot = contextRoot ;
- this . compomentPackage = appRootPackage + " . "
- + InternalConstants . COMPONENTS_SUBPACKAGE ;
- this . pagePackage = appRootPackage + " . "
- + InternalConstants . PAGES_SUBPACKAGE ;
- }
- public Resource findPageTemplateResource ( ComponentModel model , Locale locale ) {
- String className = model . getComponentClassName () ;
- if ( className . contains ( pagePackage )) {
- return findPageTemplateResourceDelegate ( className , locale ) ;
- }
- if ( className . contains ( compomentPackage )) {
- return findComponentTemplateResourceDelegate ( className , locale ) ;
- }
- return null ;
- }
- private Resource findPageTemplateResourceDelegate ( String className ,
- Locale locale ) {
- String logicalName = className . substring ( pagePackage . length () + 1 )
- . replace ( ' . ' , ' / ' ) ;
- return locateFile ( logicalName , locale ) ;
- }
- private Resource findComponentTemplateResourceDelegate ( String className ,
- Locale locale ) {
- String logicalName = className . substring ( compomentPackage . length () + 1 )
- . replace ( ' . ' , ' / ' ) ;
- return locateFile ( logicalName , locale ) ;
- }
- private Resource locateFile ( String logicalName , Locale locale ) {
- String path = format ( " %s.%s " , logicalName ,
- InternalConstants . TEMPLATE_EXTENSION ) ;
- return contextRoot . forFile ( path ) . forLocale ( locale ) ;
- }
- }
在module中设置覆盖掉默认的PageTemplateLocator实现
- public PageTemplateLocator buildContextRootTemplateLocator (
- @ InjectService ( " ContextAssetFactory " ) AssetFactory contextAssetFactory ,
- @ Inject @ Symbol ( InternalConstants . TAPESTRY_APP_PACKAGE_PARAM ) String appRootPackage ) {
- return new ContextRootTemplateLocator ( contextAssetFactory
- . getRootResource () , appRootPackage ) ;
- }
- @ SuppressWarnings ( " unchecked " )
- public static void contributeAlias (
- Configuration < AliasContribution > configuration ,
- @ InjectService ( " ContextRootTemplateLocator " ) PageTemplateLocator contextRootTemplateLocator ) {
- configuration . add ( AliasContribution . create ( PageTemplateLocator . class ,
- contextRootTemplateLocator )) ;
- }
这样就可以在根目录下加载组件类的模板了。
转载请注明出处tapestry5.com。
下一篇: OkHttp的缓存