noHandlerFound in Spring for CSS

Tags: , , ,



I am learning Spring MVC and I try to get some experience. I wrote an app and deployed it to Tomcat serve. At run time, the browser gets a 404 code for the CSS and Tomcat “WARNING [http-nio-8080-exec-12] org.springframework.web.servlet.DispatcherServlet.noHandlerFound No mapping for GET /SpringC5BuildingSpringWebApplication/resources/style.css” Can you please give me some hints of what could be wrong? Below is my code:

App initializer

public class SpittrWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer{

@Override
protected String[] getServletMappings() {
    return new String[] {"/"};
}

@Override
protected Class<?>[] getRootConfigClasses() {
    return new Class<?>[] {RootConfig.class};
}

@Override
protected Class<?>[] getServletConfigClasses() {
    return new Class<?>[] {WebConfig.class};
}

}

WebConfig

@Configuration
@EnableWebMvc
@ComponentScan(basePackageClasses = HomeController.class)        
class WebConfig extends WebMvcConfigurationSupport{
    
    @Bean
    public ViewResolver viewResolver(){
        InternalResourceViewResolver resolver = 
                new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        resolver.setExposeContextBeansAsAttributes(true);
        return resolver;
    }
    
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**")
                .addResourceLocations("/resources/");
    }
}

Controller

@Controller
public class HomeController {
    
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String home(){
        return "home";
    }
}

Webpage

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
    <head>
        <title>Spitter</title>
        <link rel="stylesheet" 
              href="<c:url value="/resources/style.css" />" >
    </head>
    <body>
        <h1>Welcome to Spitter</h1>

        <a href="<c:url value="/spittles" />">Spittles</a> | 
        <a href="<c:url value="/spitter/register" />">Register</a>
    </body>
</html>

WebXML

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:mvc="http://www.springframework.org/schema/mvc"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
            http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"
     version="3.0">
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>

Folder structure

The CSS file is inserted in many places with the home that the server will read it from somewhere. Any hint could be helpful and appreciated! Thank you and I wish you good health!

Answer

The problem was in the WebConfig.class. I need to change the inherited entity. From class WebConfig extends WebMvcConfigurationSupport to public class WebConfig implements WebMvcConfigurer.



Source: stackoverflow