- Mastering Spring 5.0
- Ranga Rao Karanam
- 169字
- 2021-07-02 22:12:20
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 locations on the classpath. The following snippet shows that content within the war can be exposed as static content:
<mvc:resources
mapping="/resources/**"
location="/static-resources/"/>
Things to note are as follows:
- location="/static-resources/": The location specifies the folders inside the war or classpath that you would want to expose as static content. In this example, we want to expose all 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/");
}
}