Dropwizard资产不提供根路径之外的静态内容

时间:2015-11-26 10:13:27

标签: dropwizard

这是我的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作为实际的网络应用/用户界面,并且我正在使用Dropwizard Views(Freemarker绑定)以及Dropwizard Assets

当我运行此应用程序时,应用程序启动就好了,我可以访问我的主页(从/提供,映射到index.ftl)以及我的信息中心页面(来自{ {1}}映射到/app/dashboard)。

问题是这两个网页都使用dashboard.ftl,这会引入我的header.ftl,但只有我的主页实际上会显示徽标。在我的信息中心页面上,我< em> do 看到“这是标题!”消息,因此我知道标题正在解析并包含在我的仪表板模板中。 但是,我收到了一个无法加载图片的“X”图标,当我打开浏览器的开发工具时,我发现我的图像上有HTTP 404。

因此,DW似乎无法从未直接居住在根目录(assets/images/mylogo.png)的视图/网址中找到我的图片资源。

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

  

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

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

2 个答案:

答案 0 :(得分:1)

向上加载两个页面(有效页面和无效页面),并使用Firebug或Chrome开发工具检查徽标元素。它试图去哪条路?我怀疑在您的索引页面上它会转到http://some.thing/images/mylogo.png,而在您的信息中心上,它会尝试加载http://some.thing/app/dashboard/images/mylogo.png

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

(原文在这里回答:Dropwizard-user Google Group

答案 1 :(得分:1)

您需要在URI之前添加/:

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

这可以从RFC 3986 URI通用语法中的示例中解释,我提取了相关的示例。

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如何,这使您可以精确地执行所需的操作。

相关问题