My project hierarchy is like this.
ProjectName--> WebContent-->WEB-->INF-->ZUL-->XYZ-->images(Folder)+abc.zul
--> when I wants to access image within images folder in abc.zul(which is also inside XYZ fodler) images are not loaded.i am accessing images like this.
image="/images/app_icon.png"
I believe it should be WEB-INF instead of WEB->INF.
Any resource inside the WEB-INF can be accessed programmatically only.
You can put images folder out of the WEB-INF and under WebContent folder in order to access images as image="/images/app_icon.png".
Again it depends on your requirement and implementation.
Related
I am new to Spring framework in spring site There is tutorial at https://spring.io/guides/gs/uploading-files/ that upload file to the root folder "upload-dir" (this folder is beside src root folder)
questions:
How can I access and show image in browser (or access it in thymeleaf by th:src="#{}" syntax) -
by browsing to localhost:8080/files/first.jpg because of controller it give me download link.
should I always upload file to folder that beside src folder for example I want to upload file to "src/main/resources/static/file" is it possible?
When accessing files in your code, Spring will (by default) assume that the src/main/resources is the parent directory. If you are planning on accessing the files that are uploaded, then I would use src/main/resources (or a subdirectory of this location) as the upload path. This way, you can simply access them in Thymeleaf as such:
Location: src/main/resources/picture.jpg
Thymeleaf: th:src="#{picture.jpg}"
Or if the file exists in a subdirectory:
Location: src/main/resources/somedir/picture.jpg
Thymeleaf: th:src="#{somedir/picture.jpg}"
If you are storing the file(s) elsewhere, then you can also access them using various prefixes like classpath or file, i.e.:
classpath:com/myapp/config.xml
See more about Resources in Spring here:
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/resources.html
Hope this helps!
Im trying to read files(xml, images) from src/main/resources. But it doesnt work.My resources folder is Source folder. For example, when i m trying to read log4j.xml tomcat looking for it in C:/bin... And i also cant read images from resources. I can read it only from webapp. I ve read, that tomcat automatically replace files from resources to webinf, but i think that it doesnt work in my case.
Please, help. I dont have any idea.
Even if the png is added to the WEB-INF folder it wouldn't be accessible from the JSP page in your case.
On runtime a JSP page renders into a servlet that return HTML code in the response. Then a browser will parse your tag <img src="/Pajero.png" ... /> and send a request onto YOUR_HOST/Pajero.png url to access the image and get the error code 404, because content of the WEB-INF folder is not accessible for a client side in a java web application.
Perhaps it will be better to place images on the webapp folder at your case.
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("log4j.xml").getFile());
You can use something like this to get resources from classpath.
Regards
I'm working in a Spring MVC project and I have the following problem:
This is my folders hierarchy
--src
--main
--webapp
--WEB-INF
--views
--css
--mystyle.css
---myview.html
and this is how I call mystyle.css in myview.html
href="css/mystyle.css"
but my CSS doesn't show up.
But if I put my views folder outside of WEB_INF, my CSS does show up and it works like this:
--src
--main
--webapp
--views
--css
--mystyle.css
--myview.html
--WEB-INF
And I call my CSS the same way like before href="css/mystyle.css"
Is there something different that I didn't notice? Why doesn't my CSS works outside of the WEB-INF folder and it does not work inside?
While it is true that you cannot access resources under WEB-INF folder directly, you can still keep your location and add a configuration like
<resources mapping="/css/**" location="/WEB-INF/views/css/" />
in your spring mvc configuration, or a java config equivalent if you're using java config. Note that you should be accessing css with an absolute URL, so if you're serving from root
href="/css/mystyle.css"
or prepand a context if you're using one
Right, CSS files need to be in a path that is directly visible to the browser. WEB-INF is hidden.
I have a war file that I have deployed which contains an images folder in the WEB-APP directory. I tried accessing the images stored in the folder using
"localhost:8080/testapp/images/image1.jpg" but I am getting a 404 response. Can someone please help me with this?
Thanks in advance.
Your images directory needs to be a sibling of WEB-INF, not a child of it. Try this:
your-war-file.war
|-images/
| |-image1.jpg
| |-image2.jpg
| `-image3.jpg
|-WEB-INF/
| |-classes/
| |-lib/
| `-web.xml
|-index.html
`-404.html
Assuming you have your folder 'images' in the base directory for this web app (on the same level as 'META-INF' and 'WEB-INF' catalogs), the path seems correct. Maybe check that you haven't put 'images' folder inside 'WEB-INF' where it is not accessible? If that doesn't work, try accessing some other file inside your web app to check that it was deployed on server.
Clients may not directly access artifacts under WEB-INF.
Put them in a location directly accessible if you're not streaming them from an app endpoint.
how to read from path ?
use ("."+File.seperator+"some else") path of File for current path into your code or html file
By default static files are located in WEB-INF directory (accessible as /images/logo.png):
foo.war
WEB-INF
web.xml
images
logo.png
I want to change the structure and use this one instead (still accessible as /images/logo.png):
foo.war
WEB-INF
web.xml
static
images
logo.png
How can I do this with web.xml?
The container will repsond with a 404 NOT FOUND error if you directly access the files under WEB-INF using HTTP GET .
But now , you said you can access WEB-INF/images/logo.png by /images/logo.png , so I think your web application most probably achieve this result by some URLRewriteFilter mechainsim or by some Java code in the servlet level (eg a filter) , or by your web application 's framework . I suggest you to check your web application to see what mechanism causes this behvaior now and configurate to your desired result accordingly.
According to http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/WCC3.html,:
A WAR has a specific directory
structure. The top-level directory of
a WAR is the document root of the
application. The document root is
where JSP pages, client-side classes
and archives, and static Web resources
are stored.
The document root contains a
subdirectory called WEB-INF, which
contains the following files and
directories:
web.xml: The Web application
deployment descriptor Tag library
descriptor files (see Tag Library
Descriptors) classes: A directory
that contains server-side classes:
servlets, utility classes, and
JavaBeans components lib: A
directory that contains JAR archives
of libraries (tag libraries and any
utility libraries called by
server-side classes).
You can also create
application-specific subdirectories
(that is, package directories) in
either the document root or the
WEB-INF/classes directory.
So the default behavior is what you're looking for. Is your document root set incorrectly to serve content from WEB-INF?
You may use a filter or URLRewriteFilter to point /images/* to /static/images/*.
If you just want your folder structure to be /static/images for development time organization purposes, but the deployment URL to be /images -- you may need to alter your build script to copy /static/** to /.
I personally would not bother whether my static files are referred as /static/images or /images -- because they would be referred in my code (only), which I have control over.
If you are using these files in CSS and that's why you wanted the path to stay the same... better keep the images under /static/css/images and have the images that are referred in the CSS here. In this way, no matter where you move your CSS folder, you would not bother spoiling your CSS.