0
votes

I have a problem about mapping css, js, images to my jsp: My project like this:

src/main/webapp/WEB-INF/web.xml

    <servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

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

<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.css</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.gif</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.png</url-pattern>
</servlet-mapping>

src/main/webapp/WEB-INF/servlet-context.xml

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

src/main/webapp/WEB-INF/jsp/css/style.css

src/main/webapp/WEB-INF/jsp/js/jquery-func.js

src/main/webapp/WEB-INF/jsp/images/sample.jpg

src/main/webapp/WEB-INF/jsp/index.jsp

<head>
    <title>Shop Around</title>
    <meta http-equiv='Content-type' content='text/html; charset=utf-8'/>
    <link rel='stylesheet' href='jsp/css/style.css' type='text/css' media='all'/>
    <script src='jsp/js/jquery-1.4.1.min.js' type='text/javascript'></script>
    <script src='jsp/js/jquery.jcarousel.pack.js' type='text/javascript'></script>
    <script src='jsp/js/jquery-func.js' type='text/javascript'></script>
</head>
1

1 Answers

1
votes

CSS and Javascript resources cannot be contained under WEB-INF, since files under WEB-INF are not accessible externally. As such, the browser will not be able to make requests to obtain them. Put them in folders directly under webapp.

Also, you shouldn't create servlet-mappings for them.