Skip to content

Commit 2b2fafb

Browse files
committed
Spring Boot form submission
1 parent 9bad262 commit 2b2fafb

File tree

9 files changed

+135
-0
lines changed

9 files changed

+135
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
target
2+
out
3+
.settings
4+
.classpath
5+
.project
6+
.idea
7+
*.iml
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.hellokoding.springboot</groupId>
5+
<artifactId>freemarker.form.submission</artifactId>
6+
<parent>
7+
<groupId>org.springframework.boot</groupId>
8+
<artifactId>spring-boot-starter-parent</artifactId>
9+
<version>2.1.4.RELEASE</version>
10+
</parent>
11+
12+
<properties>
13+
<java.version>1.7</java.version>
14+
</properties>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>org.springframework.boot</groupId>
19+
<artifactId>spring-boot-starter-web</artifactId>
20+
</dependency>
21+
<dependency>
22+
<groupId>org.springframework.boot</groupId>
23+
<artifactId>spring-boot-starter-freemarker</artifactId>
24+
</dependency>
25+
</dependencies>
26+
<build>
27+
<plugins>
28+
<plugin>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-maven-plugin</artifactId>
31+
</plugin>
32+
</plugins>
33+
</build>
34+
</project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.hellokoding.springboot;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.ui.Model;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.PostMapping;
7+
8+
@Controller
9+
public class FormController {
10+
@GetMapping("/")
11+
public String index() {
12+
return "redirect:/form";
13+
}
14+
15+
@GetMapping("/form")
16+
public String formGet() {
17+
return "form";
18+
}
19+
20+
@PostMapping("/form")
21+
public String formPost(User user, Model model) {
22+
model.addAttribute("user", user);
23+
return "form";
24+
}
25+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.hellokoding.springboot;
2+
3+
public class User {
4+
private String firstName;
5+
private String lastName;
6+
7+
public String getFirstName() {
8+
return firstName;
9+
}
10+
11+
public void setFirstName(String firstName) {
12+
this.firstName = firstName;
13+
}
14+
15+
public String getLastName() {
16+
return lastName;
17+
}
18+
19+
public void setLastName(String lastName) {
20+
this.lastName = lastName;
21+
}
22+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.hellokoding.springboot;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class WebApplication {
8+
public static void main(String[] args) throws Exception {
9+
SpringApplication.run(WebApplication.class, args);
10+
}
11+
}
12+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
spring.freemarker.template-loader-path: classpath:/templates
2+
spring.freemarker.suffix: .ftl
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
h2 {
2+
color: darkgreen;
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
(function(){
2+
console.log("Hello World!");
3+
})();
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Form example with Java, Spring Boot, FreeMarker</title>
6+
<link href="/css/main.css" rel="stylesheet">
7+
</head>
8+
<body>
9+
<h2>Handling Form Submission example with Java, Spring Boot, FreeMarker</h2>
10+
<#if user?? >
11+
Your submitted data<br>
12+
First name: ${user.firstName}<br>
13+
Last name: ${user.lastName}<br>
14+
<#else>
15+
<form action="/form" method="post">
16+
First name:<br>
17+
<input type="text" name="firstName">
18+
<br><br>
19+
Last name:<br>
20+
<input type="text" name="lastName">
21+
<br><br>
22+
<input type="submit" value="Submit">
23+
</form>
24+
</#if>
25+
<script src="/js/main.js"></script>
26+
</body>
27+
</html>

0 commit comments

Comments
 (0)