ryno_
ryno_

Reputation: 13

Spring MVC with Java based config - 404 not found for static resources

I'm trying out Spring MVC with java config and I'm getting 404 not found for static resources, the folder of which I can confirm is definitively getting published to the server. It seems like the resources are not being resolved at all. So even when calling localhost:8080/SpringMVC/resources/js/jquery-1.4.2.min.js I get 404 not found. I'm using Spring 4.0.1

Deployed structure

SpringMVC/
   resources/
      css/
      js/
         jquery-1.4.2.min.js  
   WEB-INF/
      classes/
      lib/
      views/ 
         services.jsp
      web.xml

My Code:

com/me/config/WebApplicationConfig.java

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"com.me.services"})
public class WebApplicationConfig extends WebMvcConfigurerAdapter {

    private static final String VIEW_RESOLVER_SUFFIX = ".jsp";
    private static final String VIEW_RESOLVER_PREFIX = "/WEB-INF/views/";

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    }

    @Bean
    public ViewResolver setupViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();

        resolver.setPrefix(VIEW_RESOLVER_PREFIX);
        resolver.setSuffix(VIEW_RESOLVER_SUFFIX );
        resolver.setViewClass(JstlView.class);

        return resolver;
    }
}

com/me/config/Initializer.java

public class Initializer implements WebApplicationInitializer {

    private static final String DISPATCHER_SERVLET_MAPPING = "/";
    private static final String DISPATCHER_SERVLET_NAME = "dispatcher";

    public void onStartup(ServletContext servletContext) throws ServletException {

        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        rootContext.register(WebApplicationConfig.class);
        rootContext.setServletContext(servletContext);

        Dynamic servlet = servletContext.addServlet(DISPATCHER_SERVLET_NAME, new DispatcherServlet(rootContext));
        servlet.addMapping(DISPATCHER_SERVLET_MAPPING );
        servlet.setLoadOnStartup(1);
    }
}

com/me/controller/ServicesController.java

@Controller
public class ServicesController {

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String services(ModelMap model) {
        return "services";
    }
}

WEB-INF/views/services.jsp

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>    
<script src="<c:url value="/resources/js/jquery-1.4.2.min.js" />"></script>
</head>
<body>
    <h2>Services</h2>
</body>
</html>

GET Request Response when calling localhost:8080/SpringMVC/:

<html>
<head>
<script src="/SpringMVC/resources/js/jquery-1.4.2.min.js"></script>
</head>
<body>
    <h2>Services</h2>
</body>
</html>

And then after that 404 Not Found on:

localhost:8080/SpringMVC/resources/js/jquery-1.4.2.min.js

Upvotes: 1

Views: 5101

Answers (1)

Sarah
Sarah

Reputation: 525

I think in your services.jsp, could you try with single quotes:

<script src="<c:url value='/resources/js/jquery-1.4.2.min.js' />"></script>

It seems like a problem with 2 sets of double quotes in <script>.

Upvotes: 6

Related Questions