|
|
Tapestry版本:3.0
参考文档:http://www.behindthesite.com/blog/C1931765677/E381917869/index.html
问题讨论链接:http://www.javaresearch.org/forum/thread.jsp?column=17&thread=16429
问题的提出:
默认Tapestry的页面模板文件(.html)及其对应的规范文件(.page),可以放在web根目录或其下的WEB-INF/目录中,Tapestry可以不用任何配置(在以前的版本中需要在.application文件中定义一下页面)就能正确运行,如果你需要在这些根目录下以文件系统的目录形式组织你的页面,则Tapestry是不会自动搜索这些子目录的,你必须在.application文件中定义这些不在默认位置的页面。这样一来,每加一个页面都需要定义一下,如果页面数目众多,定义起来就会比较麻烦,如何才能避免这些配置呢?本文的目的就是尝试解决这个问题。
问题的解决:
本文是在参考文档(http://www.behindthesite.com/blog/C1931765677/E381917869/index.html)源代码的基础上稍作修改而成,主要是为了解决不能在Tomcat中使用的问题。为了更好的了解,朋友们最好能阅读一下原文和原来的代码。主要修改的地方是给RecursiveFileLocator传递一个真实路径地址,以便能够列出目录下面的子目录,从而实现层次查找。 解决的途径就是定义一个ISpecificationResolverDelegate,以便Tapestry在常规路径下找不到文件时进行处理。 CustomSpecificationResolver.java:
-
- // CustomSpecificationResolver.java
- //
- // Copyright 2004 Michael J. Henderson & Associates LLC
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
-
- package com.mjhenderson.users.tapestry;
-
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import org.apache.tapestry.INamespace;
- import org.apache.tapestry.IRequestCycle;
- import org.apache.tapestry.IResourceLocation;
- import org.apache.tapestry.Tapestry;
- import org.apache.tapestry.engine.ISpecificationSource;
- import org.apache.tapestry.resolver.ISpecificationResolverDelegate;
- import org.apache.tapestry.spec.IComponentSpecification;
-
- /**
- * @author <a href="mailto:michaelh@mjhenderson.com?subject=com.mjhenderson.users.tapestry.CustomSpecificationResolver">Mike Henderson</a>
- *
- */
- public class CustomSpecificationResolver implements
- ISpecificationResolverDelegate {
-
-
- private static final Log LOG = LogFactory.getLog(RecursiveFileLocator.class);
-
- private ISpecificationSource _specificationSource;
-
- private RecursiveFileLocator _locator;
- //private boolean _applicationIsExplodedWAR;
- private boolean _initialized;
- private String _folder;
- private String _realRootFolder;
-
- public CustomSpecificationResolver() {;
- }
-
- public void setFolder(String value) {
- _folder = value;
- }
-
- public String getFolder() {
- return _folder;
- }
-
- private void _init(IRequestCycle cycle) {
- //IResourceLocation rootLocation = Tapestry.getApplicationRootLocation(cycle).getRelativeLocation("/WEB-INF/");
- IResourceLocation rootLocation = Tapestry.getApplicationRootLocation(cycle).getRelativeLocation("/");
- //_applicationIsExplodedWAR = rootLocation.getResourceURL().toString().startsWith("file:");
- //if (_applicationIsExplodedWAR) {
- _realRootFolder = cycle.getRequestContext().getServlet().getServletContext().getRealPath("/");
- _locator = new RecursiveFileLocator(rootLocation,_realRootFolder);
- _specificationSource = cycle.getEngine().getSpecificationSource();
- //}
- _initialized = true;
- }
-
- // private boolean checkLocationIsFileLocation(IResourceLocation location) {
- // String url = location.getResourceURL().toString();
- // System.out.println("url = "+url);
- // return url.startsWith("file:");
- // }
-
- /**
- * @see org.apache.tapestry.resolver.ISpecificationResolverDelegate#findPageSpecification(org.apache.tapestry.IRequestCycle, org.apache.tapestry.INamespace, java.lang.String)
- */
- public IComponentSpecification findPageSpecification(IRequestCycle cycle,
- INamespace namespace, String name) {
-
- if (!_initialized) {
- _init(cycle);
- }
- //if (!_applicationIsExplodedWAR) {
- // return null;
- //}
- IResourceLocation location = _locator.resolveLocation(name+".page");
- if (location != null) {
- return _specificationSource.getPageSpecification(location);
- }
- return null;
- }
-
- /**
- * @see org.apache.tapestry.resolver.ISpecificationResolverDelegate#findComponentSpecification(org.apache.tapestry.IRequestCycle, org.apache.tapestry.INamespace, java.lang.String)
- */
- public IComponentSpecification findComponentSpecification(
- IRequestCycle cycle, INamespace namespace, String type) {
-
- if (!_initialized) {
- _init(cycle);
- }
- //if (!_applicationIsExplodedWAR) {
- // return null;
- //}
- IResourceLocation location = _locator.resolveLocation(type+".jwc");
- if (location != null) {
- return _specificationSource.getComponentSpecification(location);
- }
- return null;
- }
-
- }
-
RecursiveFileLocator.java:
.application文件中增加如下定义:
-
- <extension name="org.apache.tapestry.specification-resolver-delegate"
- class="com.mjhenderson.users.tapestry.CustomSpecificationResolver"
- immediate="yes">
- </extension>
-
以上代码已经在Tomcat和JBoss上经过测试,希望朋友们可以在别的应用服务器上帮我测测,呵呵。代码比较粗糙,谁要是发现了什么问题或有什么改进思路,希望能联系我:sky_yjck@hotmail.com。
顺便提醒一下,在http://www.t-deli.com上有一个Ant Task,可以根据你的页面存放路径自动更新.application文件里面的页面定义,这样似乎定义几百个页面也是很简单的事情,不过我还是倾向于不用配置直接使用的方法,就看你的需要和使用习惯了。
|
|