【问题标题】:Dropwizard Assets not serving static content outside of root pathDropwizard 资产不提供根路径之外的静态内容
【发布时间】:2025-11-28 16:20:04
【问题描述】:

这是我的 Dropwizard (0.8.5) 应用的基本项目结构:

myapp/
    src/main/groovy/
        org/example/myapp/
            MyApp.groovy
            <lots of other packages/classes>
            controllers/
                site/
                    SiteController.groovy
                dashboard/
                    DashboardController.groovy
        org/example/myapp/views
            site/
                SiteView.groovy
            dashboard/
                DashboardView.groovy
    src/main/resources/
        assets/
            images/
                mylogo.png
        org/example/myapp/views/
            site/
                header.ftl
                index.ftl
            dashboard/
                dashboard.ftl

每个类的要点是:

class MyApp extends Application<MyAppConfiguration> {
    @Override
    void initialize(Bootstrap<MyAppConfiguration> bootstrap) {
        bootstrap.addBundle(new AssetsBundle('/assets/images', '/images', null, 'images'))
        bootstrap.addBundle(new ViewBundle())
    }

    // etc...
}

@Path('/')
@Produces('text/html')
class SiteController {
    @GET
    SiteView homepage() {
        new SiteView()
    }
}

@Path('/app/dashboard')
@Produces('text/html')
class DashboardController {
    @GET
    DashboardView dashboard() {
        new DashboardView()
    }
}

header.ftl  (dropwizard-views-freemarker)
=========================================
<!DOCTYPE html>
<html>
    <head> <!-- lots of stuff omitted here for brevity --> </head>
    <body>
        <div class="well">
            <img src="images/mylogo.png" />
            <br/>This is the header!
        </div>

index.ftl
=========
<#include "header.ftl">
        <p>
            Homepage!
        </p>
    </body>
</html>

dashboard.ftl
=============
<#include "../site/header.ftl">
        <p>
            Dashboard!
        </p>
    </body>
</html>

因此,您可以看到我将 DW 用作实际的 Web 应用程序/UI,并且我同时使用了 Dropwizard Views(Freemarker 绑定)以及 Dropwizard Assets

当我运行它时,应用程序启动得很好,我可以访问我的主页(从 / 提供,映射到 index.ftl)以及我的仪表板页面(从 /app/dashboard 提供,映射到dashboard.ftl)。

问题是两个页面都使用了header.ftl,这会拉入我的assets/images/mylogo.png,但实际上只有我的主页会呈现徽标。在我的仪表板页面上,我做 看到“这是标题!”消息,所以我知道标题正在解析并包含在我的仪表板模板中。 但是,我得到一个加载失败的图像“X”图标,当我打开浏览器的开发工具时,我看到图像上出现了 HTTP 404。

因此,DW 似乎无法从不直接位于根目录 (/) 下的视图/URL 中找到我的图像资产。

在 Dropwizard 资产页面(上面提供的链接)上有一个特殊的警告:

您的应用程序或静态资产都可以从根路径提供,但不能同时提供。后者在使用 Dropwizard 支持 Javascript 应用程序时很有用。要启用它,请将您的应用程序移动到子 URL。

我不完全理解这意味着什么,但怀疑它是这里的罪魁祸首。无论哪种方式,任何人都知道我哪里出了问题,以及我能做些什么(确切的步骤!)来解决这个问题?

【问题讨论】:

  • 你试过用&lt;img src="/images/mylogo.png" /&gt;代替&lt;img src="images/mylogo.png" /&gt;吗?如果这是答案,我会详细说明为什么这很重要。
  • 感谢@JohnathonHavens (+1) - 这行得通!!! 是的,如果可以的话,请在答案中解释绿色支票和丰厚的赏金!再次感谢!
  • 哦不,我失去了赏金!哈哈打盹你输了。

标签: dropwizard


【解决方案1】:

加载两个页面(一个有效,一个无效),然后使用 Firebug 或 Chrome 开发工具检查徽标元素。它试图到达什么路径?我怀疑在您的索引页面上它会转到 http://some.thing/images/mylogo.png 而在您的仪表板上它正在尝试加载 http://some.thing/app/dashboard/images/mylogo.png

尝试在模板中的路径前面添加一个 /,它应该可以从任何地方解析。

(最初在这里回答:Dropwizard-user Google Group

【讨论】:

    【解决方案2】:

    你需要在你的 URI 之前添加 /:

    <img src="/images/mylogo.png" />
    

    这可以从 RFC 3986 URI Generic Syntax 中的示例中进行解释,我提取了相关示例。

    5.4.  Reference Resolution Examples
    
       Within a representation with a well defined base URI of
    
          http://a/b/c/d;p?q
    
       a relative reference is transformed to its target URI as follows.
    
    5.4.1.  Normal Examples
    
          "g"             =  "http://a/b/c/g"
          "g/"            =  "http://a/b/c/g/"
          "/g"            =  "http://a/g"
          "../g"          =  "http://a/b/g"
          "../../g"       =  "http://a/g"
    

    加上前面的 / 使 URI 从域名开始,而不考虑引用的 URI,这使您可以准确地做您想做的事情。

    【讨论】:

      最近更新 更多