问题
I have a maven project that I run using jetty:
$ mvn run:jetty
Where in my project should I be storing my static files like HTML, CSS, Javascript, images?
My layout is using a simple web app arch type:
/src/main/java/webapp/web-inf/views/
Should I just create a folder there named e.g. 'assets' ?
And then my view pages will reference the /assets
folder somehow? I'm confused as to what path I will use in my html pages to reference an image like:
/assets/images/logo.png
回答1:
This isn't so much a Jetty question as it is a general Java webapp question. If you plan to serve them out directly (like *.css, *.css, images, etc), put them somewhere above WEB-INF
but below your docroot. Java WebApps are all the following basic directory structure.
<docroot>
+WEB-INF/
+lib/
+classes/
Anything in <docroot>
is reachable directly through straight up http. Anything WEB-INF
and below is not. A really simple webapp with one page (index.jsp), one image in an images directory, and its configuration file (web.xml) would look like this.
index.jsp
images/bob.jpg
WEB-INF/
web.xml
lib/
classes/
In index.jsp you could reference bob.jpg like...
<img src="images/bob.jpg"/>
回答2:
This is really a Maven question rather than a Jetty question.
Typically you would put your images (etc) in the maven webapp
directory - i.e. source/main/webapp/
(not under web-inf
)
How you structure things underneath that is up to you, but it will mostly depend on how much content you are expecting to put in, and how you think it is best to organise it.
source/main/webapp/assets/images
is fine, but so is source/main/webapp/images
or source/main/webapp/static/
.
Then, within your HTML, you reference the images using whatever path you put in beneath the webapp
bit.
回答3:
The general answer is - the root of your web application is webapp. Dynamic resources (as JSP pages or Freemarker templates) would better off be in a web-inf/ subfolder (they are accessible through classloader but not from a direct browser request).
来源:https://stackoverflow.com/questions/9107152/where-to-store-static-files-like-html-css-javascript-in-a-jetty-project