Kaushik Balasubramanain
Kaushik Balasubramanain

Reputation: 1258

Link CSS and Images to Spring MVC maven Projcet

I have a Spring project created using maven. The following is my directory structure.

enter image description here

I want to add css images to this project. For this, i created a resources folder inside the web-inf directory and placed an image there. To my dispatcher servlet xml, i added 3 lines, pertaining to mvc. One line is in the xmlns:mvc and the other 2 are the last 2 lines in the schema location:

http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

The mvc xmlns and xsi locations were not present earlier, i added them so that the following line works:

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

When i add the above 3 lines, My project does not work. I get the error:

description The requested resource is not available.

If i remove the mvc lines from the beans declaration and run the project w/o images/css the project runs.

Can anyone help me with this? If the above method is not the way to add css and images to a spring mvc project, please point out to the correct way.

Thanks a lot!

Upvotes: 5

Views: 4318

Answers (2)

Deepak Singh
Deepak Singh

Reputation: 460

   webapp/resources/css 
   <head>
  <link href="<c:url value="/resources/css/commoncss.css" />" rel="stylesheet">
  </head>  

if not looking resources folder in webapp then manually create folder as resources/css folder inside webapp put your css file inside css folder.

Upvotes: 0

Kalher
Kalher

Reputation: 3653

You need a small change viz.

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

plus according to standard maven project create a new folder and put your resources in this

src/main/resources

Upvotes: 5

Related Questions