3

I am developing an web application in Spring MVC Framework with Eclipse Europa IDE and Tomcat server

My directory structure is(partial):

- WebContent  
    - WEB-INF  
        - pages  
            - CSS  
            - images  
            - login.html  
        - dispatcher-servlet.xml  
        - web.xml  

In my login.html I have a link to a css file as

<link rel="stylesheet" type="text/css" href="CSS/loginregister.css" media="screen">

Since my login.html and CSS folder are in the same folder "pages" I thought this would work.
But NO. The css file didn't load (login.html appears smoothly)

I also tried:

<href="/CSS/loginregister.css">
<href="pages/CSS/loginregister.css">

I am accessing the login.html via my controller class.
my URL comes like this: http://localhost:8080/AccountCreation/login.htm

Can any one tell me where is the problem ?

skaffman
  • 398,947
  • 96
  • 818
  • 769
mukund
  • 2,866
  • 5
  • 31
  • 41
  • Related/duplicate: http://stackoverflow.com/questions/3655316/browser-cant-access-css-and-images-when-calling-a-servlet-which-forwards-to-a-j – BalusC May 18 '12 at 20:22

1 Answers1

5

Move your "pages" folder to be under WebContent instead of WEB-INF. JSP pages shold be protected under WEB-INF but not static files like css, js, html.

The root of your application context is WebContent which would be referenced as

http://server.com/contextroot/

Best practise would say to create a folder WebContent/css and place your file loginregister.css in there. Your link will then be

<link rel="stylesheet" type="text/css" href="css/loginregister.css" media="screen"> 

To test, you can simply enter the following URL in your browser to ensure that the css file is being served from your context root as expected

http://server.com/contextroot/css/loginregister.css

[EDIT]

For your initial testing, also place your login.html in WebContent (e.g. WebContent/login.html) as well, however you have mentioned you're using Spring so you'll probably end up replacing login.html with an equivalent JSP.

http://server.com/contextroot/login.html
Brad
  • 15,186
  • 11
  • 60
  • 74