diff --git a/pom.xml b/pom.xml index 6e6316edd6..1972350c75 100644 --- a/pom.xml +++ b/pom.xml @@ -692,6 +692,7 @@ spring-mvc-forms-jsp spring-mvc-forms-thymeleaf spring-mvc-java + spring-mvc-java-2 spring-mvc-kotlin spring-mvc-velocity diff --git a/spring-mvc-java-2/.gitignore b/spring-mvc-java-2/.gitignore new file mode 100644 index 0000000000..83c05e60c8 --- /dev/null +++ b/spring-mvc-java-2/.gitignore @@ -0,0 +1,13 @@ +*.class + +#folders# +/target +/neoDb* +/data +/src/main/webapp/WEB-INF/classes +*/META-INF/* + +# Packaged files # +*.jar +*.war +*.ear \ No newline at end of file diff --git a/spring-mvc-java-2/pom.xml b/spring-mvc-java-2/pom.xml new file mode 100644 index 0000000000..d5b7d087ab --- /dev/null +++ b/spring-mvc-java-2/pom.xml @@ -0,0 +1,39 @@ + + + 4.0.0 + spring-mvc-java-2 + 0.1-SNAPSHOT + spring-mvc-java-2 + war + + + com.baeldung + parent-boot-2 + 0.0.1-SNAPSHOT + ../parent-boot-2 + + + + + javax.servlet + javax.servlet-api + ${javax.version} + + + org.springframework + spring-webmvc + ${spring.mvc.version} + + + + + + 4.0.1 + 5.2.2.RELEASE + + + + + \ No newline at end of file diff --git a/spring-mvc-java/src/main/java/com/baeldung/web/controller/CacheControlController.java b/spring-mvc-java-2/src/main/java/com/baeldung/cache/CacheControlController.java similarity index 98% rename from spring-mvc-java/src/main/java/com/baeldung/web/controller/CacheControlController.java rename to spring-mvc-java-2/src/main/java/com/baeldung/cache/CacheControlController.java index f1614ab448..5bf095888e 100644 --- a/spring-mvc-java/src/main/java/com/baeldung/web/controller/CacheControlController.java +++ b/spring-mvc-java-2/src/main/java/com/baeldung/cache/CacheControlController.java @@ -1,4 +1,4 @@ -package com.baeldung.web.controller; +package com.baeldung.cache; import org.springframework.http.CacheControl; import org.springframework.http.ResponseEntity; diff --git a/spring-mvc-java-2/src/main/java/com/baeldung/cache/WebConfig.java b/spring-mvc-java-2/src/main/java/com/baeldung/cache/WebConfig.java new file mode 100644 index 0000000000..728479979c --- /dev/null +++ b/spring-mvc-java-2/src/main/java/com/baeldung/cache/WebConfig.java @@ -0,0 +1,41 @@ +package com.baeldung.cache; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.CacheControl; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +import org.springframework.web.servlet.mvc.WebContentInterceptor; + +import java.util.concurrent.TimeUnit; + +@EnableWebMvc +@Configuration +@ComponentScan(basePackages = {"com.baeldung.cache"}) +public class WebConfig implements WebMvcConfigurer { + + @Override + public void addViewControllers(final ViewControllerRegistry registry) { + registry.addViewController("/").setViewName("index"); + } + + @Override + public void addResourceHandlers(final ResourceHandlerRegistry registry) { + registry.addResourceHandler("/resources/**").addResourceLocations("/resources/") + .setCacheControl(CacheControl.maxAge(60, TimeUnit.SECONDS) + .noTransform() + .mustRevalidate()); + } + + @Override + public void addInterceptors(InterceptorRegistry registry) { + WebContentInterceptor interceptor = new WebContentInterceptor(); + interceptor.addCacheMapping(CacheControl.maxAge(60, TimeUnit.SECONDS) + .noTransform() + .mustRevalidate(), "/cache/*"); + registry.addInterceptor(interceptor); + } +} \ No newline at end of file diff --git a/spring-mvc-java/src/main/webapp/resources/hello.css b/spring-mvc-java-2/src/main/webapp/resources/hello.css similarity index 100% rename from spring-mvc-java/src/main/webapp/resources/hello.css rename to spring-mvc-java-2/src/main/webapp/resources/hello.css diff --git a/spring-mvc-java/src/test/java/com/baeldung/web/controller/CacheControlControllerIntegrationTest.java b/spring-mvc-java-2/src/test/java/com/baeldung/cache/CacheControlControllerIntegrationTest.java similarity index 95% rename from spring-mvc-java/src/test/java/com/baeldung/web/controller/CacheControlControllerIntegrationTest.java rename to spring-mvc-java-2/src/test/java/com/baeldung/cache/CacheControlControllerIntegrationTest.java index b03f628b20..ef408038ae 100644 --- a/spring-mvc-java/src/test/java/com/baeldung/web/controller/CacheControlControllerIntegrationTest.java +++ b/spring-mvc-java-2/src/test/java/com/baeldung/cache/CacheControlControllerIntegrationTest.java @@ -1,6 +1,5 @@ -package com.baeldung.web.controller; +package com.baeldung.cache; -import com.baeldung.spring.web.config.WebConfig; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -16,9 +15,6 @@ import org.springframework.test.web.servlet.result.MockMvcResultMatchers; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; -import java.time.LocalDate; -import java.time.LocalDateTime; - import static org.springframework.http.HttpHeaders.IF_UNMODIFIED_SINCE; @ExtendWith(SpringExtension.class) diff --git a/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebConfig.java b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebConfig.java index 5cc165680f..96e300464b 100644 --- a/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebConfig.java +++ b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebConfig.java @@ -2,7 +2,6 @@ package com.baeldung.spring.web.config; import java.util.ArrayList; import java.util.List; -import java.util.concurrent.TimeUnit; import org.springframework.context.MessageSource; import org.springframework.context.annotation.Bean; @@ -10,19 +9,15 @@ import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Description; import org.springframework.context.support.ResourceBundleMessageSource; -import org.springframework.http.CacheControl; import org.springframework.http.MediaType; import org.springframework.http.converter.ByteArrayHttpMessageConverter; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.EnableWebMvc; -import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.PathMatchConfigurer; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; -import org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping; -import org.springframework.web.servlet.mvc.WebContentInterceptor; import org.springframework.web.servlet.view.InternalResourceViewResolver; import org.springframework.web.servlet.view.JstlView; import org.springframework.web.util.UrlPathHelper; @@ -35,14 +30,14 @@ import com.baeldung.excel.ExcelPOIHelper; @EnableWebMvc @Configuration -@ComponentScan(basePackages = {"com.baeldung.web.controller"}) +@ComponentScan(basePackages = { "com.baeldung.web.controller" }) public class WebConfig implements WebMvcConfigurer { - + @Override public void addViewControllers(final ViewControllerRegistry registry) { registry.addViewController("/").setViewName("index"); } - + @Bean public ViewResolver thymeleafViewResolver() { final ThymeleafViewResolver viewResolver = new ThymeleafViewResolver(); @@ -89,10 +84,7 @@ public class WebConfig implements WebMvcConfigurer { @Override public void addResourceHandlers(final ResourceHandlerRegistry registry) { - registry.addResourceHandler("/resources/**").addResourceLocations("/resources/") - .setCacheControl(CacheControl.maxAge(60, TimeUnit.SECONDS) - .noTransform() - .mustRevalidate()); + registry.addResourceHandler("/resources/**").addResourceLocations("/resources/"); } @Override @@ -129,13 +121,4 @@ public class WebConfig implements WebMvcConfigurer { public ExcelPOIHelper excelPOIHelper() { return new ExcelPOIHelper(); } - - @Override - public void addInterceptors(InterceptorRegistry registry) { - WebContentInterceptor interceptor = new WebContentInterceptor(); - interceptor.addCacheMapping(CacheControl.maxAge(60, TimeUnit.SECONDS) - .noTransform() - .mustRevalidate(), "/cache/*"); - registry.addInterceptor(interceptor); - } } \ No newline at end of file