问题的提出:
默认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:
// recursivefilelocator.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 java.io.file;
import java.net.url;
import java.util.arraylist;
import java.util.hashmap;
import java.util.iterator;
import java.util.list;
import java.util.map;
import org.apache.commons.logging.log;
import org.apache.commons.logging.logfactory;
import org.apache.tapestry.iresourcelocation;
/**
* @author <a href="mailto:michaelh@mjhenderson.com?subject=com.mjhenderson.users.tapestry.recursivefilelocator">mike henderson</a>
*
*/
public class recursivefilelocator {
private static final log log = logfactory.getlog(recursivefilelocator.class);
private iresourcelocation _location;
private file realroot ;
private map _locations = new hashmap();
public recursivefilelocator(iresourcelocation location,string _realrootfolder) {
realroot = new
闽公网安备 35060202000074号