1

I am trying to use static resources in my sample spring mvc project, In my project I have created a theme under resources folder as shown in the picture.enter image description here

Then I have added the following 3 lines of code in spring-mvc-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-4.1.xsd  
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-4.1.xsd  
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">

   <context:component-scan base-package="com.sudheer.example" />

   <context:annotation-config />

   <bean
       class="org.springframework.web.servlet.view.InternalResourceViewResolver">

       <property name="prefix">
           <value>/WEB-INF/jsp/</value>
       </property>

       <property name="suffix">
           <value>.jsp</value>
       </property>        

   </bean>

   <mvc:resources mapping="/resources/**" location="/resources/theme/" cache-period="31556926"/>
   <mvc:default-servlet-handler/>
    <mvc:annotation-driven />   

</beans>

WhenI try to access the static resources in many differnt ways,I am not able to access it. This is my jsp page:

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<!DOCTYPE html>
<html lang="en">
<head>
<link href="/resources/themes/css/bootstrap.min.css" rel="stylesheet" >

<link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath}/themes/bootstrap.min.css">
<link href="<c:url value="/resources/themes/css/bootstrap.min.css"/>" 
rel="stylesheet"  type="text/css"/>

</head>
<body>
    <h1>1. Test CSS</h1>
    <h2>2. Test JS</h2>
    <div class="btn"></div>
</body>
</html>

And this is my web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>Archetype Created Web Application</display-name>
    <servlet>
        <servlet-name>spring-mvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring-mvc-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>spring-mvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

Can any one help me to solve this issue?

4 Answers 4

4

You have 3 problems:

  • 1: In your Maven project, the folder src/main/resources has a special meaning for Maven: its content will be put in the classpath after compiling.
  • 2: when you use spring resources mapping then the part of the url that mapping that match the mapping part '**' will been added to the location part for obtaining the file. (sorry for the bad explanation) An example will make it more clear: assume you have this resource mapping

     <mvc:resources mapping="/a/**" location="/b/c/" >
    

    And you request the url http://localhost/myApp/a/d then spring will look for the file at /b/c/d! - So when you send an request for ....../resources/themes/css/bootstrap.min.css Spring would search for: /resources/theme/themes/css/bootstrap.min.css

  • 3: in spring config and folder you use theme but in the jsp you use themes


Solution for 1 would be: either you change the location spring resource mapping to pick up the resource from classpath by using classpath: prefix in location attribute or you put your files in an other folder: source/main/webapp/resources/theme (then the location parameter /resources/theme/) would map.

example with classpath fix for 1 and one possible fix for 2:

<mvc:resources mapping="/resources/theme/**"
    location="classpath:/resources/theme/" cache-period="31556926"/>

jsp - fix for 3:

<link href="<c:url value="/resources/theme/css/bootstrap.min.css"/>" 
       rel="stylesheet"  type="text/css"/>
Sign up to request clarification or add additional context in comments.

Comments

2

Probably is this:

You have this configuration

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

But when you call the resourcek, you use this

<c:url value="/resources/themes/css/bootstrap.min.css"/

i htink you have to use this

/resources/css/bootstrap.min.css

Comments

1

If a resource need visible by a frontend, you need put the resources in "webapp" folder. Example: webapp/theme/css/bootstrap.min.css

Comments

1

I have create a skeleton project for Spring MVC (static resources are used also) here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.