将资源放在 jar 文件中并将它们公开给网络 [英] put resources in jar file and expose them to the web

查看:39
本文介绍了将资源放在 jar 文件中并将它们公开给网络的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  1. 我有一个 web 应用程序,它的资源文件夹中有一堆 js 和 css 文件.我怎样才能将它们公开给网络以便能够在我的网页中引用它们?以及如何引用这些文件?

  1. I got a web application, whose resources folder has a bunch of js and css files. How can I expose them to the web to be able to reference them in my web pages? And how are those files referenced?

我还想稍后将这些资源放在一个单独的 jar 文件中,并且(就像在上一个问题中一样)也将它们公开给网络.我如何使用 maven 做到这一点?我的意思是,将它们打包,以便它们可以在 META-INF/resources 中.

I also would like to later place those resources in a separate jar file and (like in the previous question) also expose them to the web. How can I do it using maven? I mean, package them so they can be in META-INF/resources.

推荐答案

使用兼容 servlet-3.0 的容器(例如 Tomcat 7 及更高版本),并将您的资源放在 META-INF/resources 在您的 jar 文件中,这些文件将由容器提供,就好像它们直接位于应用程序的根目录下一样.jar 本身必须在 WEB-INF/lib 下.

Use a servlet-3.0 compliant container (Tomcat 7 and beyond, for example), and place your resources inside META-INF/resources in your jar file, and these files will be served by the container as if they were directly under the root of the application. The jar itself must be under WEB-INF/lib.

Maven 与您的问题没有太大关系.如果您希望您的资源由应用程序提供服务,而不是在 jar 文件中,请将它们放在标准 Maven 目录布局的 src/main/webapp 中.

Maven doesn't have much to do with your question. If you want your resources to be served by the application, and not be in a jar file, you place them in src/main/webapp in the standard Maven directory layout.

如果你想构建一个包含这些资源的jar文件,那么这个jar文件应该是由另一个Maven项目构建的,并且是Maven war项目的依赖项之一.在 jar 项目中,资源必须位于 src/main/resources/META-INF/resources 下.

If you want to build a jar file containing these resources, then this jar file should be built by another Maven project, and be one of the dependencies of the Maven war project. Inside the jar project, the resources will have to be under src/main/resources/META-INF/resources.

假设文件名为 foo.png 并且位于 web 应用根目录下的 images 文件夹中,或者位于 META-INF/resources/下images 文件夹,您可以使用相对路径或绝对路径从应用程序的页面内部引用它.假设您的应用程序的上下文路径是 /myFirstApp.图像的地址将是 /myFirstApp/images/png.现在假设你的页面的 URL 是 /myFirstApp/bar/index.html,你可以使用

Suppose the file is named foo.png and is in the images folder under the root of the webapp, or under the META-INF/resources/images folder of a jar file, you reference it from inside a page of your app using a relative path or an absolute path. Suppose your app's context path is /myFirstApp. The address of the image will be /myFirstApp/images/png. Now suppse the URL of your page is /myFirstApp/bar/index.html, you can use

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

<img src="/myFirstApp/images/foo.png" />

绝对路径使用起来更容易也更安全,但您不应该硬性规定应用程序的上下文路径.因此,在 JSP 中,您将使用

An absolute path is much easier and safer to use, but you shouldn't hard-ce the context path of the application. So, inside a JSP, you would thus use

<img src="${pageContext.request.contextPath}/images/foo.png" />

或者,使用 JSTL:

or, using the JSTL:

<img src="<c:url value='/images/foo.png'/>" />

这篇关于将资源放在 jar 文件中并将它们公开给网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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