From 2d08a88ec0a796805531a5455d23c9501b84cb38 Mon Sep 17 00:00:00 2001 From: Krzysiek Date: Wed, 6 May 2020 01:31:06 +0200 Subject: [PATCH] Move A Quick Guide to Spring MVC Matrix Variables --- spring-mvc-java-2/README.md | 3 ++- spring-mvc-java-2/pom.xml | 5 +++++ .../{WebConfig.java => CacheWebConfig.java} | 2 +- .../matrix/config/MatrixWebConfig.java | 18 +++++++++++++++++ .../matrix}/controller/CompanyController.java | 17 +++++----------- .../controller/EmployeeController.java | 20 +++++-------------- .../com/baeldung/matrix}/model/Company.java | 2 +- .../com/baeldung/matrix}/model/Employee.java | 2 +- .../src/main/webapp/WEB-INF/mvc-servlet.xml | 10 ++++++++++ .../main/webapp/WEB-INF/view/companyHome.jsp | 0 .../main/webapp/WEB-INF/view/companyView.jsp | 0 .../main/webapp/WEB-INF/view/employeeHome.jsp | 0 .../main/webapp/WEB-INF/view/employeeView.jsp | 0 ...CacheControlControllerIntegrationTest.java | 2 +- .../matrix}/EmployeeMvcIntegrationTest.java | 16 +++++++-------- .../matrix}/EmployeeNoMvcIntegrationTest.java | 10 +++++----- spring-mvc-java/README.md | 1 - 17 files changed, 61 insertions(+), 47 deletions(-) rename spring-mvc-java-2/src/main/java/com/baeldung/cache/{WebConfig.java => CacheWebConfig.java} (96%) create mode 100644 spring-mvc-java-2/src/main/java/com/baeldung/matrix/config/MatrixWebConfig.java rename {spring-mvc-java/src/main/java/com/baeldung/web => spring-mvc-java-2/src/main/java/com/baeldung/matrix}/controller/CompanyController.java (81%) rename {spring-mvc-java/src/main/java/com/baeldung/web => spring-mvc-java-2/src/main/java/com/baeldung/matrix}/controller/EmployeeController.java (86%) rename {spring-mvc-java/src/main/java/com/baeldung => spring-mvc-java-2/src/main/java/com/baeldung/matrix}/model/Company.java (94%) rename {spring-mvc-java/src/main/java/com/baeldung => spring-mvc-java-2/src/main/java/com/baeldung/matrix}/model/Employee.java (97%) rename {spring-mvc-java => spring-mvc-java-2}/src/main/webapp/WEB-INF/view/companyHome.jsp (100%) rename {spring-mvc-java => spring-mvc-java-2}/src/main/webapp/WEB-INF/view/companyView.jsp (100%) rename {spring-mvc-java => spring-mvc-java-2}/src/main/webapp/WEB-INF/view/employeeHome.jsp (100%) rename {spring-mvc-java => spring-mvc-java-2}/src/main/webapp/WEB-INF/view/employeeView.jsp (100%) rename {spring-mvc-java/src/test/java/com/baeldung/web/controller => spring-mvc-java-2/src/test/java/com/baeldung/matrix}/EmployeeMvcIntegrationTest.java (81%) rename {spring-mvc-java/src/test/java/com/baeldung/web/controller => spring-mvc-java-2/src/test/java/com/baeldung/matrix}/EmployeeNoMvcIntegrationTest.java (86%) diff --git a/spring-mvc-java-2/README.md b/spring-mvc-java-2/README.md index 50cdafe1d7..09c8d8b294 100644 --- a/spring-mvc-java-2/README.md +++ b/spring-mvc-java-2/README.md @@ -2,4 +2,5 @@ - [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) \ No newline at end of file +- [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 7deab74fcb..af622321cb 100644 --- a/spring-mvc-java-2/pom.xml +++ b/spring-mvc-java-2/pom.xml @@ -26,6 +26,11 @@ spring-webmvc ${spring.mvc.version} + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + 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-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-2/src/main/webapp/WEB-INF/mvc-servlet.xml b/spring-mvc-java-2/src/main/webapp/WEB-INF/mvc-servlet.xml index b8e280dfdf..00dac5f8cb 100644 --- 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 @@ -11,7 +11,17 @@ http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> + + + + /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/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 0edc73e0e0..877d92901a 100644 --- a/spring-mvc-java/README.md +++ b/spring-mvc-java/README.md @@ -8,7 +8,6 @@ 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)