中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > Java > Struts/Hibernate
如何自由组织Tapestry页面规范文件
作者:未知 时间:2005-07-24 21:22 出处:JR 责编:chinaitpower
              摘要:如何自由组织Tapestry页面规范文件
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:
  1. // CustomSpecificationResolver.java
  2. //
  3. // Copyright 2004 Michael J. Henderson & Associates LLC
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. //     http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. package com.mjhenderson.users.tapestry;
  17. import org.apache.commons.logging.Log;
  18. import org.apache.commons.logging.LogFactory;
  19. import org.apache.tapestry.INamespace;
  20. import org.apache.tapestry.IRequestCycle;
  21. import org.apache.tapestry.IResourceLocation;
  22. import org.apache.tapestry.Tapestry;
  23. import org.apache.tapestry.engine.ISpecificationSource;
  24. import org.apache.tapestry.resolver.ISpecificationResolverDelegate;
  25. import org.apache.tapestry.spec.IComponentSpecification;
  26. /**
  27.  * @author <a href="mailto:michaelh@mjhenderson.com?subject=com.mjhenderson.users.tapestry.CustomSpecificationResolver">Mike Henderson</a>
  28.  *
  29.  */
  30. public class CustomSpecificationResolver implements
  31.         ISpecificationResolverDelegate {
  32.     
  33.     private static final Log LOG = LogFactory.getLog(RecursiveFileLocator.class);
  34.     private ISpecificationSource _specificationSource;
  35.     private RecursiveFileLocator _locator;
  36.     //private boolean _applicationIsExplodedWAR;
  37.     private boolean _initialized;
  38.     private String _folder;
  39.     private String _realRootFolder;
  40.     
  41.     public CustomSpecificationResolver() {;
  42.     }
  43.     
  44.     public void setFolder(String value) {
  45.         _folder = value;
  46.     }
  47.     
  48.     public String getFolder() {
  49.         return _folder;
  50.     }
  51.     
  52.     private void _init(IRequestCycle cycle) {
  53.             //IResourceLocation rootLocation = Tapestry.getApplicationRootLocation(cycle).getRelativeLocation("/WEB-INF/");
  54.         IResourceLocation rootLocation = Tapestry.getApplicationRootLocation(cycle).getRelativeLocation("/");
  55.         //_applicationIsExplodedWAR = rootLocation.getResourceURL().toString().startsWith("file:");
  56.         //if (_applicationIsExplodedWAR) {
  57.         _realRootFolder = cycle.getRequestContext().getServlet().getServletContext().getRealPath("/");
  58.         _locator = new RecursiveFileLocator(rootLocation,_realRootFolder);
  59.         _specificationSource = cycle.getEngine().getSpecificationSource();
  60.         //}
  61.         _initialized = true;
  62.     }
  63.     
  64. //    private boolean checkLocationIsFileLocation(IResourceLocation location) {
  65. //        String url = location.getResourceURL().toString();
  66. //        System.out.println("url = "+url);
  67. //        return url.startsWith("file:");
  68. //    }
  69.     
  70.     /**
  71.      * @see org.apache.tapestry.resolver.ISpecificationResolverDelegate#findPageSpecification(org.apache.tapestry.IRequestCycle, org.apache.tapestry.INamespace, java.lang.String)
  72.      */
  73.     public IComponentSpecification findPageSpecification(IRequestCycle cycle,
  74.             INamespace namespace, String name) {
  75.         if (!_initialized) {
  76.             _init(cycle);
  77.         }
  78.         //if (!_applicationIsExplodedWAR) {
  79.         //    return null;
  80.         //}
  81.         IResourceLocation location = _locator.resolveLocation(name+".page");
  82.         if (location != null) {
  83.             return _specificationSource.getPageSpecification(location);
  84.         }
  85.         return null;
  86.     }
  87.     /**
  88.      * @see org.apache.tapestry.resolver.ISpecificationResolverDelegate#findComponentSpecification(org.apache.tapestry.IRequestCycle, org.apache.tapestry.INamespace, java.lang.String)
  89.      */
  90.     public IComponentSpecification findComponentSpecification(
  91.             IRequestCycle cycle, INamespace namespace, String type) {
  92.         
  93.         if (!_initialized) {
  94.             _init(cycle);
  95.         }
  96.         //if (!_applicationIsExplodedWAR) {
  97.         //    return null;
  98.         //}
  99.         IResourceLocation location = _locator.resolveLocation(type+".jwc");
  100.         if (location != null) {
  101.             return _specificationSource.getComponentSpecification(location);
  102.         }
  103.         return null;
  104.     }
  105. }


RecursiveFileLocator.java:

  1. // RecursiveFileLocator.java
  2. //
  3. // Copyright 2004 Michael J. Henderson & Associates LLC
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. //     http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. package com.mjhenderson.users.tapestry;
  17. import java.io.File;
  18. import java.net.URL;
  19. import java.util.ArrayList;
  20. import java.util.HashMap;
  21. import java.util.Iterator;
  22. import java.util.List;
  23. import java.util.Map;
  24. import org.apache.commons.logging.Log;
  25. import org.apache.commons.logging.LogFactory;
  26. import org.apache.tapestry.IResourceLocation;
  27. /**
  28.  * @author <a href="mailto:michaelh@mjhenderson.com?subject=com.mjhenderson.users.tapestry.RecursiveFileLocator">Mike Henderson</a>
  29.  *
  30.  */
  31. public class RecursiveFileLocator {
  32.     
  33.     private static final Log LOG = LogFactory.getLog(RecursiveFileLocator.class);
  34.     private IResourceLocation    _location;
  35.     
  36.     private File realRoot ;
  37.     
  38.     private Map _locations      = new HashMap();
  39.     
  40.     public RecursiveFileLocator(IResourceLocation location,String _realRootFolder) {
  41.         realRoot = new File(_realRootFolder);
  42.         _location = location;
  43.     }
  44.     /* (non-Javadoc)
  45.      * @see org.apache.tapestry.INamespace#getPageSpecification(java.lang.String)
  46.      */
  47.     public IResourceLocation resolveLocation(String expectedName) {
  48.         if (LOG.isDebugEnabled()) {
  49.             LOG.debug("getLocation("+expectedName+")");
  50.         }
  51.         IResourceLocation location = _resolveThisFolderLocation(expectedName);
  52.         if (location == null) {
  53.             location = _resolveChildFolderLocation(expectedName);
  54.         }
  55.         return location;
  56.     }
  57.     
  58.     private IResourceLocation _resolveThisFolderLocation(String expectedName) {
  59.         if (LOG.isDebugEnabled()) {
  60.             LOG.debug("_resolveThisFolderLocation("+expectedName+")");
  61.         }
  62.         IResourceLocation location = (IResourceLocation)_locations.get(expectedName);
  63.         if (location == null) {        
  64.             location = _location.getRelativeLocation(_location.getPath()+"/"+expectedName);
  65.             
  66.             if (location.getResourceURL() == null) {
  67.                 return null;
  68.             }
  69.             _locations.put(expectedName, location);
  70.         }
  71.         return location;
  72.     }
  73.     
  74.     
  75.     private IResourceLocation _resolveChildFolderLocation(String expectedName) {
  76.         List children = _getChildFolderLocators();
  77.         Iterator iterator = children.iterator();
  78.         while (iterator.hasNext()) {
  79.             RecursiveFileLocator child = (RecursiveFileLocator)iterator.next();
  80.             if (LOG.isDebugEnabled()) {
  81.                 LOG.debug("_resolveChildFolderLocation() child = " + child + ", expectedName = " + expectedName);
  82.             }
  83.             IResourceLocation location = child.resolveLocation(expectedName);
  84.             if (location != null) {
  85.                 return location;
  86.             }
  87.         }
  88.         return null;
  89.     }
  90.     
  91.     public String toString() {
  92.         return "RecursiveFileLocator { "+_location+" }";
  93.     }
  94.     
  95.     private List _children = new ArrayList();
  96.     private boolean _childrenLoaded = false;
  97.     
  98.     private List _getChildFolderLocators() {
  99.         if (!_childrenLoaded) {
  100.             _loadChildFolderLocators();
  101.              _childrenLoaded = true;
  102.         }
  103.         return _children;
  104.     }
  105.     
  106.     private void _loadChildFolderLocators() {
  107.         
  108.         if (LOG.isDebugEnabled()) {
  109.             LOG.debug("_loadChildFolderLocators() this = " + this);
  110.         }
  111.         File file = realRoot;//new File(path);
  112.         if (file.isDirectory()) {
  113.             File[] files = file.listFiles();
  114.             for (int i = 0; i < files.length; i++) {
  115.                 File f = files[i];
  116.                 if (f.isDirectory() && !f.getName().equals("classes") && !f.getName().equals("lib")) {
  117.                     RecursiveFileLocator child = new RecursiveFileLocator(_location.getRelativeLocation(f.getName()+"/"),file.getAbsolutePath()+"/"+f.getName());
  118.                     if (LOG.isDebugEnabled()) {
  119.                         LOG.debug("_loadChildFolderLocators() child = " + child);
  120.                     }
  121.                     _children.add(child);
  122.                 }
  123.             }
  124.         }
  125.     }
  126. }


.application文件中增加如下定义:

  1.     <extension name="org.apache.tapestry.specification-resolver-delegate"
  2.             class="com.mjhenderson.users.tapestry.CustomSpecificationResolver"
  3.             immediate="yes">
  4. </extension>    


以上代码已经在Tomcat和JBoss上经过测试,希望朋友们可以在别的应用服务器上帮我测测,呵呵。代码比较粗糙,谁要是发现了什么问题或有什么改进思路,希望能联系我:sky_yjck@hotmail.com。

顺便提醒一下,在http://www.t-deli.com上有一个Ant Task,可以根据你的页面存放路径自动更新.application文件里面的页面定义,这样似乎定义几百个页面也是很简单的事情,不过我还是倾向于不用配置直接使用的方法,就看你的需要和使用习惯了。
关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有