This repository was archived by the owner on Jan 13, 2023. It is now read-only.
File tree Expand file tree Collapse file tree 4 files changed +54
-1
lines changed
java/com/bobocode/web/controller
spring-framework-tutorial-model/src/main/java/com/bobocode/model Expand file tree Collapse file tree 4 files changed +54
-1
lines changed Original file line number Diff line number Diff line change 22
33import com .bobocode .model .Account ;
44import com .bobocode .util .TestDataGenerator ;
5+ import org .springframework .format .annotation .DateTimeFormat ;
56import org .springframework .stereotype .Controller ;
67import org .springframework .ui .Model ;
78import org .springframework .web .bind .annotation .GetMapping ;
9+ import org .springframework .web .bind .annotation .PostMapping ;
810import 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}
Original file line number Diff line number Diff line change 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 number Diff line number Diff line change 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 >
Original file line number Diff line number Diff line change 11package com .bobocode .model ;
22
3- import com .bobocode .model .Gender ;
43import lombok .*;
54
65import javax .persistence .*;
You can’t perform that action at this time.
0 commit comments