Dropwizard资产不在根路径之外提供静态内容 [英] Dropwizard Assets not serving static content outside of root path
问题描述
这是我的Dropwizard(0.8.5)应用程序的基本项目结构:
Here's my Dropwizard (0.8.5) app's basic project structure:
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, m还同时使用了 Dropwizard视图(Freemarker绑定)格式为 Dropwizard资产。
So you can see I'm using DW as an actual web app/UI, and that I'm utilizing both Dropwizard Views (Freemarker binding) as well as Dropwizard Assets.
当我运行此程序时,该应用程序启动正常,我可以访问我的两个首页(从 /
提供,该网站映射到 index.ftl
)以及我的仪表板页面(从 / app / dashboard
提供),该页面映射到 dashboard.ftl
)。
When I run this, the app starts up just fine and I am able to visit both my homepage (served from /
which maps to index.ftl
) as well as my dashboard page (served from /app/dashboard
which maps to dashboard.ftl
).
问题是两个页面都使用 header.ftl
,它会拉入我的 assets / images / mylogo.png
,但是只有我的主页才能真正显示徽标。在我的信息中心页上,我做看到 这是标头!消息,所以我知道标头正在解析并包含在内与我的仪表板模板。 但是,我得到一个无法加载图像的 X图标,并且当我打开浏览器的开发工具时,我看到图像上得到了HTTP 404。
The problem is that both pages use the header.ftl
, which pulls in my assets/images/mylogo.png
, but only my homepage actually renders the logo. On my dashboard page, I do see the "This is the header!" message, so I know the header is being resolved and included with my dashboard template. But, I get a failed-to-load-image "X" icon, and when I open my browser's dev tools I see that I'm getting HTTP 404s on the image.
因此,DW似乎无法从不是直接位于根目录下的视图/ URL中找到我的图像资产( /
)。
So it seems that DW is unable to find my image asset from a view/URL not directly living under root (/
).
在Dropwizard资产页面(上面提供的链接)上,有一个特殊的警告:
On the Dropwizard Assets page (link provided above) there's a peculiar warning:
可以从根路径提供应用程序或静态资产,但不能同时提供。当使用Dropwizard备份Javascript应用程序时,后者非常有用。要启用它,请将您的应用程序移至子URL。
Either your application or your static assets can be served from the root path, but not both. The latter is useful when using Dropwizard to back a Javascript application. To enable it, move your application to a sub-URL.
我并不完全理解这意味着什么,但怀疑它是什么。这里的主要罪魁祸首。无论哪种方式,任何人都可以看到我要去哪里,以及我可以做什么(确切的步骤!)来解决此问题?
I don't entirely understand what this means, but suspect it is the main culprit here. Either way, anyone see where I'm going awry, and what I can do (exact steps!) to fix this?
推荐答案
加载两个页面(一个有效,一个无效),然后使用Firebug或Chrome开发者工具检查徽标元素。它试图走什么路?我怀疑在您的索引页面上它将是 http://some.thing/images/mylogo.png 而在您的仪表板上,它试图加载 http://some.thing/app/dashboard/ images / mylogo.png
Load both pages up (the one that works, and the one that doesn't), and use Firebug or Chrome dev tools to inspect the logo element. What path is it trying to get to? I suspect on your index page it's going to http://some.thing/images/mylogo.png whereas on your dashboard it's trying to load http://some.thing/app/dashboard/images/mylogo.png
尝试在模板路径的前面加上一个/,它应该可以在任何地方解析。
Try putting an additional / in front of the path in your template, and it should resolve from anywhere.
(最初在这里回答: Dropwizard用户Google Group )
这篇关于Dropwizard资产不在根路径之外提供静态内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!