Spring – CSS problems from No mapping found for HTTP request with URI warning

springspring-mvc

I am having an issue with resolving the path of my CSS files whenever I add anything to my RequestMapping path. In the example below, if I change

@RequestMapping(value="/articles/view", method = RequestMethod.GET)

to

@RequestMapping(value="/articles", method = RequestMethod.GET)

it works fine. I'm also having problems mapping my css files if I attempt to use path variables. My css files are sitting in a folder called /data/css where /data is at the same level as my /WEB-INF.

ArticlesAction.java:

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

@Controller
public class ArticlesAction {

    @RequestMapping(value="/articles/view", method = RequestMethod.GET)
    public String printWelcome(ModelMap model) {

        //model.addAttribute("id", id);
        return "articles";

    }
}

web.xml

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

    <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>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
</web-app>

mvc-dispatcher-servlet.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: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.0.xsd
        http://www.springframework.org/schema/mvc  
        http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">


    <context:component-scan base-package="com.test.actions" />

    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/pages/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
</beans>

articles.jsp

<!DOCTYPE html>
<html class="no-js" lang="en-US">

    <!-- Head section -->
    <head>

        <meta charset="utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
        <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1" />
        <meta name="description" content="Responsive website template by kubasto" />
        <meta name="msapplication-TileImage" content="data/img/tile/w.png" />
        <meta name="msapplication-TileColor" content="#089bc3" />

        <link rel="shortcut icon" href="data/img/favicon/w.png" />
        <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Rokkitt:400,700" />
        <link rel="stylesheet" href="data/css/fancybox.css" />
        <link rel="stylesheet" href="data/css/flexslider.css" />
        <link rel="stylesheet" href="data/css/mejs.css" />
        <link rel="stylesheet" href="data/css/tipsy.css" />
        <link rel="stylesheet" href="data/css/base.css" />
        <link rel="stylesheet" href="data/css/structure.css" />
        <link rel="stylesheet" href="data/css/parts.css" />
        <link rel="stylesheet" href="data/css/widgets.css" />
        <link rel="stylesheet" href="data/css/bright.css" />
        <link rel="stylesheet" href="data/css/color.css" />

Best Answer

Try to use special mvc:resources tag:

<mvc:resources location="/, classpath:/META-INF/web-resources/" mapping="/resources/**"/>

To use it add "mvc" namespace to your mvc-dispatcher-servlet.xml

Related Topic