I am trying to deploy a Webapp in Java using Spring MVC on OpenShift cloud server (using JBoss & MySQL cartridges). While I am able push my code and retrieve data from MySQL database, I am unable to access/display images in my JSP pages.
Images are stored in MyProject/resources/images/ folder (MyProject/resources/images/stockmarket.jpg)
My controller-servlet.xml file looks like this:
In my JSP, this is how I try to access the image file:
However, in my webpage the image does not show-up. Can anyone please help? Where am I making a mistake? Rest of the functionality in my Webapp is working fine.
What URL do you use to access your web application?
What is the URL that the broken image is using to look up the image? (You can find out using the Browser F12 developer tools, or just viewing source on the generated page)
What URL should it be using? Can you fiddle around with the URL manually to load the image successfully?
Couple of things I think might be contributing here:
You say your image is at: MyProject/resources/images/stockmarket.jpg
But then you try and reference it with: /resources/stockmarket.jpg
What happened to "MyProject" ?
What happened to "/images" ?
The image is using an absolute URI - it starts with "/", so will look for the picture from the root of the webserver.
Most probably your URL needs to include the servlet context value
However most probably, the correct url to be looking is at "/MyProject/resources
The context root for a web application is dynamic (normally based on the name of the war file)
The standard way of doing this is to
- Include an expression in the image url to the servlet context into the URL
- use a JSP/JSTL tag to convert a url for you (looks slightly tidier)