teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

ソースコードの追加

2020/07/28 09:22

投稿

k-kenta
k-kenta

スコア2

title CHANGED
File without changes
body CHANGED
@@ -4,18 +4,110 @@
4
4
  名前・性別・生年月日・電話番号を入力する入力フォームを作りたいと思っています。
5
5
  (名前・電話番号はテキストボックス、性別はラジオボタン、生年月日はプルダウン)
6
6
 
7
- ### 発生している問題・エラーメッセージ
8
- 入力画面において、性別と生年月日の表示はうまくいくのですが
9
- 確認画面での表示がうまくいきません。
10
7
 
11
- ###質問したいこと
8
+ 【実現したいこと
9
+ ・入力画面においてプルダウンで生年月日を入力できるようになる
12
- テキストボックスは入力したもの出力させてば良いの、理屈はわかので
10
+ ・確認画面において入力した生年月日表示ように
13
- ボタンなど選択式だと途端にわからなくなります。
14
- そこで
15
- ①ラジオボタン生年日の入力内容確認画面させたい。どうればよいですか?
11
+ ・ただし1~12月htmlで直書きるのはなく、javaでリストに設定してthymeleafのth:each属性で表示るようにする
16
- ②また、そのようになる理屈も教えて頂ければ幸いです。
17
12
 
13
+
14
+ 【ソースコード】
15
+ <入力画面>
18
- 初心者の質問で申し訳ありません。
16
+ <!DOCTYPE html>
17
+ <html xmlns:th="http://www.thymeleaf.org">
18
+ <head>
19
+ <meta charset="UTF-8">
19
- 「thymeleaf+入力フォーム」や「thymeleaf+ラジオボタン」で調べたのですが
20
+ <title>登録フォーム</title>
21
+ </head>
20
- まったくわかりませんでした。
22
+ <body>
23
+ <h1>お客様情報の入力</h1>
24
+ <form method="POST" action="output.html" th:action="@{output}">
25
+ <p>お名前</p><input type="text" name="name">
26
+ <br>
27
+ <p>性別</p>
28
+ <input type="radio" name="sex" value="male">男
29
+ <input type="radio" name="sex" value="female">女
30
+ <br>
31
+ <p>生年月日</p>
32
+ <select name="year"><option></option></select>年
33
+ <select name="month"><option></option></select>月
34
+ <select name="day"><option></option></select>日
35
+ <p>電話番号</p><input type="text" name="tel">
36
+ <br>
37
+ <input type="submit" name="登録">
38
+ </form>
39
+ </body>
40
+ </html>
41
+
42
+
43
+ <確認画面>
44
+ <!DOCTYPE html>
45
+ <html xmlns:th="http://www.thymeleaf.org">
46
+ <head>
47
+ <meta charset="UTF-8">
48
+ <title>確認画面</title>
49
+ </head>
50
+ <body>
51
+ <h1>ご入力の確認</h1>
52
+ <p>お名前:<span th:text="${name}"> 100</span></p>
53
+ <p>性別:<div th:switch="${sex}">
54
+ <p th:case ="1">男</p>
55
+ <p th:case ="2">女</p>
56
+ </div>
57
+ <p>生年月日:</p>
58
+ <p>電話番号:<span th:text="${tel}"> テスト</span></p>
59
+ <a href="index.html" th:href="@{input}"> 顧客の入力に戻る</a>
60
+ </body>
61
+ </html>
62
+
63
+
64
+
65
+ 【コントローラー】
66
+ package com.example.demo.controller;
67
+
68
+ import org.springframework.stereotype.Controller;
69
+ import org.springframework.ui.Model;
70
+ import org.springframework.web.bind.annotation.RequestMapping;
71
+ import org.springframework.web.bind.annotation.RequestParam;
72
+
73
+ @Controller
74
+ public class ClientController {
75
+
76
+ @RequestMapping("/input")
77
+ public String index() {
78
+ return "index.html";
79
+ }
80
+
81
+ @RequestMapping("/output")
82
+ public String result(@RequestParam("name")String name,
83
+ @RequestParam("tel") String tel,
84
+ @RequestParam("sex") String sex,Model model) {
85
+ model.addAttribute("name", name);
86
+ model.addAttribute("tel", tel);
87
+ if("male".equals(sex)){
88
+ model.addAttribute("sex", 1);
89
+ }else if("female".equals(sex)){
90
+ model.addAttribute("sex", 2);
91
+ }
92
+ return "output.html";
93
+ }
94
+ }
95
+
96
+
97
+ 【DemoApplication.java】
98
+ package com.example.demo;
99
+
100
+ import org.springframework.boot.SpringApplication;
101
+ import org.springframework.boot.autoconfigure.SpringBootApplication;
102
+
103
+ @SpringBootApplication
104
+ public class DemoApplication {
105
+
106
+ public static void main(String[] args) {
107
+ SpringApplication.run(DemoApplication.class, args);
108
+ }
109
+
110
+ }
111
+
112
+ 未熟な故、回答の表現があいまいになり、ご理解いただけない部分があるかもしれませんが
21
- よろしくお願いいたします。
113
+ できるだけお伝えさせていただきます。よろしくお願いいたします。