BAEL-1533 - Making a Spring MVC Form Remember Values
This commit is contained in:
@@ -199,7 +199,8 @@
|
||||
<module>spring-ldap</module>
|
||||
<module>spring-mockito</module>
|
||||
<module>spring-mvc-email</module>
|
||||
<module>spring-mvc-forms</module>
|
||||
<module>spring-mvc-forms-jsp</module>
|
||||
<module>spring-mvc-forms-thymeleaf</module>
|
||||
<module>spring-mvc-java</module>
|
||||
<module>spring-mvc-tiles</module>
|
||||
<module>spring-mvc-velocity</module>
|
||||
|
||||
+222
-215
File diff suppressed because it is too large
Load Diff
@@ -6,9 +6,9 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>0.1-SNAPSHOT</version>
|
||||
<artifactId>spring-mvc-forms</artifactId>
|
||||
<artifactId>spring-mvc-forms-jsp</artifactId>
|
||||
|
||||
<name>spring-mvc-forms</name>
|
||||
<name>spring-mvc-forms-jsp</name>
|
||||
<packaging>war</packaging>
|
||||
|
||||
<parent>
|
||||
@@ -0,0 +1,73 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>spring-mvc-forms-thymeleaf</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>spring-mvc-forms-thymeleaf</name>
|
||||
<description>spring forms examples using thymeleaf</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.0.0.RELEASE</version>
|
||||
<relativePath /> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-thymeleaf</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- runtime and test scoped -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-devtools</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<configuration>
|
||||
<forkCount>3</forkCount>
|
||||
<reuseForks>true</reuseForks>
|
||||
<parallel>methods</parallel>
|
||||
<useUnlimitedThreads>true</useUnlimitedThreads>
|
||||
<excludes>
|
||||
<exclude>**/*IntegrationTest.java</exclude>
|
||||
<exclude>**/*LiveTest.java</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+44
-44
@@ -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;
|
||||
}
|
||||
}
|
||||
+45
-45
@@ -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";
|
||||
}
|
||||
}
|
||||
+55
-55
@@ -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();
|
||||
}
|
||||
}
|
||||
+39
-39
@@ -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 + "]";
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user