Skip to content
This repository was archived by the owner on Jan 13, 2023. It is now read-only.

Commit 95748d1

Browse files
committed
Add an example of how to handle POST request in Spring MVC controller
1 parent 7e9788c commit 95748d1

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed

mvc-basics/src/main/java/com/bobocode/web/controller/AccountController.java

+22
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
import com.bobocode.model.Account;
44
import com.bobocode.util.TestDataGenerator;
5+
import org.springframework.format.annotation.DateTimeFormat;
56
import org.springframework.stereotype.Controller;
67
import org.springframework.ui.Model;
78
import org.springframework.web.bind.annotation.GetMapping;
9+
import org.springframework.web.bind.annotation.PostMapping;
810
import org.springframework.web.bind.annotation.RequestMapping;
911

1012
@Controller
@@ -17,4 +19,24 @@ public String generate(Model model) {
1719
model.addAttribute("account", account);
1820
return "account";
1921
}
22+
23+
/**
24+
* Handles POST request and forwards {@link Account} instance to the account.jsp view. Suppose you have an HTML form
25+
* that uses method POST and submits data to this controller. Spring will handle the request, create {@link Account}
26+
* instance and set all account fields that match form input parameters.
27+
*
28+
* @param account instance that is created using HTML form input parameters
29+
* @param model created and passed by Spring
30+
* @return the view name
31+
*/
32+
@PostMapping
33+
public String post(@DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Account account, Model model) {
34+
model.addAttribute(account);
35+
return "account";
36+
}
37+
38+
@GetMapping("/add")
39+
public String getAccountForm() {
40+
return "accountForm";
41+
}
2042
}

mvc-basics/src/main/webapp/WEB-INF/views/account.jsp

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<p>${account.email}</p>
1717
<p>${account.birthday}</p>
1818
<a class="btn btn-primary btn-lg" href="/accounts/generate" role="button">Generate</a>
19+
<a class="btn btn-secondary btn-lg" href="/accounts/add" role="button">Enter data</a>
1920
</div>
2021
</body>
2122
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
6+
integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
7+
<title>Account</title>
8+
</head>
9+
<body>
10+
<div class="container" style="margin-top: 10%; width: 30%">
11+
<h3>Enter account data</h3>
12+
<form method="post" action="/accounts">
13+
<div class="form-group">
14+
<input id="firstNameInput" name="firstName" type="text" class="form-control" placeholder="First name"/>
15+
</div>
16+
<div class="form-group">
17+
<input id="lastNameInput" name="lastName" type="text" class="form-control" placeholder="Last name"/>
18+
</div>
19+
<div class="form-group">
20+
<input id="emailInput" name="email" type="text" class="form-control" placeholder="Email"/>
21+
</div>
22+
<div class="form-group">
23+
<input id="birthday" name="birthday" type="date" class="form-control" placeholder="Birthday"/>
24+
</div>
25+
26+
<button type="submit" class="btn btn-primary">Submit</button>
27+
</form>
28+
</div>
29+
30+
</body>
31+
</html>

spring-framework-tutorial-model/src/main/java/com/bobocode/model/Account.java

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.bobocode.model;
22

3-
import com.bobocode.model.Gender;
43
import lombok.*;
54

65
import javax.persistence.*;

0 commit comments

Comments
 (0)