首页 > 解决方案 > 在 JBoss 7.1.1 上运行 Tapestry 5.3.8

问题描述

在 JBoss 5 到 7.1.1 的迁移过程中,我遇到了多个问题。其中之一是 Tapestry 根本不工作。

此错误的唯一“有用”迹象是,当我尝试访问我的 Web 应用程序(EAR 中捆绑的 WAR)并在浏览器中显示“未找到”时,服务器会响应 404。

即使将每个 LOG4J 输出都转换为 DEBUG,服务器日志也没有提供任何有用的信息。

我尝试升级不同的依赖项,改变战争结构以符合 Tapestry 规范等。

我注意到我的应用程序ClasspathURLConverter与此处的应用程序相对应:https ://wiki.apache.org/tapestry/HowToRunTapestry5OnJBoss5

但是转换器在 JBoss 7 上无法正常工作

标签: javaspringjbossjboss7.xtapestry

解决方案


这个问题的解决方案确实是在 Tapestry 查找页面、组件等时的错误 URL 翻译(我必须深入挖掘 Tapestry 源代码并一直调试它)。

所以我试图深入研究 VFS 系统和 URL 转换。我找到了 5 个以上的链接,其中包含相同的代码供转换器使用(例如http://www.voidcn.com/article/p-mpuwwlxm-eh.html)。这个实现的问题是如果我的 JAR 直接位于{myEar}/lib/文件夹中。我修改了代码,但导致路径指向文件系统中已爆炸但为空的 jar。

然后我在这里找到了另一个解决方案:https ://developer.jboss.org/thread/172599 - 更简单且有效。

所以这是最终的解决方案:

AppModule.java:

public static void contributeServiceOverride(MappedConfiguration<Class, Object> configuration) {
    configuration.add(ClasspathURLConverter.class, new MyClasspathURLConverterImpl());
}

MyClasspathURLConverterImpl.java:

public URL convert(URL url) {
    if (url != null && url.getProtocol().startsWith("vfs")) {
        try {
            return getRealFilePath(url.getPath());
        } catch (Exception e) {
            log.error(e.getCause());
        }
    }
    return url;
}

private URL getRealFilePath(String urlString) throws IOException {
    VirtualFile vFile = VFS.getChild(urlString);
    URL physicalUrl = VFSUtils.getPhysicalURI(vFile).toURL();
    String physicalUrlStr = physicalUrl.toString();

    if (physicalUrlStr.contains(".jar")) {
        int jarIdx = physicalUrlStr.indexOf(".jar");
        String part1 = physicalUrlStr.substring(0, jarIdx + 4);
        String part2 = physicalUrlStr.substring(jarIdx + 4);

        String jarName = part1.substring(part1.lastIndexOf("/") + 1, jarIdx + 4);

        String dir = part1 + part2.substring(0, part2.indexOf("/"));

        String jarLocation = dir + "/" + jarName;
        String packageName = part2.substring(part2.indexOf("/"));
        if (packageName.startsWith("/contents")) {
            packageName = packageName.substring(9);
        }
        String result = "jar:" + jarLocation + "!" + packageName;
        physicalUrl = new URL(result);
    }
    return physicalUrl;
}

pom.xml

<dependency>
    <groupId>org.jboss</groupId>
    <artifactId>jboss-vfs</artifactId>
    <version>3.2.14.Final</version>
    <scope>provided</scope>
</dependency>

推荐阅读