熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> JSP教程 >> 正文

如何自由組織Tapestry頁面規范文件

2022-06-13   來源: JSP教程 

  問題的提出
  默認Tapestry的頁面模板文件l)及其對應的規范文件(page)可以放在web根目錄或其下的WEBINF/目錄中Tapestry可以不用任何配置(在以前的版本中需要在application文件中定義一下頁面)就能正確運行如果你需要在這些根目錄下以文件系統的目錄形式組織你的頁面則Tapestry是不會自動搜索這些子目錄的你必須在application文件中定義這些不在默認位置的頁面這樣一來每加一個頁面都需要定義一下如果頁面數目眾多定義起來就會比較麻煩如何才能避免這些配置呢?本文的目的就是嘗試解決這個問題
  
  問題的解決
  本文是在參考文檔()源代碼的基礎上稍作修改而成主要是為了解決不能在Tomcat中使用的問題為了更好的了解朋友們最好能閱讀一下原文和原來的代碼主要修改的地方是給RecursiveFileLocator傳遞一個真實路徑地址以便能夠列出目錄下面的子目錄從而實現層次查找
  
  解決的途徑就是定義一個ISpecificationResolverDelegate以便Tapestry在常規路徑下找不到文件時進行處理
    CustomSpecificationResolverjava:
  
  // CustomSpecificationResolverjava
  //
  // Copyright Michael J Henderson & Associates LLC
  //
  // Licensed under the Apache License Version (the License);
  // you may not use this file except in compliance with the License
  // You may obtain a copy of the License at
  //
  //  
  //
  // 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 commjhendersonuserstapestry;
  
  import monsloggingLog;
  import monsloggingLogFactory;
  import orgapachetapestryINamespace;
  import orgapachetapestryIRequestCycle;
  import orgapachetapestryIResourceLocation;
  import orgapachetapestryTapestry;
  import orgapachetapestryengineISpecificationSource;
  import orgapachetapestryresolverISpecificationResolverDelegate;
  import orgapachetapestryspecIComponentSpecification;
  
  /**
   * @author <a mailto:mich?subject=commjhendersonuserstapestryCustomSpecificationResolver>Mike Henderson</a>
   *
   */
  public class CustomSpecificationResolver implements
      ISpecificationResolverDelegate {
    
  
    private static final Log LOG = LogFactorygetLog(RecursiveFileLocatorclass);
  
    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 = TapestrygetApplicationRootLocation(cycle)getRelativeLocation(/WEBINF/);
      IResourceLocation rootLocation = TapestrygetApplicationRootLocation(cycle)getRelativeLocation(/);
      //_applicationIsExplodedWAR = rootLocationgetResourceURL()toString()startsWith(file:);
      //if (_applicationIsExplodedWAR) {
      _realRootFolder = cyclegetRequestContext()getServlet()getServletContext()getRealPath(/);
      _locator = new RecursiveFileLocator(rootLocation_realRootFolder);
      _specificationSource = cyclegetEngine()getSpecificationSource();
      //}
      _initialized = true;
    }
    
  //  private boolean checkLocationIsFileLocation(IResourceLocation location) {
  //    String url = locationgetResourceURL()toString();
  //    Systemoutprintln(url = +url);
  //    return urlstartsWith(file:);
  //  }
    
    /**
     * @see orgapachetapestryresolverISpecificationResolverDelegate#findPageSpecification(orgapachetapestryIRequestCycle orgapachetapestryINamespace javalangString)
     */
    public IComponentSpecification findPageSpecification(IRequestCycle cycle
        INamespace namespace String name) {
  
      if (!_initialized) {
        _init(cycle);
      }
      //if (!_applicationIsExplodedWAR) {
      //  return null;
      //}
      IResourceLocation location = _locatorresolveLocation(name+page);
      if (location != null) {
        return _specificationSourcegetPageSpecification(location);
      }
      return null;
    }
  
    /**
     * @see orgapachetapestryresolverISpecificationResolverDelegate#findComponentSpecification(orgapachetapestryIRequestCycle orgapachetapestryINamespace javalangString)
     */
    public IComponentSpecification findComponentSpecification(
        IRequestCycle cycle INamespace namespace String type) {
      
      if (!_initialized) {
        _init(cycle);
      }
      //if (!_applicationIsExplodedWAR) {
      //  return null;
      //}
      IResourceLocation location = _locatorresolveLocation(type+jwc);
      if (location != null) {
        return _specificationSourcegetComponentSpecification(location);
      }
      return null;
    }
  
  }
  RecursiveFileLocatorjava:
  
  // RecursiveFileLocatorjava
  //
  // Copyright Michael J Henderson & Associates LLC
  //
  // Licensed under the Apache License Version (the License);
  // you may not use this file except in compliance with the License
  // You may obtain a copy of the License at
  //
  //  
  //
  // 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 commjhendersonuserstapestry;
  
  import javaioFile;
  import URL;
  import javautilArrayList;
  import javautilHashMap;
  import javautilIterator;
  import javautilList;
  import javautilMap;
  
  import monsloggingLog;
  import monsloggingLogFactory;
  import orgapachetapestryIResourceLocation;
  
  /**
   * @author <a mailto:mich?subject=commjhendersonuserstapestryRecursiveFileLocator>Mike Henderson</a>
   *
   */
  public class RecursiveFileLocator {
    
    private static final Log LOG = LogFactorygetLog(RecursiveFileLocatorclass);
  
    private IResourceLocation  _location;
    
    private File realRoot ;
    
    private Map _locations   = new HashMap();
    
    public RecursiveFileLocator(IResourceLocation locationString _realRootFolder) {
      realRoot = new
From:http://tw.wingwit.com/Article/program/Java/JSP/201311/19198.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.