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

質問編集履歴

2

全体的にわかりにくかったため、詳細を記述した

2021/05/22 23:39

投稿

hito2718
hito2718

スコア0

title CHANGED
@@ -1,1 +1,1 @@
1
- Spring Tool Suite 4で作成しているプログラムの画面遷移について
1
+ Spring Tool Suite 4で作成しているプログラムのコントロール制御について
body CHANGED
@@ -1,48 +1,214 @@
1
1
  #なぜ今回質問したのか
2
+ プログラムのコントロール制御ができなく原因が不明なため
2
3
 
3
- Spring Tool Suite 4で画面の遷移をしたい。
4
- 自分が知りうる以外で、何か問題があるかもしれないと思ったため。
5
4
 
6
-
7
-
8
5
  #ご質問
9
6
  Spring Tool Suite 4 にて開発をしております。
10
7
  以下のように画面遷移するプログラムを作成しています。
11
- 最初の画面Aから遷移して、画面Bに行き最初の画面Aに戻る単純なプログラムです。
12
8
 
9
+ ##index.html→home.html→edit.html→home.html
13
10
 
11
+ #最終的にしたいこと
14
12
 
13
+ 上記の画面遷移し最終的にHomeServletの中のhomeメソッドの中の処理に入ることです。
14
+ ブレークポイントは以下で設定しています。
15
+  index.htmlから遷移すると以下のブレークポイントに止まりますが、
16
+ edit.htmlから遷移するとhome.htmlに遷移するだけで以下のブレークポイントに止まりません。
15
17
 
16
- ##A画面→B画面→A画面
17
18
 
19
+ ・HomeServlet.javaの中のブレークポイント設定行
20
+ mav.setViewName("home");
18
21
 
19
22
 
20
- B画面からA画面へ戻る場合に、コントローラに記述するメソッドは、
21
- 以下のようなプロぐグラムになる思います
23
+ ##試したこ
22
24
 
25
+ 下記の簡単なソースコードを作成し、動かしていますが予想と違った動きをします。
26
+ すべてのソースコードを下記に記述します。
23
27
 
28
+
29
+ #ソースコード
30
+
31
+
32
+
24
33
  ```
34
+ index.html
35
+
36
+ <!DOCTYPE html>
37
+ <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
38
+ <head>
39
+ <meta charset="UTF-8">
40
+ <title>
41
+ index
42
+ </title>
43
+ </head>
44
+ <br>
45
+ <body>
46
+ INDEXページです
47
+
48
+ <form th:action="@{/home}" method="POST">
49
+ <p align="center">
50
+ <td align="center">
51
+ <input type="submit" value="HOME">
52
+ </td>
53
+ </p>
54
+ </form>
55
+ </body>
56
+ </html>
57
+ ```
58
+
59
+
60
+ ```
61
+ home.html
62
+
63
+ <!DOCTYPE html>
64
+ <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
65
+ <head>
66
+ <meta charset="UTF-8">
67
+ <title>
68
+ home
69
+ </title>
70
+ </head>
71
+ <br>
72
+ <body>
73
+ HOMEページです
74
+ <form th:action="@{/edit}" method="POST">
75
+ <p align="center">
76
+ <td align="center">
77
+ <input type="submit" name="paramedit" value="EDIT">
78
+ </td>
79
+ </p>
80
+ </form>
81
+ </body>
82
+ </html>
83
+ ```
84
+
85
+ ```
86
+ edit.html
87
+
88
+ <!DOCTYPE html>
89
+ <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
90
+ <head>
91
+ <meta charset="UTF-8">
92
+ <title>
93
+ edit
94
+ </title>
95
+ </head>
96
+ <br>
97
+ <body>
98
+ EDITページです
99
+ <form th:action="@{/home}" method="POST">
100
+ <p align="center">
101
+ <td align="center">
102
+ <input type="submit" name="paramhome" value="HOME">
103
+ </td>
104
+ </p>
105
+ </form>
106
+ </body>
107
+ </html>
108
+ ```
109
+
110
+ ```
111
+ IndexServlet.java
112
+
113
+
114
+ package com.example.demo;
115
+
116
+
117
+ import org.springframework.stereotype.Controller;
118
+ import org.springframework.web.bind.annotation.GetMapping;
119
+
120
+
121
+
122
+ @Controller
123
+ public class IndexServlet {
124
+
125
+
126
+ @GetMapping("index")
127
+ public String index() {
128
+ return "index";
129
+ }
130
+
131
+
132
+ }
133
+
134
+
135
+ ```
136
+
137
+
138
+
139
+ ```
140
+ HomeServletjava
141
+
142
+ package com.example.demo;
143
+
144
+ import org.springframework.stereotype.Controller;
145
+ import org.springframework.web.bind.annotation.RequestMapping;
146
+ import org.springframework.web.bind.annotation.RequestMethod;
147
+ import org.springframework.web.bind.annotation.ResponseBody;
148
+ import org.springframework.web.servlet.ModelAndView;
149
+
150
+
151
+ @Controller
152
+ public class HomeServlet {
153
+
154
+
25
- @RequestMapping(path = "A", method = RequestMethod.POST)
155
+ @RequestMapping(path = "home", method = RequestMethod.POST)
26
- @ResponseBody
156
+ @ResponseBody
27
- public ModelAndView methodA(ModelAndView mav) {
157
+ public ModelAndView home(ModelAndView mav) {
158
+ //★ブレークポイント
28
- mav.setViewName("A");
159
+ mav.setViewName("home");
29
160
  return mav;
30
161
  }
162
+
163
+
164
+ }
165
+
166
+
31
167
  ```
32
168
 
33
- エラーメッセージは発生していなく画面も遷移するのですが、デバッグするとメソッドの先頭の1行目
169
+ ```
34
- に来ないで、画面だけ遷移して切り替わります。
170
+ EditServlet.java
35
171
 
36
- ##試したこと
172
+ package com.example.demo;
37
173
 
174
+ import org.springframework.stereotype.Controller;
38
- 業務ロジックを追加せず、一番単純なコード(上記のみ)を作成してみたら正常な動きはするのですが、自分が作成中のプログラムでは業務ロジック追加してるだけで上記と記述は同じはずですが、画面が切り替わるだけでメソッドの1行目に遷移しません。
175
+ import org.springframework.web.bind.annotation.RequestMapping;
176
+ import org.springframework.web.bind.annotation.RequestMethod;
177
+ import org.springframework.web.bind.annotation.ResponseBody;
178
+ import org.springframework.web.servlet.ModelAndView;
39
179
 
180
+ @Controller
181
+ public class EditServlet {
40
182
 
183
+
184
+ @RequestMapping(path = "edit", params = "paramedit", method = RequestMethod.POST)
185
+ @ResponseBody
186
+ public ModelAndView edit(ModelAndView mav) {
187
+ mav.setViewName("edit");
188
+ return mav;
189
+ }
190
+
191
+
192
+ @RequestMapping(path = "home", params = "paramhome", method = RequestMethod.POST)
193
+ @ResponseBody
194
+ public ModelAndView home(ModelAndView mav) {
195
+ mav.setViewName("home");
196
+ return mav;
197
+ }
198
+
199
+
200
+ }
41
201
 
42
202
 
43
- 抽象的な質問で申し訳ありませんが、原因が不明なのでご教授いただけると助かります。
44
203
 
204
+ ```
45
205
 
206
+
207
+
208
+
209
+
210
+
211
+
46
212
  ##環境
47
213
  ・OS WIN10
48
214
  ・フレームワーク:Spring Tool Suite4

1

タイトルを修正

2021/05/22 23:39

投稿

hito2718
hito2718

スコア0

title CHANGED
@@ -1,1 +1,1 @@
1
- STSの画面遷移について
1
+ Spring Tool Suite 4で作成しているプログラムの画面遷移について
body CHANGED
File without changes