Merge pull request #9250 from kwoyke/JAVA-1529

JAVA-1529: Split or move spring-mvc-java module
This commit is contained in:
Josh Cummings 2020-05-07 10:59:06 -06:00 committed by GitHub
commit 7350a9377e
22 changed files with 123 additions and 59 deletions

View File

@ -1,3 +1,6 @@
### Relevant Articles: ### Relevant Articles:
- [Cache Headers in Spring MVC](https://www.baeldung.com/spring-mvc-cache-headers) - [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)

View File

@ -7,14 +7,14 @@
<version>0.1-SNAPSHOT</version> <version>0.1-SNAPSHOT</version>
<name>spring-mvc-java-2</name> <name>spring-mvc-java-2</name>
<packaging>war</packaging> <packaging>war</packaging>
<parent> <parent>
<groupId>com.baeldung</groupId> <groupId>com.baeldung</groupId>
<artifactId>parent-boot-2</artifactId> <artifactId>parent-boot-2</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-boot-2</relativePath> <relativePath>../parent-boot-2</relativePath>
</parent> </parent>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>javax.servlet</groupId> <groupId>javax.servlet</groupId>
@ -26,14 +26,27 @@
<artifactId>spring-webmvc</artifactId> <artifactId>spring-webmvc</artifactId>
<version>${spring.mvc.version}</version> <version>${spring.mvc.version}</version>
</dependency> </dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
</dependencies> </dependencies>
<build>
<finalName>spring-mvc-java-2</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<properties> <properties>
<javax.version>4.0.1</javax.version> <javax.version>4.0.1</javax.version>
<spring.mvc.version>5.2.2.RELEASE</spring.mvc.version> <spring.mvc.version>5.2.2.RELEASE</spring.mvc.version>
</properties> </properties>
</project> </project>

View File

@ -15,7 +15,7 @@ import java.util.concurrent.TimeUnit;
@EnableWebMvc @EnableWebMvc
@Configuration @Configuration
@ComponentScan(basePackages = {"com.baeldung.cache"}) @ComponentScan(basePackages = {"com.baeldung.cache"})
public class WebConfig implements WebMvcConfigurer { public class CacheWebConfig implements WebMvcConfigurer {
@Override @Override
public void addViewControllers(final ViewControllerRegistry registry) { public void addViewControllers(final ViewControllerRegistry registry) {

View File

@ -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);
}
}

View File

@ -1,23 +1,16 @@
package com.baeldung.web.controller; package com.baeldung.matrix.controller;
import java.util.HashMap;
import java.util.Map;
import com.baeldung.matrix.model.Company;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap; import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult; import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.MatrixVariable; import org.springframework.web.bind.annotation.*;
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.servlet.ModelAndView; import org.springframework.web.servlet.ModelAndView;
import com.baeldung.model.Company; import java.util.HashMap;
import java.util.Map;
@Controller @Controller
public class CompanyController { public class CompanyController {

View File

@ -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.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap; import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult; import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.MatrixVariable; import org.springframework.web.bind.annotation.*;
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.servlet.ModelAndView; import org.springframework.web.servlet.ModelAndView;
import com.baeldung.model.Employee; import java.util.*;
@SessionAttributes("employees") @SessionAttributes("employees")
@Controller @Controller
public class EmployeeController { public class EmployeeController {
Map<Long, Employee> employeeMap = new HashMap<>(); public Map<Long, Employee> employeeMap = new HashMap<>();
@ModelAttribute("employees") @ModelAttribute("employees")
public void initEmployees() { public void initEmployees() {

View File

@ -1,4 +1,4 @@
package com.baeldung.model; package com.baeldung.matrix.model;
public class Company { public class Company {

View File

@ -1,4 +1,4 @@
package com.baeldung.model; package com.baeldung.matrix.model;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;

View File

@ -1,4 +1,4 @@
package com.baeldung.spring.web.config; package com.baeldung.pathvariable;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer; import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;

View File

@ -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.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/site") @RequestMapping("/site")
public class SiteController { public class SiteController {
@RequestMapping(value = "/{firstValue}/{secondValue}", method = RequestMethod.GET) @GetMapping("/{firstValue}/{secondValue}")
public String requestWithError(@PathVariable("firstValue") String firstValue, public String requestWithError(@PathVariable("firstValue") String firstValue,
@PathVariable("secondValue") String secondValue) { @PathVariable("secondValue") String secondValue) {
return firstValue + " - " + secondValue; return firstValue + " - " + secondValue;
} }
@RequestMapping(value = "/{firstValue}/{secondValue:.+}", method = RequestMethod.GET) @GetMapping("/{firstValue}/{secondValue:.+}")
public String requestWithRegex(@PathVariable("firstValue") String firstValue, public String requestWithRegex(@PathVariable("firstValue") String firstValue,
@PathVariable("secondValue") String secondValue) { @PathVariable("secondValue") String secondValue) {
return firstValue + " - " + secondValue; return firstValue + " - " + secondValue;
} }
@RequestMapping(value = "/{firstValue}/{secondValue}/", method = RequestMethod.GET) @GetMapping("/{firstValue}/{secondValue}/")
public String requestWithSlash(@PathVariable("firstValue") String firstValue, public String requestWithSlash(@PathVariable("firstValue") String firstValue,
@PathVariable("secondValue") String secondValue) { @PathVariable("secondValue") String secondValue) {

View File

@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans
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">
<!-- <mvc:annotation-driven/>-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/view/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<context:component-scan base-package="com.baeldung"/>
</beans>

View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns="https://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="https://xmlns.jcp.org/xml/ns/javaee https://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>Spring MVC Application 2</display-name>
<!-- Add Spring MVC DispatcherServlet as front controller -->
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

View File

@ -19,7 +19,7 @@ import static org.springframework.http.HttpHeaders.IF_UNMODIFIED_SINCE;
@ExtendWith(SpringExtension.class) @ExtendWith(SpringExtension.class)
@WebAppConfiguration @WebAppConfiguration
@ContextConfiguration(classes = {WebConfig.class, WebConfig.class}) @ContextConfiguration(classes = {CacheWebConfig.class, CacheWebConfig.class})
public class CacheControlControllerIntegrationTest { public class CacheControlControllerIntegrationTest {
@Autowired @Autowired

View File

@ -1,11 +1,7 @@
package com.baeldung.web.controller; package com.baeldung.matrix;
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;
import com.baeldung.matrix.config.MatrixWebConfig;
import com.baeldung.matrix.controller.EmployeeController;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; 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.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext; 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) @RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration @WebAppConfiguration
@ContextConfiguration(classes = WebConfig.class) @ContextConfiguration(classes = { MatrixWebConfig.class, EmployeeController.class })
public class EmployeeMvcIntegrationTest { public class EmployeeMvcIntegrationTest {
@Autowired @Autowired

View File

@ -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.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; 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.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.context.web.WebAppConfiguration;
import com.baeldung.model.Employee;
import com.baeldung.spring.web.config.WebConfig;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration @WebAppConfiguration
@ContextConfiguration(classes = WebConfig.class) @ContextConfiguration(classes = { MatrixWebConfig.class, EmployeeController.class })
public class EmployeeNoMvcIntegrationTest { public class EmployeeNoMvcIntegrationTest {
@Autowired @Autowired

View File

@ -8,12 +8,9 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
### Relevant Articles: ### Relevant Articles:
- [Integration Testing in Spring](https://www.baeldung.com/integration-testing-in-spring) - [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) - [File Upload with Spring MVC](https://www.baeldung.com/spring-file-upload)
- [Introduction to HtmlUnit](https://www.baeldung.com/htmlunit) - [Introduction to HtmlUnit](https://www.baeldung.com/htmlunit)
- [Upload and Display Excel Files with Spring MVC](https://www.baeldung.com/spring-mvc-excel-files) - [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) - [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) - [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) - [Accessing Spring MVC Model Objects in JavaScript](https://www.baeldung.com/spring-mvc-model-objects-js)