3

I have a SpringMVC project. Upon running the project in tomcat7 I get the following message:

No mapping found for HTTP request with URI [/myproject/resources/css/welcome.css] in DispatcherServlet with name 'DispatcherServlet'

CSS does not get applied:

enter image description here

I've tried available solutions but none of them worked and none of them actually explained what was going on.

I have a simple welcome.jsp in /WEB-INF/jsp. Here it is:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link type="text/css"
    href="${pageContext.request.contextPath}/resources/css/welcome.css"
    rel="stylesheet">
<title>Welcome!</title>
</head>
<body>
    <div id="font-HarabaraHand">
        <p class="centrify">Poem Collection</p>
    </div>
    <div id="font-Goodfoot">
        <p class="centrify2">What would you like to do?</p>
    </div>

</body>
</html>

My welcome.css file lies in src/main/webapp/resources/css/. My Spring configuration is java based, DispatcherServlet class is in AppInit.java file:

public class AppInit implements WebApplicationInitializer {
    // this method is automatically invoked on application startup
    public void onStartup(ServletContext container) throws ServletException {
        WebApplicationContext webcntx = getWebCntx();

        container.addListener(new ContextLoaderListener(webcntx));

        ServletRegistration.Dynamic dispatcher = container.addServlet(
                "DispatcherServlet", new DispatcherServlet(webcntx));

        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");
    }

    private AnnotationConfigWebApplicationContext getWebCntx() {
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.register(WebAppConfiguration.class);
        return context;
    }
}

And finally in WebAppConfig.java file:

@Bean
    public InternalResourceViewResolver getInternalResourceViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/jsp/");
        resolver.setSuffix(".jsp");
        resolver.setViewClass(JstlView.class);
        return resolver;
    }

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    super.addResourceHandlers(registry);
    if (!registry.hasMappingForPattern("/resources/**")) {
        registry.addResourceHandler("/resources/**").addResourceLocations(
                "/resources/").setCachePeriod(CACHE_PERIOD);;

    }
}

My welcome page controller:

@Controller
public class WelcomeController {

    @RequestMapping(value="/")
    public ModelAndView welcomePage() {
        return new ModelAndView("welcome");
    }
}

Here is my project structure:

upper part welcome.jsp file

1. Why my welcome.css file cannot be reached?

2. How I can correct my mappings configuration?

I would really appreciate an explanation or a link about Spring url mappings config.

6
  • Try href="${pageContext.request.contextPath}/resources/welcome.css" and let know
    – geoand
    May 19, 2014 at 7:03
  • Sorry, your variant does not work. My folder structure is src/main/webapp/resources/css/welcome.css
    – minerals
    May 19, 2014 at 7:06
  • then just try to check that weather you giving path is indicating that file of not try (f.exits) if that is true then file is getting path May 19, 2014 at 7:09
  • Do not put your jsp files into WEB-INF! They should be in the src/main/webapp folder.
    – Roman K
    May 19, 2014 at 7:19
  • @kromit why? I read exactly the opposite. In WEB-INF folder .jsp files are not accessible from the client side, while in other folders they are. My project structure is ok, there are plenty tutorials with the same setup, I thought that maybe my mappings are wrong.
    – minerals
    May 19, 2014 at 7:31

2 Answers 2

4

By using Java Config:

  1. Create the following subdirectory structure: src/main/webapp/public/css

  2. Put your css files in src/main/webapp/public/css (e.g. src/main/webapp/public/css/welcome.css)

  3. Define an handler for public resources:

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        super.addResourceHandlers(registry);
        if (!registry.hasMappingForPattern("/public/**")) {
            registry.addResourceHandler("/public/**")
                    .addResourceLocations("/public/")
                    .setCachePeriod(CACHE_PERIOD); // You have to define a cache period.
        }
    }
    
  4. Add a security exception:

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/public/**");
    }
    
  5. Link you css files (by defining a relative path):

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
    ...
    <head>
        <c:set var="url">${pageContext.request.requestURL}</c:set>
        <base href="${fn:substring(url, 0, fn:length(url) - fn:length(pageContext.request.requestURI))}${pageContext.request.contextPath}/" />
        <link rel="stylesheet" href="public/css/welcome.css" />
    </head>
    
4
  • this looks a way more complicated than I thought it would be, why? Just take a look mkyong.com/spring-mvc/…
    – minerals
    May 19, 2014 at 8:49
  • If you are using Spring with Java Config, it represents a reliable way to set up your application.
    – vdenotaris
    May 19, 2014 at 9:03
  • tried it, didn't help. welcome.css file does not get applied.
    – minerals
    May 20, 2014 at 5:08
  • You did something wrong. The solution above results well-tested on an enterprise project (just as is).
    – vdenotaris
    Jul 31, 2014 at 20:11
0

@vdenotaris answer is correct

My configuration didn't work because of my pom.xml file setup. maven-war-plugin had <warSourceDirectory>WebContent</warSourceDirectory> and my /src/main/webapp/resources/css has never been detected by maven. Once I moved my jsp files to /src/main/webapp/ all was ok.

So, either you leave everything (css resources and other stuff) in WebContent\WEB-INF\ folder or move everything to /src/main/webapp

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.