Question

I'm new to Spring MVC and the Spring framework in general and I'm trying to learn it.

I did a small "hello world" project using the Spring Tool Suite and encountered a strange issue. So I started a new Spring Project and chose Spring MVC Project. I created a controller and a view jsp page to display a hello page. All is working fine. There is no problem here. Then I decided to include a picture in my jsp page. I copied a picture to the "webapp/resources" folder and put an img tag like this in my view page (whose path is "webapp/WEB-INF/views/hello.jsp"). The tag looks like this :

When I run the application and go to my view page it doesn't show the image in STS internal browser or in my regular browser. I tried to view the source of the page from my browser and the source does have the img tag but for some reason it's not shown.

I tried to put the image directly in "webapp/WEB-INF/views" along with my jsp file and changed the tag to this:

but still no success. Finally, I blamed this on my inexpirience with creating jsp's and tried to open up my regular Eclipse IDE (i.e. one different from STS) and created a Dynamic Web Project where I inserted the tag into a jsp file and copied the picture in one of the project's subfolders. I ran the project and now it works ; the picture is shown.

Why is happening this? Why it works in a regular web project and doesn't work in a Spring MVC Project? Thank you for your time and I appreciate if you help me!

Was it helpful?

Solution

I tried to put the image directly in "webapp/WEB-INF/views"

The image shouldn't be under /WEB-INF/ because this folder is not accessibile outside the app. It should be under webapp, e.g.webapp/resources/myLogo.png and then use it like : resources/myLogo.png in the img src.

OTHER TIPS

jsp files are not regular html file. you should use <c:url /> tag to make sure your resources path are computed from the root of your project:

<img src='<c:url value="/img/mylogo.png"/>' class="logo"/>

where mylogo.png is inside WebContent/img/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top