This repository was archived by the owner on Jan 13, 2023. It is now read-only.
File tree 4 files changed +54
-1
lines changed
java/com/bobocode/web/controller
spring-framework-tutorial-model/src/main/java/com/bobocode/model
4 files changed +54
-1
lines changed Original file line number Diff line number Diff line change 2
2
3
3
import com .bobocode .model .Account ;
4
4
import com .bobocode .util .TestDataGenerator ;
5
+ import org .springframework .format .annotation .DateTimeFormat ;
5
6
import org .springframework .stereotype .Controller ;
6
7
import org .springframework .ui .Model ;
7
8
import org .springframework .web .bind .annotation .GetMapping ;
9
+ import org .springframework .web .bind .annotation .PostMapping ;
8
10
import org .springframework .web .bind .annotation .RequestMapping ;
9
11
10
12
@ Controller
@@ -17,4 +19,24 @@ public String generate(Model model) {
17
19
model .addAttribute ("account" , account );
18
20
return "account" ;
19
21
}
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
+ }
20
42
}
Original file line number Diff line number Diff line change 16
16
<p >${ account. email } </p >
17
17
<p >${ account. birthday } </p >
18
18
<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 >
19
20
</div >
20
21
</body >
21
22
</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 1
1
package com .bobocode .model ;
2
2
3
- import com .bobocode .model .Gender ;
4
3
import lombok .*;
5
4
6
5
import javax .persistence .*;
You can’t perform that action at this time.
0 commit comments