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。
分享到:
相关推荐
Tapestry5和jQuery集成.使用jQuery以极少的兼容问题完全替换Prototype 和 Scriptaculous库 标签:tapestry5
tapestry-bin-5.1.0.5
Tapestry in action 作者最新作品,包含Tapestry 5.2
tapestry-project-4.1.5 裏面有例子和所用到的包
tapestry-spring-5.2.6.jar,java spring框架依赖,可以直接使用
apache-tapestry-5.3.8-bin.zip
Tapestry-xiti库通过加载适当的脚本来简化与Web分析框架的集成。 通过提供可以在链接(动作链接,事件链接,页面链接)和表单上声明的混合包,它还使您可以轻松地使用Xiti API。 如何使用它 该模块提供了一种添加...
Tapestry5最新中文教程.doc 作者 Renat Zubairov & Igor Drobiazko译者 沙晓兰 发布于 2008年7月2日 下午9时30分 社区 Java 主题 Web框架 ----------------------------------------- Tapestry5.1实例教程.pdf ...
很久以前-在1995年-Web对象框架( )由史蒂夫·乔布斯(Steve Jobs)领导的Next公司创立。 Web对象的基本部分是企业对象框架(EOF)( )。 Apache Tapestry的设计原理与Web对象非常相似,Apache Cayenne的设计原理...
一个web框架的IOC实现,Apache tapestry-ioc
Tapestry-3.0的API和组件,详细的使用手册,网页形式查询方便。
包含: tapestry 的源代码, tapestry集成spring2.0 tapestry快速启动 tapestry upload tapestry hibernate tapestry annotations
tapestry-monitoring.zip,Tapestry监视为Tapestry5服务提供性能监视。@通过JMX监视页面/组件和服务接口方法
tapestry-4.0.2.jar
tapestry4.02中封装ext的GridPanel组件
Tapestry是一个开源的基于servlet的应用程序框架,它使用组件对象模型来创建动态的,交互的web应用。
自定义的邮件组件!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Tapestry是一项基于组件的web展现层技术,这里将它划分成业务工程HelloTapestry及Component工程源码两项.实际项目中,Component可以成为不同项目中抽离出来的通用的模块,与具体的业务脱离。
Tapestry-3-Getting Started.doc 中文版
这个是开发tapestry3所用的JAR包