diff --git a/spring-mvc-java-2/README.md b/spring-mvc-java-2/README.md index b5d5df3cd4..09c8d8b294 100644 --- a/spring-mvc-java-2/README.md +++ b/spring-mvc-java-2/README.md @@ -1,3 +1,6 @@ ### Relevant Articles: - [Cache Headers in Spring MVC](https://www.baeldung.com/spring-mvc-cache-headers) +- [Working with Date Parameters in Spring](https://www.baeldung.com/spring-date-parameters) +- [Spring MVC @PathVariable with a dot (.) gets truncated](https://www.baeldung.com/spring-mvc-pathvariable-dot) +- [A Quick Guide to Spring MVC Matrix Variables](https://www.baeldung.com/spring-mvc-matrix-variables) \ No newline at end of file diff --git a/spring-mvc-java-2/pom.xml b/spring-mvc-java-2/pom.xml index d5b7d087ab..af622321cb 100644 --- a/spring-mvc-java-2/pom.xml +++ b/spring-mvc-java-2/pom.xml @@ -7,14 +7,14 @@ 0.1-SNAPSHOT spring-mvc-java-2 war - + com.baeldung parent-boot-2 0.0.1-SNAPSHOT ../parent-boot-2 - + javax.servlet @@ -26,14 +26,27 @@ spring-webmvc ${spring.mvc.version} - + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + - + + + spring-mvc-java-2 + + + src/main/resources + true + + + + 4.0.1 5.2.2.RELEASE - \ No newline at end of file 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/CacheWebConfig.java similarity index 96% rename from spring-mvc-java-2/src/main/java/com/baeldung/cache/WebConfig.java rename to spring-mvc-java-2/src/main/java/com/baeldung/cache/CacheWebConfig.java index 2f07912e80..95367077bd 100644 --- a/spring-mvc-java-2/src/main/java/com/baeldung/cache/WebConfig.java +++ b/spring-mvc-java-2/src/main/java/com/baeldung/cache/CacheWebConfig.java @@ -15,7 +15,7 @@ import java.util.concurrent.TimeUnit; @EnableWebMvc @Configuration @ComponentScan(basePackages = {"com.baeldung.cache"}) -public class WebConfig implements WebMvcConfigurer { +public class CacheWebConfig implements WebMvcConfigurer { @Override public void addViewControllers(final ViewControllerRegistry registry) { diff --git a/spring-mvc-java/src/main/java/com/baeldung/datetime/DateTimeConfig.java b/spring-mvc-java-2/src/main/java/com/baeldung/datetime/DateTimeConfig.java similarity index 100% rename from spring-mvc-java/src/main/java/com/baeldung/datetime/DateTimeConfig.java rename to spring-mvc-java-2/src/main/java/com/baeldung/datetime/DateTimeConfig.java diff --git a/spring-mvc-java/src/main/java/com/baeldung/datetime/DateTimeController.java b/spring-mvc-java-2/src/main/java/com/baeldung/datetime/DateTimeController.java similarity index 100% rename from spring-mvc-java/src/main/java/com/baeldung/datetime/DateTimeController.java rename to spring-mvc-java-2/src/main/java/com/baeldung/datetime/DateTimeController.java diff --git a/spring-mvc-java-2/src/main/java/com/baeldung/matrix/config/MatrixWebConfig.java b/spring-mvc-java-2/src/main/java/com/baeldung/matrix/config/MatrixWebConfig.java new file mode 100644 index 0000000000..489740fd33 --- /dev/null +++ b/spring-mvc-java-2/src/main/java/com/baeldung/matrix/config/MatrixWebConfig.java @@ -0,0 +1,18 @@ +package com.baeldung.matrix.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.PathMatchConfigurer; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +import org.springframework.web.util.UrlPathHelper; + +@Configuration +public class MatrixWebConfig implements WebMvcConfigurer { + + @Override + public void configurePathMatch(PathMatchConfigurer configurer) { + final UrlPathHelper urlPathHelper = new UrlPathHelper(); + urlPathHelper.setRemoveSemicolonContent(false); + + configurer.setUrlPathHelper(urlPathHelper); + } +} diff --git a/spring-mvc-java/src/main/java/com/baeldung/web/controller/CompanyController.java b/spring-mvc-java-2/src/main/java/com/baeldung/matrix/controller/CompanyController.java similarity index 81% rename from spring-mvc-java/src/main/java/com/baeldung/web/controller/CompanyController.java rename to spring-mvc-java-2/src/main/java/com/baeldung/matrix/controller/CompanyController.java index af1e729c13..7a21ded026 100644 --- a/spring-mvc-java/src/main/java/com/baeldung/web/controller/CompanyController.java +++ b/spring-mvc-java-2/src/main/java/com/baeldung/matrix/controller/CompanyController.java @@ -1,23 +1,16 @@ -package com.baeldung.web.controller; - -import java.util.HashMap; -import java.util.Map; +package com.baeldung.matrix.controller; +import com.baeldung.matrix.model.Company; import org.springframework.http.HttpStatus; -import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.validation.BindingResult; -import org.springframework.web.bind.annotation.MatrixVariable; -import org.springframework.web.bind.annotation.ModelAttribute; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.*; import org.springframework.web.servlet.ModelAndView; -import com.baeldung.model.Company; +import java.util.HashMap; +import java.util.Map; @Controller public class CompanyController { diff --git a/spring-mvc-java/src/main/java/com/baeldung/web/controller/EmployeeController.java b/spring-mvc-java-2/src/main/java/com/baeldung/matrix/controller/EmployeeController.java similarity index 86% rename from spring-mvc-java/src/main/java/com/baeldung/web/controller/EmployeeController.java rename to spring-mvc-java-2/src/main/java/com/baeldung/matrix/controller/EmployeeController.java index 251287dff8..3f9de2179a 100644 --- a/spring-mvc-java/src/main/java/com/baeldung/web/controller/EmployeeController.java +++ b/spring-mvc-java-2/src/main/java/com/baeldung/matrix/controller/EmployeeController.java @@ -1,32 +1,22 @@ -package com.baeldung.web.controller; +package com.baeldung.matrix.controller; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; +import com.baeldung.matrix.model.Employee; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.validation.BindingResult; -import org.springframework.web.bind.annotation.MatrixVariable; -import org.springframework.web.bind.annotation.ModelAttribute; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.ResponseBody; -import org.springframework.web.bind.annotation.SessionAttributes; +import org.springframework.web.bind.annotation.*; import org.springframework.web.servlet.ModelAndView; -import com.baeldung.model.Employee; +import java.util.*; @SessionAttributes("employees") @Controller public class EmployeeController { - Map employeeMap = new HashMap<>(); + public Map employeeMap = new HashMap<>(); @ModelAttribute("employees") public void initEmployees() { diff --git a/spring-mvc-java/src/main/java/com/baeldung/model/Company.java b/spring-mvc-java-2/src/main/java/com/baeldung/matrix/model/Company.java similarity index 94% rename from spring-mvc-java/src/main/java/com/baeldung/model/Company.java rename to spring-mvc-java-2/src/main/java/com/baeldung/matrix/model/Company.java index 558507268a..cdf6cb0fd6 100644 --- a/spring-mvc-java/src/main/java/com/baeldung/model/Company.java +++ b/spring-mvc-java-2/src/main/java/com/baeldung/matrix/model/Company.java @@ -1,4 +1,4 @@ -package com.baeldung.model; +package com.baeldung.matrix.model; public class Company { diff --git a/spring-mvc-java/src/main/java/com/baeldung/model/Employee.java b/spring-mvc-java-2/src/main/java/com/baeldung/matrix/model/Employee.java similarity index 97% rename from spring-mvc-java/src/main/java/com/baeldung/model/Employee.java rename to spring-mvc-java-2/src/main/java/com/baeldung/matrix/model/Employee.java index fb0a452219..c3384122b4 100644 --- a/spring-mvc-java/src/main/java/com/baeldung/model/Employee.java +++ b/spring-mvc-java-2/src/main/java/com/baeldung/matrix/model/Employee.java @@ -1,4 +1,4 @@ -package com.baeldung.model; +package com.baeldung.matrix.model; import javax.xml.bind.annotation.XmlRootElement; diff --git a/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/CustomWebMvcConfigurationSupport.java b/spring-mvc-java-2/src/main/java/com/baeldung/pathvariable/CustomWebMvcConfigurationSupport.java similarity index 93% rename from spring-mvc-java/src/main/java/com/baeldung/spring/web/config/CustomWebMvcConfigurationSupport.java rename to spring-mvc-java-2/src/main/java/com/baeldung/pathvariable/CustomWebMvcConfigurationSupport.java index a0dd7358d0..12c208c623 100644 --- a/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/CustomWebMvcConfigurationSupport.java +++ b/spring-mvc-java-2/src/main/java/com/baeldung/pathvariable/CustomWebMvcConfigurationSupport.java @@ -1,4 +1,4 @@ -package com.baeldung.spring.web.config; +package com.baeldung.pathvariable; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.PathMatchConfigurer; diff --git a/spring-mvc-java/src/main/java/com/baeldung/web/controller/SiteController.java b/spring-mvc-java-2/src/main/java/com/baeldung/pathvariable/SiteController.java similarity index 69% rename from spring-mvc-java/src/main/java/com/baeldung/web/controller/SiteController.java rename to spring-mvc-java-2/src/main/java/com/baeldung/pathvariable/SiteController.java index 3867380665..493161b0eb 100644 --- a/spring-mvc-java/src/main/java/com/baeldung/web/controller/SiteController.java +++ b/spring-mvc-java-2/src/main/java/com/baeldung/pathvariable/SiteController.java @@ -1,27 +1,30 @@ -package com.baeldung.web.controller; +package com.baeldung.pathvariable; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; +@RestController @RequestMapping("/site") public class SiteController { - @RequestMapping(value = "/{firstValue}/{secondValue}", method = RequestMethod.GET) + @GetMapping("/{firstValue}/{secondValue}") public String requestWithError(@PathVariable("firstValue") String firstValue, @PathVariable("secondValue") String secondValue) { return firstValue + " - " + secondValue; } - @RequestMapping(value = "/{firstValue}/{secondValue:.+}", method = RequestMethod.GET) + @GetMapping("/{firstValue}/{secondValue:.+}") public String requestWithRegex(@PathVariable("firstValue") String firstValue, @PathVariable("secondValue") String secondValue) { return firstValue + " - " + secondValue; } - @RequestMapping(value = "/{firstValue}/{secondValue}/", method = RequestMethod.GET) + @GetMapping("/{firstValue}/{secondValue}/") public String requestWithSlash(@PathVariable("firstValue") String firstValue, @PathVariable("secondValue") String secondValue) { diff --git a/spring-mvc-java-2/src/main/webapp/WEB-INF/mvc-servlet.xml b/spring-mvc-java-2/src/main/webapp/WEB-INF/mvc-servlet.xml new file mode 100644 index 0000000000..00dac5f8cb --- /dev/null +++ b/spring-mvc-java-2/src/main/webapp/WEB-INF/mvc-servlet.xml @@ -0,0 +1,27 @@ + + + + + + + + /WEB-INF/view/ + + + .jsp + + + + + \ No newline at end of file diff --git a/spring-mvc-java/src/main/webapp/WEB-INF/view/companyHome.jsp b/spring-mvc-java-2/src/main/webapp/WEB-INF/view/companyHome.jsp similarity index 100% rename from spring-mvc-java/src/main/webapp/WEB-INF/view/companyHome.jsp rename to spring-mvc-java-2/src/main/webapp/WEB-INF/view/companyHome.jsp diff --git a/spring-mvc-java/src/main/webapp/WEB-INF/view/companyView.jsp b/spring-mvc-java-2/src/main/webapp/WEB-INF/view/companyView.jsp similarity index 100% rename from spring-mvc-java/src/main/webapp/WEB-INF/view/companyView.jsp rename to spring-mvc-java-2/src/main/webapp/WEB-INF/view/companyView.jsp diff --git a/spring-mvc-java/src/main/webapp/WEB-INF/view/employeeHome.jsp b/spring-mvc-java-2/src/main/webapp/WEB-INF/view/employeeHome.jsp similarity index 100% rename from spring-mvc-java/src/main/webapp/WEB-INF/view/employeeHome.jsp rename to spring-mvc-java-2/src/main/webapp/WEB-INF/view/employeeHome.jsp diff --git a/spring-mvc-java/src/main/webapp/WEB-INF/view/employeeView.jsp b/spring-mvc-java-2/src/main/webapp/WEB-INF/view/employeeView.jsp similarity index 100% rename from spring-mvc-java/src/main/webapp/WEB-INF/view/employeeView.jsp rename to spring-mvc-java-2/src/main/webapp/WEB-INF/view/employeeView.jsp diff --git a/spring-mvc-java-2/src/main/webapp/WEB-INF/web.xml b/spring-mvc-java-2/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000000..86a24e7646 --- /dev/null +++ b/spring-mvc-java-2/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,22 @@ + + + Spring MVC Application 2 + + + mvc + + org.springframework.web.servlet.DispatcherServlet + + + contextConfigLocation + /WEB-INF/mvc-servlet.xml + + 1 + + + + mvc + / + + \ No newline at end of file diff --git a/spring-mvc-java-2/src/test/java/com/baeldung/cache/CacheControlControllerIntegrationTest.java b/spring-mvc-java-2/src/test/java/com/baeldung/cache/CacheControlControllerIntegrationTest.java index 7acfe5e480..1e34dd182b 100644 --- a/spring-mvc-java-2/src/test/java/com/baeldung/cache/CacheControlControllerIntegrationTest.java +++ b/spring-mvc-java-2/src/test/java/com/baeldung/cache/CacheControlControllerIntegrationTest.java @@ -19,7 +19,7 @@ import static org.springframework.http.HttpHeaders.IF_UNMODIFIED_SINCE; @ExtendWith(SpringExtension.class) @WebAppConfiguration -@ContextConfiguration(classes = {WebConfig.class, WebConfig.class}) +@ContextConfiguration(classes = {CacheWebConfig.class, CacheWebConfig.class}) public class CacheControlControllerIntegrationTest { @Autowired diff --git a/spring-mvc-java/src/test/java/com/baeldung/web/controller/EmployeeMvcIntegrationTest.java b/spring-mvc-java-2/src/test/java/com/baeldung/matrix/EmployeeMvcIntegrationTest.java similarity index 81% rename from spring-mvc-java/src/test/java/com/baeldung/web/controller/EmployeeMvcIntegrationTest.java rename to spring-mvc-java-2/src/test/java/com/baeldung/matrix/EmployeeMvcIntegrationTest.java index 86420a5fbd..c061c1efc7 100644 --- a/spring-mvc-java/src/test/java/com/baeldung/web/controller/EmployeeMvcIntegrationTest.java +++ b/spring-mvc-java-2/src/test/java/com/baeldung/matrix/EmployeeMvcIntegrationTest.java @@ -1,11 +1,7 @@ -package com.baeldung.web.controller; - -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; +package com.baeldung.matrix; +import com.baeldung.matrix.config.MatrixWebConfig; +import com.baeldung.matrix.controller.EmployeeController; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -18,11 +14,13 @@ import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; -import com.baeldung.spring.web.config.WebConfig; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration -@ContextConfiguration(classes = WebConfig.class) +@ContextConfiguration(classes = { MatrixWebConfig.class, EmployeeController.class }) public class EmployeeMvcIntegrationTest { @Autowired diff --git a/spring-mvc-java/src/test/java/com/baeldung/web/controller/EmployeeNoMvcIntegrationTest.java b/spring-mvc-java-2/src/test/java/com/baeldung/matrix/EmployeeNoMvcIntegrationTest.java similarity index 86% rename from spring-mvc-java/src/test/java/com/baeldung/web/controller/EmployeeNoMvcIntegrationTest.java rename to spring-mvc-java-2/src/test/java/com/baeldung/matrix/EmployeeNoMvcIntegrationTest.java index e84c20c973..2ca70cc0b9 100644 --- a/spring-mvc-java/src/test/java/com/baeldung/web/controller/EmployeeNoMvcIntegrationTest.java +++ b/spring-mvc-java-2/src/test/java/com/baeldung/matrix/EmployeeNoMvcIntegrationTest.java @@ -1,5 +1,8 @@ -package com.baeldung.web.controller; +package com.baeldung.matrix; +import com.baeldung.matrix.config.MatrixWebConfig; +import com.baeldung.matrix.controller.EmployeeController; +import com.baeldung.matrix.model.Employee; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -9,12 +12,9 @@ import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; -import com.baeldung.model.Employee; -import com.baeldung.spring.web.config.WebConfig; - @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration -@ContextConfiguration(classes = WebConfig.class) +@ContextConfiguration(classes = { MatrixWebConfig.class, EmployeeController.class }) public class EmployeeNoMvcIntegrationTest { @Autowired diff --git a/spring-mvc-java/README.md b/spring-mvc-java/README.md index f1263860f9..877d92901a 100644 --- a/spring-mvc-java/README.md +++ b/spring-mvc-java/README.md @@ -8,12 +8,9 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring ### Relevant Articles: - [Integration Testing in Spring](https://www.baeldung.com/integration-testing-in-spring) -- [A Quick Guide to Spring MVC Matrix Variables](https://www.baeldung.com/spring-mvc-matrix-variables) - [File Upload with Spring MVC](https://www.baeldung.com/spring-file-upload) - [Introduction to HtmlUnit](https://www.baeldung.com/htmlunit) - [Upload and Display Excel Files with Spring MVC](https://www.baeldung.com/spring-mvc-excel-files) - [web.xml vs Initializer with Spring](https://www.baeldung.com/spring-xml-vs-java-config) -- [Spring MVC @PathVariable with a dot (.) gets truncated](https://www.baeldung.com/spring-mvc-pathvariable-dot) -- [Working with Date Parameters in Spring](https://www.baeldung.com/spring-date-parameters) - [A Java Web Application Without a web.xml](https://www.baeldung.com/java-web-app-without-web-xml) - [Accessing Spring MVC Model Objects in JavaScript](https://www.baeldung.com/spring-mvc-model-objects-js)