From 84080c9e955fafc9b56ab8079a520995c669af44 Mon Sep 17 00:00:00 2001 From: Chris Oberle Date: Sat, 10 Mar 2018 07:06:26 -0500 Subject: [PATCH] BAEL-1533 - Making a Spring MVC Form Remember Values --- pom.xml | 3 +- spring-5/pom.xml | 437 +++++++++--------- .../README.md | 0 .../pom.xml | 4 +- .../ApplicationConfiguration.java | 0 .../configuration/WebInitializer.java | 0 .../controller/CustomerController.java | 0 .../controller/EmployeeController.java | 0 .../controller/FileUploadController.java | 0 .../controller/UserController.java | 0 .../springmvcforms/domain/Customer.java | 0 .../springmvcforms/domain/Employee.java | 0 .../baeldung/springmvcforms/domain/User.java | 0 .../FileUploadExceptionAdvice.java | 0 .../validator/CustomerValidator.java | 0 .../src/main/webapp/WEB-INF/html/user.html | 0 .../webapp/WEB-INF/views/customerHome.jsp | 0 .../webapp/WEB-INF/views/customerView.jsp | 0 .../webapp/WEB-INF/views/employeeHome.jsp | 0 .../webapp/WEB-INF/views/employeeView.jsp | 0 .../src/main/webapp/WEB-INF/views/error.jsp | 0 .../src/main/webapp/WEB-INF/views/file.jsp | 0 .../src/main/webapp/css/user.css | 0 .../src/main/webapp/js/app.js | 0 spring-mvc-forms-thymeleaf/pom.xml | 73 +++ .../com/baeldung/sessionattrs/Config.java | 88 ++-- .../sessionattrs/SessionAttrsApplication.java | 0 .../TodoControllerWithScopedProxy.java | 90 ++-- .../TodoControllerWithSessionAttributes.java | 110 ++--- .../com/baeldung/sessionattrs/TodoItem.java | 78 ++-- .../com/baeldung/sessionattrs/TodoList.java | 16 +- .../src/main/resources/application.properties | 3 + .../templates/sessionattrs/index.html | 52 +-- .../sessionattrs/scopedproxyform.html | 52 +-- .../sessionattrs/scopedproxytodos.html | 94 ++-- .../sessionattrs/sessionattributesform.html | 52 +-- .../sessionattrs/sessionattributestodos.html | 94 ++-- .../SessionAttrsApplicationTest.java | 2 +- .../com/baeldung/sessionattrs/TestConfig.java | 34 +- .../TodoControllerWithScopedProxyTest.java | 136 +++--- ...doControllerWithSessionAttributesTest.java | 136 +++--- 41 files changed, 819 insertions(+), 735 deletions(-) rename {spring-mvc-forms => spring-mvc-forms-jsp}/README.md (100%) rename {spring-mvc-forms => spring-mvc-forms-jsp}/pom.xml (97%) rename {spring-mvc-forms => spring-mvc-forms-jsp}/src/main/java/com/baeldung/springmvcforms/configuration/ApplicationConfiguration.java (100%) rename {spring-mvc-forms => spring-mvc-forms-jsp}/src/main/java/com/baeldung/springmvcforms/configuration/WebInitializer.java (100%) rename {spring-mvc-forms => spring-mvc-forms-jsp}/src/main/java/com/baeldung/springmvcforms/controller/CustomerController.java (100%) rename {spring-mvc-forms => spring-mvc-forms-jsp}/src/main/java/com/baeldung/springmvcforms/controller/EmployeeController.java (100%) rename {spring-mvc-forms => spring-mvc-forms-jsp}/src/main/java/com/baeldung/springmvcforms/controller/FileUploadController.java (100%) rename {spring-mvc-forms => spring-mvc-forms-jsp}/src/main/java/com/baeldung/springmvcforms/controller/UserController.java (100%) rename {spring-mvc-forms => spring-mvc-forms-jsp}/src/main/java/com/baeldung/springmvcforms/domain/Customer.java (100%) rename {spring-mvc-forms => spring-mvc-forms-jsp}/src/main/java/com/baeldung/springmvcforms/domain/Employee.java (100%) rename {spring-mvc-forms => spring-mvc-forms-jsp}/src/main/java/com/baeldung/springmvcforms/domain/User.java (100%) rename {spring-mvc-forms => spring-mvc-forms-jsp}/src/main/java/com/baeldung/springmvcforms/interceptor/FileUploadExceptionAdvice.java (100%) rename {spring-mvc-forms => spring-mvc-forms-jsp}/src/main/java/com/baeldung/springmvcforms/validator/CustomerValidator.java (100%) rename {spring-mvc-forms => spring-mvc-forms-jsp}/src/main/webapp/WEB-INF/html/user.html (100%) rename {spring-mvc-forms => spring-mvc-forms-jsp}/src/main/webapp/WEB-INF/views/customerHome.jsp (100%) rename {spring-mvc-forms => spring-mvc-forms-jsp}/src/main/webapp/WEB-INF/views/customerView.jsp (100%) rename {spring-mvc-forms => spring-mvc-forms-jsp}/src/main/webapp/WEB-INF/views/employeeHome.jsp (100%) rename {spring-mvc-forms => spring-mvc-forms-jsp}/src/main/webapp/WEB-INF/views/employeeView.jsp (100%) rename {spring-mvc-forms => spring-mvc-forms-jsp}/src/main/webapp/WEB-INF/views/error.jsp (100%) rename {spring-mvc-forms => spring-mvc-forms-jsp}/src/main/webapp/WEB-INF/views/file.jsp (100%) rename {spring-mvc-forms => spring-mvc-forms-jsp}/src/main/webapp/css/user.css (100%) rename {spring-mvc-forms => spring-mvc-forms-jsp}/src/main/webapp/js/app.js (100%) create mode 100644 spring-mvc-forms-thymeleaf/pom.xml rename {spring-5 => spring-mvc-forms-thymeleaf}/src/main/java/com/baeldung/sessionattrs/Config.java (97%) rename {spring-5 => spring-mvc-forms-thymeleaf}/src/main/java/com/baeldung/sessionattrs/SessionAttrsApplication.java (100%) rename {spring-5 => spring-mvc-forms-thymeleaf}/src/main/java/com/baeldung/sessionattrs/TodoControllerWithScopedProxy.java (96%) rename {spring-5 => spring-mvc-forms-thymeleaf}/src/main/java/com/baeldung/sessionattrs/TodoControllerWithSessionAttributes.java (97%) rename {spring-5 => spring-mvc-forms-thymeleaf}/src/main/java/com/baeldung/sessionattrs/TodoItem.java (95%) rename {spring-5 => spring-mvc-forms-thymeleaf}/src/main/java/com/baeldung/sessionattrs/TodoList.java (94%) create mode 100644 spring-mvc-forms-thymeleaf/src/main/resources/application.properties rename {spring-5 => spring-mvc-forms-thymeleaf}/src/main/resources/templates/sessionattrs/index.html (97%) rename {spring-5 => spring-mvc-forms-thymeleaf}/src/main/resources/templates/sessionattrs/scopedproxyform.html (98%) rename {spring-5 => spring-mvc-forms-thymeleaf}/src/main/resources/templates/sessionattrs/scopedproxytodos.html (97%) rename {spring-5 => spring-mvc-forms-thymeleaf}/src/main/resources/templates/sessionattrs/sessionattributesform.html (98%) rename {spring-5 => spring-mvc-forms-thymeleaf}/src/main/resources/templates/sessionattrs/sessionattributestodos.html (97%) rename spring-5/src/test/java/com/baeldung/sessionattrs/SessionAttrsApplicationIntegrationTest.java => spring-mvc-forms-thymeleaf/src/test/java/com/baeldung/sessionattrs/SessionAttrsApplicationTest.java (85%) rename {spring-5 => spring-mvc-forms-thymeleaf}/src/test/java/com/baeldung/sessionattrs/TestConfig.java (97%) rename spring-5/src/test/java/com/baeldung/sessionattrs/TodoControllerWithScopedProxyIntegrationTest.java => spring-mvc-forms-thymeleaf/src/test/java/com/baeldung/sessionattrs/TodoControllerWithScopedProxyTest.java (91%) rename spring-5/src/test/java/com/baeldung/sessionattrs/TodoControllerWithSessionAttributesIntegrationTest.java => spring-mvc-forms-thymeleaf/src/test/java/com/baeldung/sessionattrs/TodoControllerWithSessionAttributesTest.java (95%) diff --git a/pom.xml b/pom.xml index 70a3bbc5b4..205d42858a 100644 --- a/pom.xml +++ b/pom.xml @@ -199,7 +199,8 @@ spring-ldap spring-mockito spring-mvc-email - spring-mvc-forms + spring-mvc-forms-jsp + spring-mvc-forms-thymeleaf spring-mvc-java spring-mvc-tiles spring-mvc-velocity diff --git a/spring-5/pom.xml b/spring-5/pom.xml index b4e6e684ad..f8bad72c51 100644 --- a/spring-5/pom.xml +++ b/spring-5/pom.xml @@ -1,215 +1,222 @@ - - - 4.0.0 - - com.baeldung - spring-5 - 0.0.1-SNAPSHOT - jar - - spring-5 - spring 5 sample project about new features - - - org.springframework.boot - spring-boot-starter-parent - 2.0.0.RELEASE - - - - - - org.springframework.boot - spring-boot-starter-data-jpa - - - org.springframework.boot - spring-boot-starter-security - - - org.springframework.boot - spring-boot-starter-validation - - - org.springframework.boot - spring-boot-starter-web - - - org.springframework.boot - spring-boot-starter-webflux - - - org.springframework.boot - spring-boot-starter-hateoas - - - org.springframework.boot - spring-boot-starter-thymeleaf - - - org.projectreactor - reactor-spring - ${reactor-spring.version} - - - javax.json.bind - javax.json.bind-api - - - org.apache.geronimo.specs - geronimo-json_1.1_spec - ${geronimo-json_1.1_spec.version} - - - org.apache.johnzon - johnzon-jsonb - ${johnzon.version} - - - - org.apache.commons - commons-lang3 - - - - - - org.springframework.boot - spring-boot-devtools - runtime - - - com.h2database - h2 - runtime - - - - org.springframework - spring-test - - - org.springframework.boot - spring-boot-starter-test - test - - - org.springframework.security - spring-security-test - test - - - - org.apache.commons - commons-collections4 - 4.1 - test - - - - org.junit.jupiter - junit-jupiter-api - - - org.junit.jupiter - junit-jupiter-engine - test - - - org.junit.platform - junit-platform-surefire-provider - ${junit.platform.version} - test - - - org.junit.platform - junit-platform-runner - ${junit.platform.version} - test - - - - org.springframework.restdocs - spring-restdocs-mockmvc - test - - - org.springframework.restdocs - spring-restdocs-webtestclient - test - - - org.springframework.restdocs - spring-restdocs-restassured - test - - - - - - - - - org.springframework.boot - spring-boot-maven-plugin - - com.baeldung.Spring5Application - JAR - - - - - org.apache.maven.plugins - maven-surefire-plugin - - 3 - true - methods - true - - **/*IntegrationTest.java - **/*LiveTest.java - - - - - org.asciidoctor - asciidoctor-maven-plugin - ${asciidoctor-plugin.version} - - - generate-docs - package - - process-asciidoc - - - html - book - - ${snippetsDirectory} - - src/docs/asciidocs - target/generated-docs - - - - - - - - - UTF-8 - UTF-8 - 1.8 - 1.0.0 - 2.20 - 1.0.1.RELEASE - 1.1.3 - 1.0 - 1.5.6 - ${project.build.directory}/generated-snippets - - - + + + 4.0.0 + + com.baeldung + spring-5 + 0.0.1-SNAPSHOT + jar + + spring-5 + spring 5 sample project about new features + + + org.springframework.boot + spring-boot-starter-parent + 2.0.0.RELEASE + + + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + org.springframework.boot + spring-boot-starter-security + + + org.springframework.boot + spring-boot-starter-validation + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-webflux + + + org.springframework.boot + spring-boot-starter-hateoas + + + org.projectreactor + reactor-spring + ${reactor-spring.version} + + + javax.json.bind + javax.json.bind-api + + + + + + + + + + + + + + + org.apache.geronimo.specs + geronimo-json_1.1_spec + ${geronimo-json_1.1_spec.version} + + + org.apache.johnzon + johnzon-jsonb + + + + org.apache.commons + commons-lang3 + + + + + + org.springframework.boot + spring-boot-devtools + runtime + + + com.h2database + h2 + runtime + + + + org.springframework + spring-test + + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework.security + spring-security-test + test + + + + org.apache.commons + commons-collections4 + 4.1 + test + + + + org.junit.jupiter + junit-jupiter-api + + + org.junit.jupiter + junit-jupiter-engine + test + + + org.junit.platform + junit-platform-surefire-provider + ${junit.platform.version} + test + + + org.junit.platform + junit-platform-runner + ${junit.platform.version} + test + + + + org.springframework.restdocs + spring-restdocs-mockmvc + test + + + org.springframework.restdocs + spring-restdocs-webtestclient + test + + + org.springframework.restdocs + spring-restdocs-restassured + test + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + com.baeldung.Spring5Application + JAR + + + + + org.apache.maven.plugins + maven-surefire-plugin + + 3 + true + methods + true + + **/*IntegrationTest.java + **/*LiveTest.java + + + + + org.asciidoctor + asciidoctor-maven-plugin + ${asciidoctor-plugin.version} + + + generate-docs + package + + process-asciidoc + + + html + book + + ${snippetsDirectory} + + src/docs/asciidocs + target/generated-docs + + + + + + + + + UTF-8 + UTF-8 + 1.8 + 1.0.0 + 2.20 + 5.0.2.RELEASE + 1.0.1.RELEASE + 1.0 + 1.5.6 + ${project.build.directory}/generated-snippets + + + diff --git a/spring-mvc-forms/README.md b/spring-mvc-forms-jsp/README.md similarity index 100% rename from spring-mvc-forms/README.md rename to spring-mvc-forms-jsp/README.md diff --git a/spring-mvc-forms/pom.xml b/spring-mvc-forms-jsp/pom.xml similarity index 97% rename from spring-mvc-forms/pom.xml rename to spring-mvc-forms-jsp/pom.xml index e82a906112..bb666cff4f 100644 --- a/spring-mvc-forms/pom.xml +++ b/spring-mvc-forms-jsp/pom.xml @@ -6,9 +6,9 @@ 4.0.0 com.baeldung 0.1-SNAPSHOT - spring-mvc-forms + spring-mvc-forms-jsp - spring-mvc-forms + spring-mvc-forms-jsp war diff --git a/spring-mvc-forms/src/main/java/com/baeldung/springmvcforms/configuration/ApplicationConfiguration.java b/spring-mvc-forms-jsp/src/main/java/com/baeldung/springmvcforms/configuration/ApplicationConfiguration.java similarity index 100% rename from spring-mvc-forms/src/main/java/com/baeldung/springmvcforms/configuration/ApplicationConfiguration.java rename to spring-mvc-forms-jsp/src/main/java/com/baeldung/springmvcforms/configuration/ApplicationConfiguration.java diff --git a/spring-mvc-forms/src/main/java/com/baeldung/springmvcforms/configuration/WebInitializer.java b/spring-mvc-forms-jsp/src/main/java/com/baeldung/springmvcforms/configuration/WebInitializer.java similarity index 100% rename from spring-mvc-forms/src/main/java/com/baeldung/springmvcforms/configuration/WebInitializer.java rename to spring-mvc-forms-jsp/src/main/java/com/baeldung/springmvcforms/configuration/WebInitializer.java diff --git a/spring-mvc-forms/src/main/java/com/baeldung/springmvcforms/controller/CustomerController.java b/spring-mvc-forms-jsp/src/main/java/com/baeldung/springmvcforms/controller/CustomerController.java similarity index 100% rename from spring-mvc-forms/src/main/java/com/baeldung/springmvcforms/controller/CustomerController.java rename to spring-mvc-forms-jsp/src/main/java/com/baeldung/springmvcforms/controller/CustomerController.java diff --git a/spring-mvc-forms/src/main/java/com/baeldung/springmvcforms/controller/EmployeeController.java b/spring-mvc-forms-jsp/src/main/java/com/baeldung/springmvcforms/controller/EmployeeController.java similarity index 100% rename from spring-mvc-forms/src/main/java/com/baeldung/springmvcforms/controller/EmployeeController.java rename to spring-mvc-forms-jsp/src/main/java/com/baeldung/springmvcforms/controller/EmployeeController.java diff --git a/spring-mvc-forms/src/main/java/com/baeldung/springmvcforms/controller/FileUploadController.java b/spring-mvc-forms-jsp/src/main/java/com/baeldung/springmvcforms/controller/FileUploadController.java similarity index 100% rename from spring-mvc-forms/src/main/java/com/baeldung/springmvcforms/controller/FileUploadController.java rename to spring-mvc-forms-jsp/src/main/java/com/baeldung/springmvcforms/controller/FileUploadController.java diff --git a/spring-mvc-forms/src/main/java/com/baeldung/springmvcforms/controller/UserController.java b/spring-mvc-forms-jsp/src/main/java/com/baeldung/springmvcforms/controller/UserController.java similarity index 100% rename from spring-mvc-forms/src/main/java/com/baeldung/springmvcforms/controller/UserController.java rename to spring-mvc-forms-jsp/src/main/java/com/baeldung/springmvcforms/controller/UserController.java diff --git a/spring-mvc-forms/src/main/java/com/baeldung/springmvcforms/domain/Customer.java b/spring-mvc-forms-jsp/src/main/java/com/baeldung/springmvcforms/domain/Customer.java similarity index 100% rename from spring-mvc-forms/src/main/java/com/baeldung/springmvcforms/domain/Customer.java rename to spring-mvc-forms-jsp/src/main/java/com/baeldung/springmvcforms/domain/Customer.java diff --git a/spring-mvc-forms/src/main/java/com/baeldung/springmvcforms/domain/Employee.java b/spring-mvc-forms-jsp/src/main/java/com/baeldung/springmvcforms/domain/Employee.java similarity index 100% rename from spring-mvc-forms/src/main/java/com/baeldung/springmvcforms/domain/Employee.java rename to spring-mvc-forms-jsp/src/main/java/com/baeldung/springmvcforms/domain/Employee.java diff --git a/spring-mvc-forms/src/main/java/com/baeldung/springmvcforms/domain/User.java b/spring-mvc-forms-jsp/src/main/java/com/baeldung/springmvcforms/domain/User.java similarity index 100% rename from spring-mvc-forms/src/main/java/com/baeldung/springmvcforms/domain/User.java rename to spring-mvc-forms-jsp/src/main/java/com/baeldung/springmvcforms/domain/User.java diff --git a/spring-mvc-forms/src/main/java/com/baeldung/springmvcforms/interceptor/FileUploadExceptionAdvice.java b/spring-mvc-forms-jsp/src/main/java/com/baeldung/springmvcforms/interceptor/FileUploadExceptionAdvice.java similarity index 100% rename from spring-mvc-forms/src/main/java/com/baeldung/springmvcforms/interceptor/FileUploadExceptionAdvice.java rename to spring-mvc-forms-jsp/src/main/java/com/baeldung/springmvcforms/interceptor/FileUploadExceptionAdvice.java diff --git a/spring-mvc-forms/src/main/java/com/baeldung/springmvcforms/validator/CustomerValidator.java b/spring-mvc-forms-jsp/src/main/java/com/baeldung/springmvcforms/validator/CustomerValidator.java similarity index 100% rename from spring-mvc-forms/src/main/java/com/baeldung/springmvcforms/validator/CustomerValidator.java rename to spring-mvc-forms-jsp/src/main/java/com/baeldung/springmvcforms/validator/CustomerValidator.java diff --git a/spring-mvc-forms/src/main/webapp/WEB-INF/html/user.html b/spring-mvc-forms-jsp/src/main/webapp/WEB-INF/html/user.html similarity index 100% rename from spring-mvc-forms/src/main/webapp/WEB-INF/html/user.html rename to spring-mvc-forms-jsp/src/main/webapp/WEB-INF/html/user.html diff --git a/spring-mvc-forms/src/main/webapp/WEB-INF/views/customerHome.jsp b/spring-mvc-forms-jsp/src/main/webapp/WEB-INF/views/customerHome.jsp similarity index 100% rename from spring-mvc-forms/src/main/webapp/WEB-INF/views/customerHome.jsp rename to spring-mvc-forms-jsp/src/main/webapp/WEB-INF/views/customerHome.jsp diff --git a/spring-mvc-forms/src/main/webapp/WEB-INF/views/customerView.jsp b/spring-mvc-forms-jsp/src/main/webapp/WEB-INF/views/customerView.jsp similarity index 100% rename from spring-mvc-forms/src/main/webapp/WEB-INF/views/customerView.jsp rename to spring-mvc-forms-jsp/src/main/webapp/WEB-INF/views/customerView.jsp diff --git a/spring-mvc-forms/src/main/webapp/WEB-INF/views/employeeHome.jsp b/spring-mvc-forms-jsp/src/main/webapp/WEB-INF/views/employeeHome.jsp similarity index 100% rename from spring-mvc-forms/src/main/webapp/WEB-INF/views/employeeHome.jsp rename to spring-mvc-forms-jsp/src/main/webapp/WEB-INF/views/employeeHome.jsp diff --git a/spring-mvc-forms/src/main/webapp/WEB-INF/views/employeeView.jsp b/spring-mvc-forms-jsp/src/main/webapp/WEB-INF/views/employeeView.jsp similarity index 100% rename from spring-mvc-forms/src/main/webapp/WEB-INF/views/employeeView.jsp rename to spring-mvc-forms-jsp/src/main/webapp/WEB-INF/views/employeeView.jsp diff --git a/spring-mvc-forms/src/main/webapp/WEB-INF/views/error.jsp b/spring-mvc-forms-jsp/src/main/webapp/WEB-INF/views/error.jsp similarity index 100% rename from spring-mvc-forms/src/main/webapp/WEB-INF/views/error.jsp rename to spring-mvc-forms-jsp/src/main/webapp/WEB-INF/views/error.jsp diff --git a/spring-mvc-forms/src/main/webapp/WEB-INF/views/file.jsp b/spring-mvc-forms-jsp/src/main/webapp/WEB-INF/views/file.jsp similarity index 100% rename from spring-mvc-forms/src/main/webapp/WEB-INF/views/file.jsp rename to spring-mvc-forms-jsp/src/main/webapp/WEB-INF/views/file.jsp diff --git a/spring-mvc-forms/src/main/webapp/css/user.css b/spring-mvc-forms-jsp/src/main/webapp/css/user.css similarity index 100% rename from spring-mvc-forms/src/main/webapp/css/user.css rename to spring-mvc-forms-jsp/src/main/webapp/css/user.css diff --git a/spring-mvc-forms/src/main/webapp/js/app.js b/spring-mvc-forms-jsp/src/main/webapp/js/app.js similarity index 100% rename from spring-mvc-forms/src/main/webapp/js/app.js rename to spring-mvc-forms-jsp/src/main/webapp/js/app.js diff --git a/spring-mvc-forms-thymeleaf/pom.xml b/spring-mvc-forms-thymeleaf/pom.xml new file mode 100644 index 0000000000..9238153847 --- /dev/null +++ b/spring-mvc-forms-thymeleaf/pom.xml @@ -0,0 +1,73 @@ + + + 4.0.0 + + com.baeldung + spring-mvc-forms-thymeleaf + 0.0.1-SNAPSHOT + jar + + spring-mvc-forms-thymeleaf + spring forms examples using thymeleaf + + + org.springframework.boot + spring-boot-starter-parent + 2.0.0.RELEASE + + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + + + org.springframework.boot + spring-boot-devtools + runtime + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.apache.maven.plugins + maven-surefire-plugin + + 3 + true + methods + true + + **/*IntegrationTest.java + **/*LiveTest.java + + + + + + + + UTF-8 + UTF-8 + + + diff --git a/spring-5/src/main/java/com/baeldung/sessionattrs/Config.java b/spring-mvc-forms-thymeleaf/src/main/java/com/baeldung/sessionattrs/Config.java similarity index 97% rename from spring-5/src/main/java/com/baeldung/sessionattrs/Config.java rename to spring-mvc-forms-thymeleaf/src/main/java/com/baeldung/sessionattrs/Config.java index 9d5c9d9f42..7c9f1bf00b 100644 --- a/spring-5/src/main/java/com/baeldung/sessionattrs/Config.java +++ b/spring-mvc-forms-thymeleaf/src/main/java/com/baeldung/sessionattrs/Config.java @@ -1,44 +1,44 @@ -package com.baeldung.sessionattrs; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Scope; -import org.springframework.context.annotation.ScopedProxyMode; -import org.springframework.core.Ordered; -import org.springframework.web.context.WebApplicationContext; -import org.springframework.web.servlet.config.annotation.EnableWebMvc; -import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; -import org.thymeleaf.templatemode.TemplateMode; -import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver; -import org.thymeleaf.templateresolver.ITemplateResolver; - -@EnableWebMvc -@Configuration -public class Config implements WebMvcConfigurer { - - @Override - public void addViewControllers(ViewControllerRegistry registry) { - registry.addViewController("/").setViewName("index"); - registry.setOrder(Ordered.HIGHEST_PRECEDENCE); - } - - @Bean - @Scope( - value = WebApplicationContext.SCOPE_SESSION, - proxyMode = ScopedProxyMode.TARGET_CLASS) - public TodoList todos() { - return new TodoList(); - } - - @Bean - public ITemplateResolver templateResolver() { - ClassLoaderTemplateResolver resolver - = new ClassLoaderTemplateResolver(); - resolver.setPrefix("templates/sessionattrs/"); - resolver.setSuffix(".html"); - resolver.setTemplateMode(TemplateMode.HTML); - resolver.setCharacterEncoding("UTF-8"); - return resolver; - } -} +package com.baeldung.sessionattrs; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Scope; +import org.springframework.context.annotation.ScopedProxyMode; +import org.springframework.core.Ordered; +import org.springframework.web.context.WebApplicationContext; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +import org.thymeleaf.templatemode.TemplateMode; +import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver; +import org.thymeleaf.templateresolver.ITemplateResolver; + +@EnableWebMvc +@Configuration +public class Config implements WebMvcConfigurer { + + @Override + public void addViewControllers(ViewControllerRegistry registry) { + registry.addViewController("/").setViewName("index"); + registry.setOrder(Ordered.HIGHEST_PRECEDENCE); + } + + @Bean + @Scope( + value = WebApplicationContext.SCOPE_SESSION, + proxyMode = ScopedProxyMode.TARGET_CLASS) + public TodoList todos() { + return new TodoList(); + } + + @Bean + public ITemplateResolver templateResolver() { + ClassLoaderTemplateResolver resolver + = new ClassLoaderTemplateResolver(); + resolver.setPrefix("templates/sessionattrs/"); + resolver.setSuffix(".html"); + resolver.setTemplateMode(TemplateMode.HTML); + resolver.setCharacterEncoding("UTF-8"); + return resolver; + } +} diff --git a/spring-5/src/main/java/com/baeldung/sessionattrs/SessionAttrsApplication.java b/spring-mvc-forms-thymeleaf/src/main/java/com/baeldung/sessionattrs/SessionAttrsApplication.java similarity index 100% rename from spring-5/src/main/java/com/baeldung/sessionattrs/SessionAttrsApplication.java rename to spring-mvc-forms-thymeleaf/src/main/java/com/baeldung/sessionattrs/SessionAttrsApplication.java diff --git a/spring-5/src/main/java/com/baeldung/sessionattrs/TodoControllerWithScopedProxy.java b/spring-mvc-forms-thymeleaf/src/main/java/com/baeldung/sessionattrs/TodoControllerWithScopedProxy.java similarity index 96% rename from spring-5/src/main/java/com/baeldung/sessionattrs/TodoControllerWithScopedProxy.java rename to spring-mvc-forms-thymeleaf/src/main/java/com/baeldung/sessionattrs/TodoControllerWithScopedProxy.java index 0c3bd6c8b6..feedecf844 100644 --- a/spring-5/src/main/java/com/baeldung/sessionattrs/TodoControllerWithScopedProxy.java +++ b/spring-mvc-forms-thymeleaf/src/main/java/com/baeldung/sessionattrs/TodoControllerWithScopedProxy.java @@ -1,45 +1,45 @@ -package com.baeldung.sessionattrs; - -import java.time.LocalDateTime; - -import org.springframework.stereotype.Controller; -import org.springframework.ui.Model; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.ModelAttribute; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestMapping; - -@Controller -@RequestMapping("/scopedproxy") -public class TodoControllerWithScopedProxy { - - private TodoList todos; - - public TodoControllerWithScopedProxy(TodoList todos) { - this.todos = todos; - } - - @GetMapping("/form") - public String showForm(Model model) { - if (!todos.isEmpty()) { - model.addAttribute("todo", todos.peekLast()); - } else { - model.addAttribute("todo", new TodoItem()); - } - - return "scopedproxyform"; - } - - @PostMapping("/form") - public String create(@ModelAttribute TodoItem todo) { - todo.setCreateDate(LocalDateTime.now()); - todos.add(todo); - return "redirect:/scopedproxy/todos.html"; - } - - @GetMapping("/todos.html") - public String list(Model model) { - model.addAttribute("todos", todos); - return "scopedproxytodos"; - } -} +package com.baeldung.sessionattrs; + +import java.time.LocalDateTime; + +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; + +@Controller +@RequestMapping("/scopedproxy") +public class TodoControllerWithScopedProxy { + + private TodoList todos; + + public TodoControllerWithScopedProxy(TodoList todos) { + this.todos = todos; + } + + @GetMapping("/form") + public String showForm(Model model) { + if (!todos.isEmpty()) { + model.addAttribute("todo", todos.peekLast()); + } else { + model.addAttribute("todo", new TodoItem()); + } + + return "scopedproxyform"; + } + + @PostMapping("/form") + public String create(@ModelAttribute TodoItem todo) { + todo.setCreateDate(LocalDateTime.now()); + todos.add(todo); + return "redirect:/scopedproxy/todos.html"; + } + + @GetMapping("/todos.html") + public String list(Model model) { + model.addAttribute("todos", todos); + return "scopedproxytodos"; + } +} diff --git a/spring-5/src/main/java/com/baeldung/sessionattrs/TodoControllerWithSessionAttributes.java b/spring-mvc-forms-thymeleaf/src/main/java/com/baeldung/sessionattrs/TodoControllerWithSessionAttributes.java similarity index 97% rename from spring-5/src/main/java/com/baeldung/sessionattrs/TodoControllerWithSessionAttributes.java rename to spring-mvc-forms-thymeleaf/src/main/java/com/baeldung/sessionattrs/TodoControllerWithSessionAttributes.java index fc7e57b1db..99e0b46fc2 100644 --- a/spring-5/src/main/java/com/baeldung/sessionattrs/TodoControllerWithSessionAttributes.java +++ b/spring-mvc-forms-thymeleaf/src/main/java/com/baeldung/sessionattrs/TodoControllerWithSessionAttributes.java @@ -1,55 +1,55 @@ -package com.baeldung.sessionattrs; - -import java.time.LocalDateTime; - -import org.springframework.stereotype.Controller; -import org.springframework.ui.Model; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.ModelAttribute; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.SessionAttributes; -import org.springframework.web.servlet.mvc.support.RedirectAttributes; -import org.springframework.web.servlet.view.RedirectView; - -@Controller -@RequestMapping("/sessionattributes") -@SessionAttributes("todos") -public class TodoControllerWithSessionAttributes { - - @GetMapping("/form") - public String showForm( - Model model, - @ModelAttribute("todos") TodoList todos) { - if (!todos.isEmpty()) { - model.addAttribute("todo", todos.peekLast()); - } else { - model.addAttribute("todo", new TodoItem()); - } - return "sessionattributesform"; - } - - @PostMapping("/form") - public RedirectView create( - @ModelAttribute TodoItem todo, - @ModelAttribute("todos") TodoList todos, - RedirectAttributes attributes) { - todo.setCreateDate(LocalDateTime.now()); - todos.add(todo); - attributes.addFlashAttribute("todos", todos); - return new RedirectView("/sessionattributes/todos.html"); - } - - @GetMapping("/todos.html") - public String list( - Model model, - @ModelAttribute("todos") TodoList todos) { - model.addAttribute("todos", todos); - return "sessionattributestodos"; - } - - @ModelAttribute("todos") - public TodoList todos() { - return new TodoList(); - } -} +package com.baeldung.sessionattrs; + +import java.time.LocalDateTime; + +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.SessionAttributes; +import org.springframework.web.servlet.mvc.support.RedirectAttributes; +import org.springframework.web.servlet.view.RedirectView; + +@Controller +@RequestMapping("/sessionattributes") +@SessionAttributes("todos") +public class TodoControllerWithSessionAttributes { + + @GetMapping("/form") + public String showForm( + Model model, + @ModelAttribute("todos") TodoList todos) { + if (!todos.isEmpty()) { + model.addAttribute("todo", todos.peekLast()); + } else { + model.addAttribute("todo", new TodoItem()); + } + return "sessionattributesform"; + } + + @PostMapping("/form") + public RedirectView create( + @ModelAttribute TodoItem todo, + @ModelAttribute("todos") TodoList todos, + RedirectAttributes attributes) { + todo.setCreateDate(LocalDateTime.now()); + todos.add(todo); + attributes.addFlashAttribute("todos", todos); + return new RedirectView("/sessionattributes/todos.html"); + } + + @GetMapping("/todos.html") + public String list( + Model model, + @ModelAttribute("todos") TodoList todos) { + model.addAttribute("todos", todos); + return "sessionattributestodos"; + } + + @ModelAttribute("todos") + public TodoList todos() { + return new TodoList(); + } +} diff --git a/spring-5/src/main/java/com/baeldung/sessionattrs/TodoItem.java b/spring-mvc-forms-thymeleaf/src/main/java/com/baeldung/sessionattrs/TodoItem.java similarity index 95% rename from spring-5/src/main/java/com/baeldung/sessionattrs/TodoItem.java rename to spring-mvc-forms-thymeleaf/src/main/java/com/baeldung/sessionattrs/TodoItem.java index 619d61d5f0..b8b24bc04d 100644 --- a/spring-5/src/main/java/com/baeldung/sessionattrs/TodoItem.java +++ b/spring-mvc-forms-thymeleaf/src/main/java/com/baeldung/sessionattrs/TodoItem.java @@ -1,39 +1,39 @@ -package com.baeldung.sessionattrs; - -import java.time.LocalDateTime; - -public class TodoItem { - - private String description; - private LocalDateTime createDate; - - public TodoItem(String description, LocalDateTime createDate) { - this.description = description; - this.createDate = createDate; - } - - public TodoItem() { - // default no arg constructor - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - - public LocalDateTime getCreateDate() { - return createDate; - } - - public void setCreateDate(LocalDateTime createDate) { - this.createDate = createDate; - } - - @Override - public String toString() { - return "TodoItem [description=" + description + ", createDate=" + createDate + "]"; - } -} +package com.baeldung.sessionattrs; + +import java.time.LocalDateTime; + +public class TodoItem { + + private String description; + private LocalDateTime createDate; + + public TodoItem(String description, LocalDateTime createDate) { + this.description = description; + this.createDate = createDate; + } + + public TodoItem() { + // default no arg constructor + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public LocalDateTime getCreateDate() { + return createDate; + } + + public void setCreateDate(LocalDateTime createDate) { + this.createDate = createDate; + } + + @Override + public String toString() { + return "TodoItem [description=" + description + ", createDate=" + createDate + "]"; + } +} diff --git a/spring-5/src/main/java/com/baeldung/sessionattrs/TodoList.java b/spring-mvc-forms-thymeleaf/src/main/java/com/baeldung/sessionattrs/TodoList.java similarity index 94% rename from spring-5/src/main/java/com/baeldung/sessionattrs/TodoList.java rename to spring-mvc-forms-thymeleaf/src/main/java/com/baeldung/sessionattrs/TodoList.java index cad7811da4..02c05f4282 100644 --- a/spring-5/src/main/java/com/baeldung/sessionattrs/TodoList.java +++ b/spring-mvc-forms-thymeleaf/src/main/java/com/baeldung/sessionattrs/TodoList.java @@ -1,8 +1,8 @@ -package com.baeldung.sessionattrs; - -import java.util.ArrayDeque; - -@SuppressWarnings("serial") -public class TodoList extends ArrayDeque{ - -} +package com.baeldung.sessionattrs; + +import java.util.ArrayDeque; + +@SuppressWarnings("serial") +public class TodoList extends ArrayDeque{ + +} diff --git a/spring-mvc-forms-thymeleaf/src/main/resources/application.properties b/spring-mvc-forms-thymeleaf/src/main/resources/application.properties new file mode 100644 index 0000000000..ccec014c2b --- /dev/null +++ b/spring-mvc-forms-thymeleaf/src/main/resources/application.properties @@ -0,0 +1,3 @@ +server.port=8081 + +logging.level.root=INFO \ No newline at end of file diff --git a/spring-5/src/main/resources/templates/sessionattrs/index.html b/spring-mvc-forms-thymeleaf/src/main/resources/templates/sessionattrs/index.html similarity index 97% rename from spring-5/src/main/resources/templates/sessionattrs/index.html rename to spring-mvc-forms-thymeleaf/src/main/resources/templates/sessionattrs/index.html index 72427cd62b..f9df863e45 100644 --- a/spring-5/src/main/resources/templates/sessionattrs/index.html +++ b/spring-mvc-forms-thymeleaf/src/main/resources/templates/sessionattrs/index.html @@ -1,27 +1,27 @@ - - - - Session Scope in Spring MVC - - - - - - - -
-

-

Session Scope in Spring MVC - Example

-

-
- - + + + + Session Scope in Spring MVC + + + + + + + +
+

+

Session Scope in Spring MVC - Example

+

+
+ + \ No newline at end of file diff --git a/spring-5/src/main/resources/templates/sessionattrs/scopedproxyform.html b/spring-mvc-forms-thymeleaf/src/main/resources/templates/sessionattrs/scopedproxyform.html similarity index 98% rename from spring-5/src/main/resources/templates/sessionattrs/scopedproxyform.html rename to spring-mvc-forms-thymeleaf/src/main/resources/templates/sessionattrs/scopedproxyform.html index e72651556b..604c60dfbb 100644 --- a/spring-5/src/main/resources/templates/sessionattrs/scopedproxyform.html +++ b/spring-mvc-forms-thymeleaf/src/main/resources/templates/sessionattrs/scopedproxyform.html @@ -1,27 +1,27 @@ - - - - Session Scope in Spring MVC - - - - - - - -
-

-

Scoped Proxy Example

-

-
-
-
Enter a TODO
-
-
- - -
-
-
- + + + + Session Scope in Spring MVC + + + + + + + +
+

+

Scoped Proxy Example

+

+
+
+
Enter a TODO
+
+
+ + +
+
+
+ \ No newline at end of file diff --git a/spring-5/src/main/resources/templates/sessionattrs/scopedproxytodos.html b/spring-mvc-forms-thymeleaf/src/main/resources/templates/sessionattrs/scopedproxytodos.html similarity index 97% rename from spring-5/src/main/resources/templates/sessionattrs/scopedproxytodos.html rename to spring-mvc-forms-thymeleaf/src/main/resources/templates/sessionattrs/scopedproxytodos.html index 5493b5cf64..743cd76b5d 100644 --- a/spring-5/src/main/resources/templates/sessionattrs/scopedproxytodos.html +++ b/spring-mvc-forms-thymeleaf/src/main/resources/templates/sessionattrs/scopedproxytodos.html @@ -1,48 +1,48 @@ - - - - Session Scope in Spring MVC - - - - - - - -
-

-

Scoped Proxy Example

-

-
-
-
-
- Add New -
-
-
-
-
-
TODO List
- - - - - - - - - - - -
DescriptionCreate Date
DescriptionCreate Date
-
-
-
-
- + + + + Session Scope in Spring MVC + + + + + + + +
+

+

Scoped Proxy Example

+

+
+
+
+
+ Add New +
+
+
+
+
+
TODO List
+ + + + + + + + + + + +
DescriptionCreate Date
DescriptionCreate Date
+
+
+
+
+ \ No newline at end of file diff --git a/spring-5/src/main/resources/templates/sessionattrs/sessionattributesform.html b/spring-mvc-forms-thymeleaf/src/main/resources/templates/sessionattrs/sessionattributesform.html similarity index 98% rename from spring-5/src/main/resources/templates/sessionattrs/sessionattributesform.html rename to spring-mvc-forms-thymeleaf/src/main/resources/templates/sessionattrs/sessionattributesform.html index 28e1d5d2c1..93e052d6b5 100644 --- a/spring-5/src/main/resources/templates/sessionattrs/sessionattributesform.html +++ b/spring-mvc-forms-thymeleaf/src/main/resources/templates/sessionattrs/sessionattributesform.html @@ -1,27 +1,27 @@ - - - - Session Scope in Spring MVC - - - - - - - -
-

-

Session Attributes Example

-

-
-
-
Enter a TODO
-
-
- - -
-
-
- + + + + Session Scope in Spring MVC + + + + + + + +
+

+

Session Attributes Example

+

+
+
+
Enter a TODO
+
+
+ + +
+
+
+ \ No newline at end of file diff --git a/spring-5/src/main/resources/templates/sessionattrs/sessionattributestodos.html b/spring-mvc-forms-thymeleaf/src/main/resources/templates/sessionattrs/sessionattributestodos.html similarity index 97% rename from spring-5/src/main/resources/templates/sessionattrs/sessionattributestodos.html rename to spring-mvc-forms-thymeleaf/src/main/resources/templates/sessionattrs/sessionattributestodos.html index 4bae12ffb9..7525604298 100644 --- a/spring-5/src/main/resources/templates/sessionattrs/sessionattributestodos.html +++ b/spring-mvc-forms-thymeleaf/src/main/resources/templates/sessionattrs/sessionattributestodos.html @@ -1,48 +1,48 @@ - - - - Session Scope in Spring MVC - - - - - - - -
-

-

Session Attributes Example

-

-
-
-
-
- Add New -
-
-
-
-
-
TODO List
- - - - - - - - - - - -
DescriptionCreate Date
DescriptionCreate Date
-
-
-
-
- + + + + Session Scope in Spring MVC + + + + + + + +
+

+

Session Attributes Example

+

+
+
+
+
+ Add New +
+
+
+
+
+
TODO List
+ + + + + + + + + + + +
DescriptionCreate Date
DescriptionCreate Date
+
+
+
+
+ \ No newline at end of file diff --git a/spring-5/src/test/java/com/baeldung/sessionattrs/SessionAttrsApplicationIntegrationTest.java b/spring-mvc-forms-thymeleaf/src/test/java/com/baeldung/sessionattrs/SessionAttrsApplicationTest.java similarity index 85% rename from spring-5/src/test/java/com/baeldung/sessionattrs/SessionAttrsApplicationIntegrationTest.java rename to spring-mvc-forms-thymeleaf/src/test/java/com/baeldung/sessionattrs/SessionAttrsApplicationTest.java index 05e9cdd21e..2fc27841da 100644 --- a/spring-5/src/test/java/com/baeldung/sessionattrs/SessionAttrsApplicationIntegrationTest.java +++ b/spring-mvc-forms-thymeleaf/src/test/java/com/baeldung/sessionattrs/SessionAttrsApplicationTest.java @@ -7,7 +7,7 @@ import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest -public class SessionAttrsApplicationIntegrationTest { +public class SessionAttrsApplicationTest { @Test public void contextLoads() { diff --git a/spring-5/src/test/java/com/baeldung/sessionattrs/TestConfig.java b/spring-mvc-forms-thymeleaf/src/test/java/com/baeldung/sessionattrs/TestConfig.java similarity index 97% rename from spring-5/src/test/java/com/baeldung/sessionattrs/TestConfig.java rename to spring-mvc-forms-thymeleaf/src/test/java/com/baeldung/sessionattrs/TestConfig.java index 07d65dd7c2..cf38463c0f 100644 --- a/spring-5/src/test/java/com/baeldung/sessionattrs/TestConfig.java +++ b/spring-mvc-forms-thymeleaf/src/test/java/com/baeldung/sessionattrs/TestConfig.java @@ -1,17 +1,17 @@ -package com.baeldung.sessionattrs; - -import org.springframework.beans.factory.config.CustomScopeConfigurer; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.support.SimpleThreadScope; - -@Configuration -public class TestConfig { - - @Bean - public CustomScopeConfigurer customScopeConfigurer() { - CustomScopeConfigurer configurer = new CustomScopeConfigurer(); - configurer.addScope("session", new SimpleThreadScope()); - return configurer; - } -} +package com.baeldung.sessionattrs; + +import org.springframework.beans.factory.config.CustomScopeConfigurer; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.support.SimpleThreadScope; + +@Configuration +public class TestConfig { + + @Bean + public CustomScopeConfigurer customScopeConfigurer() { + CustomScopeConfigurer configurer = new CustomScopeConfigurer(); + configurer.addScope("session", new SimpleThreadScope()); + return configurer; + } +} diff --git a/spring-5/src/test/java/com/baeldung/sessionattrs/TodoControllerWithScopedProxyIntegrationTest.java b/spring-mvc-forms-thymeleaf/src/test/java/com/baeldung/sessionattrs/TodoControllerWithScopedProxyTest.java similarity index 91% rename from spring-5/src/test/java/com/baeldung/sessionattrs/TodoControllerWithScopedProxyIntegrationTest.java rename to spring-mvc-forms-thymeleaf/src/test/java/com/baeldung/sessionattrs/TodoControllerWithScopedProxyTest.java index 5b2f653f8a..49be948a2a 100644 --- a/spring-5/src/test/java/com/baeldung/sessionattrs/TodoControllerWithScopedProxyIntegrationTest.java +++ b/spring-mvc-forms-thymeleaf/src/test/java/com/baeldung/sessionattrs/TodoControllerWithScopedProxyTest.java @@ -1,68 +1,68 @@ -package com.baeldung.sessionattrs; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.context.annotation.Import; -import org.springframework.test.context.junit4.SpringRunner; -import org.springframework.test.web.servlet.MockMvc; -import org.springframework.test.web.servlet.MvcResult; -import org.springframework.test.web.servlet.setup.MockMvcBuilders; -import org.springframework.util.StringUtils; -import org.springframework.web.context.WebApplicationContext; - -@RunWith(SpringRunner.class) -@SpringBootTest -@AutoConfigureMockMvc -@Import(TestConfig.class) -public class TodoControllerWithScopedProxyIntegrationTest { - - @Autowired - private MockMvc mockMvc; - - @Autowired - private WebApplicationContext wac; - - @Before - public void setup() { - this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac) - .build(); - } - - @Test - public void whenFirstRequest_thenContainsAllCategoriesAndUnintializedTodo() throws Exception { - MvcResult result = mockMvc.perform(get("/scopedproxy/form")) - .andExpect(status().isOk()) - .andExpect(model().attributeExists("todo")) - .andReturn(); - - TodoItem item = (TodoItem) result.getModelAndView().getModel().get("todo"); - assertTrue(StringUtils.isEmpty(item.getDescription())); - } - - @Test - public void whenSubmit_thenSubsequentFormRequestContainsMostRecentTodo() throws Exception { - mockMvc.perform(post("/scopedproxy/form") - .param("description", "newtodo")) - .andExpect(status().is3xxRedirection()) - .andReturn(); - - MvcResult result = mockMvc.perform(get("/scopedproxy/form")) - .andExpect(status().isOk()) - .andExpect(model().attributeExists("todo")) - .andReturn(); - TodoItem item = (TodoItem) result.getModelAndView().getModel().get("todo"); - assertEquals("newtodo", item.getDescription()); - } - -} +package com.baeldung.sessionattrs; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Import; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.MvcResult; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.util.StringUtils; +import org.springframework.web.context.WebApplicationContext; + +@RunWith(SpringRunner.class) +@SpringBootTest +@AutoConfigureMockMvc +@Import(TestConfig.class) +public class TodoControllerWithScopedProxyTest { + + @Autowired + private MockMvc mockMvc; + + @Autowired + private WebApplicationContext wac; + + @Before + public void setup() { + this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac) + .build(); + } + + @Test + public void whenFirstRequest_thenContainsUnintializedTodo() throws Exception { + MvcResult result = mockMvc.perform(get("/scopedproxy/form")) + .andExpect(status().isOk()) + .andExpect(model().attributeExists("todo")) + .andReturn(); + + TodoItem item = (TodoItem) result.getModelAndView().getModel().get("todo"); + assertTrue(StringUtils.isEmpty(item.getDescription())); + } + + @Test + public void whenSubmit_thenSubsequentFormRequestContainsMostRecentTodo() throws Exception { + mockMvc.perform(post("/scopedproxy/form") + .param("description", "newtodo")) + .andExpect(status().is3xxRedirection()) + .andReturn(); + + MvcResult result = mockMvc.perform(get("/scopedproxy/form")) + .andExpect(status().isOk()) + .andExpect(model().attributeExists("todo")) + .andReturn(); + TodoItem item = (TodoItem) result.getModelAndView().getModel().get("todo"); + assertEquals("newtodo", item.getDescription()); + } + +} diff --git a/spring-5/src/test/java/com/baeldung/sessionattrs/TodoControllerWithSessionAttributesIntegrationTest.java b/spring-mvc-forms-thymeleaf/src/test/java/com/baeldung/sessionattrs/TodoControllerWithSessionAttributesTest.java similarity index 95% rename from spring-5/src/test/java/com/baeldung/sessionattrs/TodoControllerWithSessionAttributesIntegrationTest.java rename to spring-mvc-forms-thymeleaf/src/test/java/com/baeldung/sessionattrs/TodoControllerWithSessionAttributesTest.java index c97fc79f29..e41de880d3 100644 --- a/spring-5/src/test/java/com/baeldung/sessionattrs/TodoControllerWithSessionAttributesIntegrationTest.java +++ b/spring-mvc-forms-thymeleaf/src/test/java/com/baeldung/sessionattrs/TodoControllerWithSessionAttributesTest.java @@ -1,68 +1,68 @@ -package com.baeldung.sessionattrs; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringRunner; -import org.springframework.test.web.servlet.MockMvc; -import org.springframework.test.web.servlet.MvcResult; -import org.springframework.test.web.servlet.setup.MockMvcBuilders; -import org.springframework.util.StringUtils; -import org.springframework.web.context.WebApplicationContext; -import org.springframework.web.servlet.FlashMap; - -@RunWith(SpringRunner.class) -@SpringBootTest -@AutoConfigureMockMvc -public class TodoControllerWithSessionAttributesIntegrationTest { - - @Autowired - private MockMvc mockMvc; - - @Autowired - private WebApplicationContext wac; - - @Before - public void setup() { - this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac) - .build(); - } - - @Test - public void whenFirstRequest_thenContainsUnintializedTodo() throws Exception { - MvcResult result = mockMvc.perform(get("/sessionattributes/form")) - .andExpect(status().isOk()) - .andExpect(model().attributeExists("todo")) - .andReturn(); - - TodoItem item = (TodoItem) result.getModelAndView().getModel().get("todo"); - assertTrue(StringUtils.isEmpty(item.getDescription())); - } - - @Test - public void whenSubmit_thenSubsequentFormRequestContainsMostRecentTodo() throws Exception { - FlashMap flashMap = mockMvc.perform(post("/sessionattributes/form") - .param("description", "newtodo")) - .andExpect(status().is3xxRedirection()) - .andReturn().getFlashMap(); - - MvcResult result = mockMvc.perform(get("/sessionattributes/form") - .sessionAttrs(flashMap)) - .andExpect(status().isOk()) - .andExpect(model().attributeExists("todo")) - .andReturn(); - TodoItem item = (TodoItem) result.getModelAndView().getModel().get("todo"); - assertEquals("newtodo", item.getDescription()); - } - -} +package com.baeldung.sessionattrs; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.MvcResult; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.util.StringUtils; +import org.springframework.web.context.WebApplicationContext; +import org.springframework.web.servlet.FlashMap; + +@RunWith(SpringRunner.class) +@SpringBootTest +@AutoConfigureMockMvc +public class TodoControllerWithSessionAttributesTest { + + @Autowired + private MockMvc mockMvc; + + @Autowired + private WebApplicationContext wac; + + @Before + public void setup() { + this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac) + .build(); + } + + @Test + public void whenFirstRequest_thenContainsUnintializedTodo() throws Exception { + MvcResult result = mockMvc.perform(get("/sessionattributes/form")) + .andExpect(status().isOk()) + .andExpect(model().attributeExists("todo")) + .andReturn(); + + TodoItem item = (TodoItem) result.getModelAndView().getModel().get("todo"); + assertTrue(StringUtils.isEmpty(item.getDescription())); + } + + @Test + public void whenSubmit_thenSubsequentFormRequestContainsMostRecentTodo() throws Exception { + FlashMap flashMap = mockMvc.perform(post("/sessionattributes/form") + .param("description", "newtodo")) + .andExpect(status().is3xxRedirection()) + .andReturn().getFlashMap(); + + MvcResult result = mockMvc.perform(get("/sessionattributes/form") + .sessionAttrs(flashMap)) + .andExpect(status().isOk()) + .andExpect(model().attributeExists("todo")) + .andReturn(); + TodoItem item = (TodoItem) result.getModelAndView().getModel().get("todo"); + assertEquals("newtodo", item.getDescription()); + } + +}