使用Spring ResourceServlet同时服务多个资源
ResourceServlet 的 JavaDoc 声明它可以返回资源列表。但这种使用模式的例子似乎很少。
我们有一个包含以下内容的 web.xml:
<servlet>
<servlet-name>Resource</servlet-name>
<servlet-class>org.springframework.web.servlet.ResourceServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Resource</servlet-name>
<url-pattern>/combo</url-pattern>
</servlet-mapping>
当我们向 url 发出请求时,如下所示: http://localhost:8080/app/combo?resource =js/file1.js;js/file2.js
我们似乎只在响应中获取了 file1。
对于这个用例来说,正确的配置是什么?
The JavaDoc for the ResourceServlet states that it can return a list of resources. But examples of this usage pattern seem to be sparse at best.
We have a web.xml with the following:
<servlet>
<servlet-name>Resource</servlet-name>
<servlet-class>org.springframework.web.servlet.ResourceServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Resource</servlet-name>
<url-pattern>/combo</url-pattern>
</servlet-mapping>
When we make a request to url along the lines of:
http://localhost:8080/app/combo?resource=js/file1.js;js/file2.js
We only seem to get file1 in the response.
What would a proper configuration be for this use case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ResourceServlet
已被弃用,转而使用
但是,它不处理多个资源。你必须制作自己的控制器才能做到这一点。对于
ResourceServlet
,代码中使用的分隔符是,; \t\n
- 其中任何一个都应该有效。The
ResourceServlet
has been deprecated in favour of using<mvc:resources />
However, it doesn't handle multiple resources. You'd have to make your own controller to do that.As for the
ResourceServlet
, the delimiters used in the code are,; \t\n
- any of them should work.我们案例中的问题是应用程序使用 mvc:resource 实用程序来处理静态文件的版本控制。正如 Bosho 指出的那样,mvc:resource 实用程序的后端不会正确响应多个文件,我通过查看源代码也意识到了这一点。
The problem in our case turned out to be that the application used the mvc:resource utility to handle versioning of the static files. The backend of the mvc:resource utility will not respond to multiple files properly as Bosho noted, and I was aware of too from looking at the source.