Exposing static content

Web applications typically have a lot of static content. Spring MVC provides options to expose static content from folders on the web application root, as well as locations on the classpath. The following snippet shows that content within war can be exposed as static content:

    <mvc:resources  
mapping="/resources/**"
location="/static-resources/"/>

Elements to note include the following:

  • location="/static-resources/": The location specifies the folders inside war or the classpath that you would want to expose as static content. In this example, we want to expose all of the content in the static-resources folder inside the root of war as static content. We can specify multiple comma-separated values to expose multiple folders under the same external facing URI.
  • mapping="/resources/**": The mapping specifies the external facing URI path. So, a CSS file named app.css inside the static resources folder can be accessed using the /resources/app.css URI.

The complete Java configuration for the same configuration is shown here:

    @Configuration 
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

@Override
public void addResourceHandlers (ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/static-resources/**")
.addResourceLocations("/static-resources/");
}
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset