Spring MVC Cache Control is an extension to Spring MVC that aims to simplify implementing HTTP/1.1 Cache-Control headers for annotated MVC controllers.
<dependency>
<groupId>net.rossillo.mvc.cache</groupId>
<artifactId>spring-mvc-cache-control</artifactId>
<version>1.1.1-RELEASE</version>
<scope>compile</scope>
</dependency>
compile 'net.rossillo.mvc.cache:spring-mvc-cache-control:1.1.1-RELEASE'
Simply include net.rossillo.spring.web.mvc.CacheControlHandlerInterceptor
in your Spring MVC configuration.
<mvc:interceptors>
<bean class="net.rossillo.spring.web.mvc.CacheControlHandlerInterceptor" />
</mvc:interceptors>
For XML contexts, this usually defaults to ${appName}-servlet.xml
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter implements WebMvcConfigurer
{
@Override
public void addInterceptors(InterceptorRegistry registry)
{
registry.addInterceptor(new CacheControlHandlerInterceptor());
}
}
Use the @CacheControl
annotation on either (or both) type level @Controller
s or method level @RequestMapping
s. The handler interceptor will read the annotations and generate HTTP/1.1 compliant cache-control headers. For example:
@Controller
public final class DemoController {
/**
* Public home page, cacheable for 5 minutes.
*/
@CacheControl(maxAge = 300)
@RequestMapping({"/", "/home.do"})
public String handleHomePageRequest(Model model) {
...
}
}
See our spring-mvc-cache-control-demo project for full details.
-
Clone the repository from GitHub:
$ git clone git://github.com/foo4u/spring-mvc-cache-control
-
Navigate into the cloned repository directory:
$ cd spring-mvc-cache-control
-
The project uses Gradle to build:
$ ./gradlew build
IDEA 13+ natively support Gralde projects. Simply choose to import an existing project and select the build.gradle file. Tick the checkbox to use the Gradle wrapper.
To generate Eclipse metadata (.classpath and .project files), use the following Gradle task:
$ ./gradlew eclipse
Once complete, you may then import the projects into Eclipse as usual:
File -> Import -> Existing projects into workspace
Use the following Gradle task to build the JavaDoc
$ ./gradlew javadoc
Note: The result will be available in 'spring-mvc-cache-control/build/docs/javadoc'.
Contributions are always welcome. Fork the repository, create a topic branch and send a pull request.