3

I have read several post in stackoverflow about configurations and mappings to serve static content like js and css but I can not make it work.

I have the following files:

-> POM.XML

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>es.landeSoft</groupId>
        <artifactId>MvcTesting</artifactId>
        <name>SpringMVCTesting</name>
        <packaging>war</packaging>
        <version>1.0.0-BUILD-SNAPSHOT</version>
        <properties>
            <java-version>1.6</java-version>
            <org.springframework-version>3.1.1.RELEASE</org.springframework-version>
            <org.aspectj-version>1.6.10</org.aspectj-version>
            <org.slf4j-version>1.6.6</org.slf4j-version>
        </properties>
        <dependencies>
            <!-- Spring -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${org.springframework-version}</version>
                <exclusions>
                    <!-- Exclude Commons Logging in favor of SLF4j -->
                    <exclusion>
                        <groupId>commons-logging</groupId>
                        <artifactId>commons-logging</artifactId>
                     </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${org.springframework-version}</version>
            </dependency>

            <!-- AspectJ -->
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjrt</artifactId>
                <version>${org.aspectj-version}</version>
            </dependency>   

            <!-- Logging -->
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>${org.slf4j-version}</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>jcl-over-slf4j</artifactId>
                <version>${org.slf4j-version}</version>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
                <version>${org.slf4j-version}</version>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>1.2.15</version>
                <exclusions>
                    <exclusion>
                        <groupId>javax.mail</groupId>
                        <artifactId>mail</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>javax.jms</groupId>
                        <artifactId>jms</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>com.sun.jdmk</groupId>
                        <artifactId>jmxtools</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>com.sun.jmx</groupId>
                        <artifactId>jmxri</artifactId>
                    </exclusion>
                </exclusions>
                <scope>runtime</scope>
            </dependency>

            <!-- @Inject -->
            <dependency>
                <groupId>javax.inject</groupId>
                <artifactId>javax.inject</artifactId>
                <version>1</version>
            </dependency>

            <!-- Servlet -->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>servlet-api</artifactId>
                <version>2.5</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>javax.servlet.jsp</groupId>
                <artifactId>jsp-api</artifactId>
                <version>2.1</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jstl</artifactId>
                <version>1.2</version>
            </dependency>

            <!-- Test -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.7</version>
                <scope>test</scope>
            </dependency>        
        </dependencies>
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-eclipse-plugin</artifactId>
                    <version>2.9</version>
                    <configuration>
                        <additionalProjectnatures>
                            <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
                        </additionalProjectnatures>
                        <additionalBuildcommands>
                            <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
                        </additionalBuildcommands>
                        <downloadSources>true</downloadSources>
                        <downloadJavadocs>true</downloadJavadocs>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>2.5.1</version>
                    <configuration>
                        <source>1.6</source>
                        <target>1.6</target>
                        <compilerArgument>-Xlint:all</compilerArgument>
                        <showWarnings>true</showWarnings>
                        <showDeprecation>true</showDeprecation>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <version>1.2.1</version>
                    <configuration>
                        <mainClass>org.test.int1.Main</mainClass>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>

--> WEB.XML

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Processes application requests -->
    <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/spring/appServlet/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>

</web-app>

--> SERVLET-CONTEXT.XML

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

        <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->


            <!-- Enables the Spring MVC @Controller programming model -->
        <mvc:annotation-driven />   

        <!-- Handles HTTP GET requests for /resources/ by efficiently serving up static resources in the ${webappRoot}/resources directory -->

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


        <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/views/" />
            <property name="suffix" value=".jsp" />
        </bean>

        <context:component-scan base-package="es.landeSoft.MvcTesting" />

    </beans>

As you can see in servlet-context I have declared the following line:

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->

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

And I have created a css file inside:

src/Main/webapp/resources/css/styles.css

Inside my jsp I reference it like:

href="<c:url value="/resources/css/styles.css" />" 

It renders like this:

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

But I am getting :

GET http://localhost:8081/MvcTesting/resources/css/styles.css 405 (Method not allowed)

I tried adding servlet-mappings in web.xml but I want to make it work using the new tag

And It works, but I do not want to do it like this. the

When tomcat is starting I can read the following line:

INFO : org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Mapped URL path [/resources/**] onto handler 'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0'

.

enter image description here

EDIT:

I'm trying using:

<mvc:annotation-driven />
<mvc:default-servlet-handler/>

NO luck

9
  • Can you post your web.xml file? Commented Nov 24, 2013 at 11:26
  • sorry I forgot. Updated Commented Nov 24, 2013 at 11:28
  • I am having trouble seeing your problem. I have configured many projects in the exact manner you provided. Does this occur for all resource files? One thing I have noticed is that you need to make sure the XML files do not wrap any configuration, extra whitespace in those files causes issues and they are a bear to detect. Commented Nov 24, 2013 at 11:38
  • I see the following line: INFO : org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Mapped URL path [/resources/**] onto handler 'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0'. Do I need to register the ResourceHttpRequestHandler somewhere?. Thanks for your comment Kevin. This is driving me nuts Commented Nov 24, 2013 at 11:40
  • Updated the web.xml. It was not showing. Commented Nov 24, 2013 at 11:41

4 Answers 4

4

I had the same problem, below code, worked for me. Try to add this to your web.xml

<servlet-mapping>
  <servlet-name>default</servlet-name>
  <url-pattern>*.js</url-pattern>
</servlet-mapping>

<servlet-mapping>
  <servlet-name>default</servlet-name>
  <url-pattern>*.css</url-pattern>
</servlet-mapping>
Sign up to request clarification or add additional context in comments.

Comments

1

A ResourceHttpRequestHandler class' instance cannot produce a 405 Method Not Allowed response.

You either have a security layer wrapping your requests (doubtful) or you have a @Controller handler method mapped to / or /* that handles a request method other than GET.

Since @RequestMapping handler methods have higher priority than any ResourceHttpRequestHandler mappings, the handler mapped to /* which can handle /MvcTesting/resources/css/styles.css will be used for handling that request and will respond with 405 because the path is good but the request method isn't.

Comments

0

You can try mapping <url-pattern>*.css</url-pattern> with DispatcherServlet in web.xml like

<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
    <url-pattern>*.css</url-pattern>
</servlet-mapping>

1 Comment

I have read this would make the servlet to serve the static content and there is not need for this. THat is why the <mvc:resource was created
0

I suspect you may not be accessing the .jsp file through a controller. Try adding in this controller and accessing through the .jsp through this mapping.

Controller

package es.landeSoft.MvcTesting;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class LoginController {

    @RequestMapping("/")
    public String login(){
        return "login";     
    }
}

To see a full working example view this GitHub repository.

2 Comments

Can you explain why this would make a difference?
@SotiriosDelimanolis Your right this wouldn't. I think the provided example could be of value though.

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.