(Java)App Engine中的静态文件无法访问 [英] Static files in (Java) App Engine not accessible

查看:72
本文介绍了(Java)App Engine中的静态文件无法访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

范例文件表示您只需放置你的文件在war /(或者子目录),并且它们应该可以从主机访问(只要它们不是JSP或者WEB-INF)。例如,如果您将foo.css置于war中,那么您应该可以通过 http:// localhost: 8080 / foo.css 。但是,这根本不适合我。无法访问我的静态文件。



appengine-web.xml 说,你也可以特定地将某些类型表示为静态。我已经尝试过这种方式,并没有什么区别。



我是否错过了某些明显的东西?

更新:
我发现web.xml中的一个映射有点太过分了。以下是罪魁祸首:


 < servlet> 
< servlet-name>主< / servlet-name>
< servlet-class> MainServlet< / servlet-class>
< / servlet>
< servlet-mapping>
< servlet-name>主< / servlet-name>
< url-pattern> /< / url-pattern>
< / servlet-mapping>

它似乎抓住了所有未被抓住的东西,成为其他规则之一不明白,因为在url-pattern的末尾没有*。这似乎也与文档存在直接矛盾说:


注意:静态文件,为用户逐字提供的文件,如图像,CSS或JavaScript ,与部署描述符中提到的路径分开处理。无论在部署描述符中使用何种servlet和过滤器映射,对WAR路径中与被指定为静态文件的文件路径相匹配的URL路径请求都将为该文件提供服务。您可以使用 appengine-web.xml从排除静态文件的文件中排除文件


那么,我怎么能有一个匹配我的域的基础的规则(例如 http://www.example.com/ ),仍然允许静态文件过滤?

解决方案

尝试在appengine-web.xml中手动定义静态文件,如

 <静电文件> 
< include path =/ favicon.icoexpiration =1d/>
< include path =/ static / **/>
< include path =/ **。css/>
< / static-files>

即使像servlet一样,这对我也很有用,例如

 < servlet的映射> 
< servlet-name> testServlet< / servlet-name>
< url-pattern> /< / url-pattern>
< / servlet-mapping>

 < servlet的映射> 
< servlet-name> testServlet< / servlet-name>
< url-pattern> / *< / url-pattern>
< / servlet-mapping>

请参阅静态文件和资源文件


The example documentation says that you simply need to place your files in war/ (or a subdirectory) and they should be accessible from the host (as long as they aren't JSPs or in WEB-INF). For example, if you place foo.css in war/ then you should be able to access it at http://localhost:8080/foo.css. However, this isn't working for me at all. NONE of my static files are accessible.

The docs on appengine-web.xml say that you can also specifically denote certain types as static. I've tried this as well and it makes no difference.

Am I missing something obvious?

UPDATE: Turns out one of the mappings in my web.xml was a little too aggressive. The following was the culprit:

<servlet>
    <servlet-name>Main</servlet-name>
    <servlet-class>MainServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>Main</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

It seems that it was grabbing everything that wasn't grabbed be one of the other rules, which I don't understand because there was no * on the end of the url-pattern. It also seems to be directly contradictory to the documentation that says:

Note: Static files, files that are served verbatim to users such as images, CSS or JavaScript, are handled separately from paths mentioned in the deployment descriptor. A request for a URL path that matches a path to a file in the WAR that's considered a static file will serve the file, regardless of servlet and filter mappings in the deployment descriptor. You can exclude files from those treated as static files using the appengine-web.xml file.

So, how can I have a rule that matches the base of my domain (eg. http://www.example.com/) and still allows the static files to filter through?

解决方案

Try manually defining the static files in appengine-web.xml like

<static-files>
  <include path="/favicon.ico" expiration="1d" />
  <include path="/static/**" />
  <include path="/**.css" />      
</static-files>

This works for me even with servlets like

<servlet-mapping>
 <servlet-name>testServlet</servlet-name>
 <url-pattern>/</url-pattern>
</servlet-mapping>

and

<servlet-mapping>
 <servlet-name>testServlet</servlet-name>
 <url-pattern>/*</url-pattern>
</servlet-mapping>

See Static Files and Resource Files

这篇关于(Java)App Engine中的静态文件无法访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆