2015-11-26 54 views
0

這裏是我的Dropwizard(0.8.5)應用的基本項目結構:Dropwizard資產不服務的根路徑的靜態內容外

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應用程序時很有用。要啓用它,請將您的應用程序移到一個子網址。

我不完全明白這是什麼意思,但懷疑它是這裏的主要罪魁禍首。無論哪種方式,任何人都可以看到我要去的地方,以及我能做些什麼(確切的步驟!)來解決這個問題?

+1

您是否試過''而不是''?如果這是答案,我會詳細說明爲什麼這很重要。 –

+0

Thanks @JohnathonHavens(+1) - This works !!! **是**,如果可以的話,請在綠色支票和有利可圖的賞金答案中加上解釋!再次感謝! – smeeb

+0

哦,不,我失去了賞金!哈哈打盹你輸了。 –

回答

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,它允許你做的正是你想要的東西無關。