javamavenspring-mvcserver-side-includes

springmvc maven link files in jsp file


I use eclipse, springmvc and maven. I created a folder under

Web-Content --> WEB-INF --> resources

I added the following into my servlet:

 <mvc:resources mapping="/resources/**" location="/WEB-INF/resources/" />
<mvc:annotation-driven />

Then I try to include a picture into my index.jsp file.(Web-Content->index.jsp)

<img src = "/resources/images/Logo.png" />

But it's not loading my image. I tryed many tutorials but it doesn't work. I guess I'm something basic wrong. Can you tell me whats wrong?

EDIT:

my serlvet got this

<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/" />

In my jsp file I did this:

<img src="<c:url value="/resources/logo.png" />"/>

I got resource folders with the logo.png file in the following places:

main->resources
main->webapp->resources
main->webapp->WEB-INF->resources

It still doesn't work I also tried :

<mvc:resources mapping="/resources/**"
               location="/, classpath:/WEB-INF/public-resources/"
               cache-period="10000" />

and include the picture with

/resources/images/logo.png

and the picture located in

src/main/webapp/images/logo.png

doesnt't work for me

EDIT:

I guess it this:

<mvc:resources mapping="/resources/**" location="/resources/" />

got no effect to the program. I can always acces the url http://localhost:8080/test/resources/logo.png doenst matter if I got this mvc line in my serlvet or not. If I change

<mvc:resources mapping="/resources/**" location="/resources/" />

to

<mvc:resources mapping="/resources1/**" location="/resources/" />

I can't acces to the file with http://localhost:8080/test/resources1/logo.png but with http://localhost:8080/test/resources/logo.png

Greets Sam


Solution

  • As an aside, while it is probably not best to make an Eclipse Dynamic Web Project and convert it to a Maven project through Eclipse, it will work, because Eclipse will put an entry into your POM that points to the WebContent directory. Have a look you will probably find it there. There is nothing wrong with making a Dynamic Web Project per se, but I typically change the initial directory configuration to match maven's expectations in the Wizard and convert to a maven project right away. Much easy to work on the POM than copy jars to the lib directory.

    Otherwise, the resources directory you referring to should be in the root of your war file. The best way to get it there is to put resources at the root of your WebContent directory, not under WEB-INF. Try moving it there, open the .war file and inspect the contents to be sure it is in the root, and you should be good to go.