0

I am having trouble linking to a style sheet from a jsp page. I don't know what's wrong. This is my directory structure:

|
|-- WEB-INF
|     |
|     |-- jsp
|          |-- index.jsp
|
|-- resources
      |
      |-- css
           |-- style.css

I tried doing this

<link rel="stylesheet" type="text/css" href="/resources/css/style.css" />

This is my web.xml

<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

Please some help on what am I doing wrong would be appreciated.

Thanks in advance

Marco Aviles
  • 5,516
  • 6
  • 33
  • 47

4 Answers4

1

As you mapped your spring-servlet(Spring) with url "/" all request will redirect to Spring-servlet. Including .css & .js and others too.

Although you find solution, i am providing you another alternative which i used.

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <servlet>
    <servlet-name>static</servlet-name>
    <servlet-class>StaticServlet</servlet-class>
  </servlet>

      <servlet-mapping>
        <servlet-name>static</servlet-name>
        <url-pattern>/images/*</url-pattern>
        <url-pattern>/css/*</url-pattern>
        <url-pattern>/img/*</url-pattern>
        <url-pattern>/js/*</url-pattern>
        <url-pattern>/swf/*</url-pattern>
      </servlet-mapping>

Static Servlet to serve resources

import java.io.FileInputStream;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;

public class StaticServlet extends HttpServlet {

    private static final long serialVersionUID = 8458501870440200891L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        String resourcePath = req.getServletPath() + req.getPathInfo();
        String realPath = getServletContext().getRealPath(resourcePath);
        FileInputStream fileInputStream = new FileInputStream(realPath);
        try {
            IOUtils.copy(fileInputStream, resp.getOutputStream());
        } finally {
            fileInputStream.close();
        }
    }

}
Rohan
  • 266
  • 1
  • 9
0

Your css sheet is under a css directory. Try href="/resources/css/style.css"

km1
  • 2,383
  • 1
  • 22
  • 27
0

I followed this steps, worked like a charm

Community
  • 1
  • 1
Marco Aviles
  • 5,516
  • 6
  • 33
  • 47
0

In your dispatcher-servlet.xml file make-sure following available

xmlns:mvc="http://www.springframework.org/schema/mvc"

xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"

and

then you should be able to access your css via href="/resources/css/style.css"