Don Subert
Don Subert

Reputation: 2676

In Spring MVC (Pure Annotations), handle static resources like html, css, javascript, and images

In Spring MVC (Pure Annotations, no web.xml), I wish to configure, such that, in addition to using controllers to handle dynamic data, my web app can show static html, javascript, css, and images.

I am using a Maven project structure, so I keep my web source in main/webapp/:

main/
 |-> webapp/
      |-> js/
           |-> something.js
      |-> images/
           |-> image.jpg
      |-> test.html

I currently access my dynamic resources at http://localhost:8080/app-name/**

I have done a lot of research, and tried a few things, but none of them seem to work. The existing examples that I have found seem to leave out crucial pieces of information. For example, they will explain some method of configuration, but then not explain what the folder structure has to be for it to work.

An acceptable answer will explain:

here is my existing configuration code.

WebConfig.java:

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;


@Configuration
@EnableWebMvc
public class WebConfig {
}

MyWebAppInitializer.java:

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

public class MyWebAppInitializer implements WebApplicationInitializer {

  @Override
  public void onStartup(ServletContext servletContext) throws ServletException {
      WebApplicationContext context = getContext();
      servletContext.addListener(new ContextLoaderListener(context));
      ServletRegistration.Dynamic dispatcher = servletContext.addServlet("DispatcherServlet", new DispatcherServlet(context));
      dispatcher.setLoadOnStartup(1);
      dispatcher.addMapping("/*");
  }

  private AnnotationConfigWebApplicationContext getContext() {
      AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
      context.setConfigLocation("com.example.appname.config");
      return context;
  }
}

Upvotes: 1

Views: 716

Answers (2)

Gerardo Martínez
Gerardo Martínez

Reputation: 863

Try this in your WebConfig:

    @Configuration
    @EnableWebMvc
    public class WebConfig extends WebMvcConfigurerAdapter  {
     @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/js/**").addResourceLocations("/js/");
            registry.addResourceHandler("/images/**").addResourceLocations("/images/");
        }
    }

Upvotes: 1

Omkar Puttagunta
Omkar Puttagunta

Reputation: 4156

For handling static resources using pure Java Configuration, your WebConfig class should extend this WebMvcConfigurerAdapter class and override addResourceHandlers method.

You can check these links for more information - 1, 2, 3

Upvotes: 2

Related Questions