質問編集履歴
1
文法の修正
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
入力フォームの値を保持したい
|
test
CHANGED
@@ -1,102 +1,152 @@
|
|
1
|
-
###
|
1
|
+
### 解決したいこと
|
2
|
+
|
3
|
+
今Webアプリを作っています。入力フォームに値を保持したいのですが、うまくいきません。
|
4
|
+
|
5
|
+
表以外はうまくいったのですが、表の中の入力フォームはうまく値を保持することができませんでした
|
6
|
+
|
7
|
+
ネットでも色々調べたのですが、そもそもtableを入力フォームにしている事例があまりなく困っています
|
8
|
+
|
9
|
+
### 発生している問題・エラー
|
10
|
+
|
11
|
+
1,2枚目で入力して投稿ボタンを押してエラーがあった場合1,2枚目で入力した値をそのままにしたいのですが3,4枚目のとおりうまくいってません。
|
12
|
+
|
13
|
+
![イメージ説明](d802c2653f7d6cfc58a871d1cf365f59.png)
|
14
|
+
|
15
|
+
![イメージ説明](9c9844cbef5f5d1d56e3cc19652bb31c.png)
|
16
|
+
|
17
|
+
![イメージ説明](5b01f5c79b25867812fc39e58ac3578a.png)
|
18
|
+
|
19
|
+
![イメージ説明](10a408f7befe90256f22b4d3f15257ac.png)
|
20
|
+
|
21
|
+
### 該当するソースコード
|
22
|
+
|
23
|
+
```java
|
24
|
+
|
25
|
+
<form method="POST" action="<c:url value='/purchases/new' />">
|
2
26
|
|
3
27
|
|
4
28
|
|
29
|
+
<label for="title">現場名</label><br />
|
5
30
|
|
31
|
+
<input type="text" name="title" value="${purchase.title}" />
|
6
32
|
|
7
|
-
<
|
33
|
+
<br />
|
8
34
|
|
9
35
|
|
10
36
|
|
37
|
+
<label for="delivery_date">発注日</label><br />
|
11
38
|
|
39
|
+
<input type="date" name="delivery_date" value="${purchase.delivery_date}" />
|
12
40
|
|
13
|
-
|
41
|
+
<br />
|
14
42
|
|
15
43
|
|
16
44
|
|
45
|
+
<label for="desired_delivery_date">納入希望日</label><br />
|
46
|
+
|
47
|
+
<input type="date" name="desired_delivery_date" value="${purchase.desired_delivery_date }" />
|
48
|
+
|
17
|
-
|
49
|
+
<br />
|
18
50
|
|
19
51
|
|
20
52
|
|
53
|
+
<label for="delivery_plase">納品住所</label><br />
|
54
|
+
|
55
|
+
<input type="text" name="delivery_plase" value="${login_orderer.address}" />
|
56
|
+
|
21
|
-
|
57
|
+
<br /><br />
|
22
58
|
|
23
59
|
|
24
60
|
|
25
|
-
|
61
|
+
<table>
|
26
62
|
|
27
|
-
<t
|
63
|
+
<tbody>
|
28
64
|
|
29
|
-
<t
|
65
|
+
<tr>
|
30
66
|
|
31
|
-
<t
|
67
|
+
<th>品番・品名</th>
|
32
68
|
|
33
|
-
<th
|
69
|
+
<th>数量</th>
|
34
70
|
|
35
|
-
<th
|
71
|
+
<th>単位</th>
|
36
72
|
|
37
|
-
<
|
73
|
+
</tr>
|
38
74
|
|
39
|
-
<
|
75
|
+
<c:choose>
|
40
76
|
|
41
|
-
<c:
|
77
|
+
<c:when test="${errorsMap != null }">
|
42
78
|
|
43
|
-
<tr>
|
79
|
+
<c:forEach items="${success }" var="success" varStatus="i">
|
44
80
|
|
45
|
-
<t
|
81
|
+
<tr>
|
46
82
|
|
47
|
-
<c:
|
83
|
+
<c:choose>
|
48
84
|
|
49
|
-
<
|
85
|
+
<c:when test="${success.item != null }">
|
50
86
|
|
51
|
-
<
|
87
|
+
<td><input type="text" name="items" value="${success.item }"></td>
|
52
88
|
|
53
|
-
<c:
|
89
|
+
</c:when>
|
54
90
|
|
55
|
-
|
91
|
+
<c:otherwise>
|
56
92
|
|
57
|
-
<
|
93
|
+
<td><input type="text" name="items" value=""></td>
|
58
94
|
|
59
|
-
</c:
|
95
|
+
</c:otherwise>
|
60
96
|
|
97
|
+
</c:choose>
|
61
98
|
|
99
|
+
<td><input type="text" name="volumes"
|
62
100
|
|
63
|
-
|
101
|
+
value="${success.volume }" /></td>
|
64
102
|
|
65
|
-
<c
|
103
|
+
<td><select name="units">
|
66
104
|
|
67
|
-
<
|
105
|
+
<option value="m">m</option>
|
68
106
|
|
69
|
-
</
|
107
|
+
<option value="㎡">㎡</option>
|
70
108
|
|
71
|
-
<
|
109
|
+
<option value="式">式</option>
|
72
110
|
|
73
|
-
<
|
111
|
+
<option value="ケース">ケース</option>
|
74
112
|
|
75
|
-
</c
|
113
|
+
</select></td>
|
76
114
|
|
77
|
-
</
|
115
|
+
</tr>
|
78
116
|
|
117
|
+
</c:forEach>
|
79
118
|
|
119
|
+
</c:when>
|
80
120
|
|
81
|
-
<
|
121
|
+
<c:otherwise>
|
82
122
|
|
83
|
-
|
123
|
+
<c:forEach var="i" begin="0" end="9">
|
84
124
|
|
85
|
-
|
125
|
+
<tr>
|
86
126
|
|
87
|
-
|
127
|
+
<td><input type="text" name="items" value=""></td>
|
88
128
|
|
89
|
-
|
129
|
+
<td><input type="text" name="volumes" value="" /></td>
|
90
130
|
|
91
|
-
|
131
|
+
<td><select name="units">
|
92
132
|
|
93
|
-
<
|
133
|
+
<option value="m">m</option>
|
94
134
|
|
95
|
-
<
|
135
|
+
<option value="㎡">㎡</option>
|
96
136
|
|
97
|
-
</t
|
137
|
+
<option value="式">式</option>
|
98
138
|
|
139
|
+
<option value="ケース">ケース</option>
|
140
|
+
|
141
|
+
</select></td>
|
142
|
+
|
143
|
+
</tr>
|
144
|
+
|
99
|
-
|
145
|
+
</c:forEach>
|
146
|
+
|
147
|
+
</c:otherwise>
|
148
|
+
|
149
|
+
</c:choose>
|
100
150
|
|
101
151
|
</tbody>
|
102
152
|
|
@@ -104,382 +154,8 @@
|
|
104
154
|
|
105
155
|
```
|
106
156
|
|
107
|
-
|
157
|
+
### 自分で試したこと
|
108
158
|
|
109
|
-
p
|
159
|
+
入力フォームで受け取ったものをサーブレットのpostで"items"や"volumes"をListにいれてjspで出力したり
|
110
160
|
|
111
|
-
|
112
|
-
|
113
|
-
import java.io.IOException;
|
114
|
-
|
115
|
-
import java.sql.Date;
|
116
|
-
|
117
|
-
import java.sql.Timestamp;
|
118
|
-
|
119
|
-
import java.util.ArrayList;
|
120
|
-
|
121
|
-
import java.util.HashMap;
|
122
|
-
|
123
|
-
import java.util.List;
|
124
|
-
|
125
|
-
import java.util.Map;
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
import javax.persistence.EntityManager;
|
130
|
-
|
131
|
-
import javax.servlet.RequestDispatcher;
|
132
|
-
|
133
|
-
import javax.servlet.ServletException;
|
134
|
-
|
135
|
-
import javax.servlet.annotation.WebServlet;
|
136
|
-
|
137
|
-
import javax.servlet.http.HttpServlet;
|
138
|
-
|
139
|
-
import javax.servlet.http.HttpServletRequest;
|
140
|
-
|
141
|
-
import javax.servlet.http.HttpServletResponse;
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
import models.Connection;
|
146
|
-
|
147
|
-
import models.Contractor;
|
148
|
-
|
149
|
-
import models.Orderer;
|
150
|
-
|
151
|
-
import models.Purchase;
|
152
|
-
|
153
|
-
import models.validators.PurchaseTableErrorsValidator;
|
154
|
-
|
155
|
-
import utils.DBUtil;
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
/**
|
160
|
-
|
161
|
-
* Servlet implementation class PurchasesNewServlet
|
162
|
-
|
163
|
-
*/
|
164
|
-
|
165
|
-
@WebServlet("/purchases/new")
|
166
|
-
|
167
|
-
public class PurchasesNewServlet extends HttpServlet {
|
168
|
-
|
169
|
-
private static final long serialVersionUID = 1L;
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
/**
|
174
|
-
|
175
|
-
* @see HttpServlet#HttpServlet()
|
176
|
-
|
177
|
-
*/
|
178
|
-
|
179
|
-
public PurchasesNewServlet() {
|
180
|
-
|
181
|
-
super();
|
182
|
-
|
183
|
-
// TODO Auto-generated constructor stub
|
184
|
-
|
185
|
-
}
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
/**
|
190
|
-
|
191
|
-
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
|
192
|
-
|
193
|
-
*/
|
194
|
-
|
195
|
-
protected void doGet(HttpServletRequest request, HttpServletResponse response)
|
196
|
-
|
197
|
-
throws ServletException, IOException {
|
198
|
-
|
199
|
-
EntityManager em = DBUtil.createEntityManager();
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
Map<Integer, List<String>> errorsMap = new HashMap<Integer, List<String>>();
|
204
|
-
|
205
|
-
List<String> errors = new ArrayList <String>();
|
206
|
-
|
207
|
-
errors.add(null);
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
Connection select_connection = (Connection) request.getSession().getAttribute("select_connection");
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
em.close();
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
request.setAttribute("errorsMap", errorsMap);
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
request.setAttribute("connection", select_connection);
|
224
|
-
|
225
|
-
request.setAttribute("_token", request.getSession().getId());
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
Purchase p = new Purchase();
|
230
|
-
|
231
|
-
p.setDelivery_date(new Date(System.currentTimeMillis()));
|
232
|
-
|
233
|
-
p.setDesired_delivery_date(new Date(System.currentTimeMillis()));
|
234
|
-
|
235
|
-
request.setAttribute("purchase", p);
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/views/purchases/new.jsp");
|
240
|
-
|
241
|
-
rd.forward(request, response);
|
242
|
-
|
243
|
-
}
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
/**
|
248
|
-
|
249
|
-
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
|
250
|
-
|
251
|
-
*/
|
252
|
-
|
253
|
-
protected void doPost(HttpServletRequest request, HttpServletResponse response)
|
254
|
-
|
255
|
-
throws ServletException, IOException {
|
256
|
-
|
257
|
-
String _token = (String) request.getParameter("_token");
|
258
|
-
|
259
|
-
if (_token != null && _token.equals(request.getSession().getId())) {
|
260
|
-
|
261
|
-
EntityManager em = DBUtil.createEntityManager();
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
Map<Integer, List<String>> errorsMap = new HashMap<Integer, List<String>>();
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
String[] itemList = (String[])request.getParameterValues("items");
|
270
|
-
|
271
|
-
String[] volumeList = (String[])request.getParameterValues("volumes");
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
List<String> errors = new ArrayList <String>();
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
List<String> errorList = new ArrayList <String>();
|
280
|
-
|
281
|
-
String title = request.getParameter("title");
|
282
|
-
|
283
|
-
if(title == null || title.equals("")) {
|
284
|
-
|
285
|
-
errorList.add("現場名を入力してください。");
|
286
|
-
|
287
|
-
}
|
288
|
-
|
289
|
-
String delivery_plase = request.getParameter("delivery_plase");
|
290
|
-
|
291
|
-
if(delivery_plase == null || delivery_plase.equals("")) {
|
292
|
-
|
293
|
-
errorList.add("現場住所を入力してください");
|
294
|
-
|
295
|
-
}
|
296
|
-
|
297
|
-
Purchase p = new Purchase();
|
298
|
-
|
299
|
-
for (int i = 0; i < itemList.length; i++) { //フォームから入力したデータ文だけ繰り返す
|
300
|
-
|
301
|
-
if (!itemList[i].equals("") || !volumeList[i].equals("")) {
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
p.setOrderer((Orderer) request.getSession().getAttribute("login_orderer"));
|
306
|
-
|
307
|
-
p.setContractor((Contractor) request.getSession().getAttribute("select_contractor"));
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
p.setTitle(request.getParameter("title"));
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
Date delivery_date = new Date(System.currentTimeMillis());
|
316
|
-
|
317
|
-
String dd_str = request.getParameter("delivery_date");
|
318
|
-
|
319
|
-
if (dd_str != null && !dd_str.equals("")) {
|
320
|
-
|
321
|
-
delivery_date = Date.valueOf(request.getParameter("delivery_date"));
|
322
|
-
|
323
|
-
}
|
324
|
-
|
325
|
-
p.setDelivery_date(delivery_date);
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
Date desired_delivery_date = new Date(System.currentTimeMillis());
|
330
|
-
|
331
|
-
String ddd_str = request.getParameter("desired_delivery_date");
|
332
|
-
|
333
|
-
if (ddd_str != null && !ddd_str.equals("")) {
|
334
|
-
|
335
|
-
desired_delivery_date = Date.valueOf(request.getParameter("desired_delivery_date"));
|
336
|
-
|
337
|
-
}
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
p.setDesired_delivery_date(desired_delivery_date);
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
161
|
+
hashmapに入れてみたりpをjspで出力したりいろいろ試した結果サーブレットではなくjspがうまく書けていないのではないかと思っています。
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
String[] item = request.getParameterValues("items");
|
350
|
-
|
351
|
-
p.setItem(item[i]);
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
String[] volumes = request.getParameterValues("volumes");
|
356
|
-
|
357
|
-
try {
|
358
|
-
|
359
|
-
int volume = Integer.parseInt(volumes[i]);
|
360
|
-
|
361
|
-
p.setVolume(volume);
|
362
|
-
|
363
|
-
} catch(NumberFormatException e) {
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
}
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
String[] unit = request.getParameterValues("units");
|
372
|
-
|
373
|
-
p.setUnit(unit[i]);
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
p.setRemarks(request.getParameter("remarks"));
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
Timestamp currentTime = new Timestamp(System.currentTimeMillis());
|
382
|
-
|
383
|
-
p.setCreated_at(currentTime);
|
384
|
-
|
385
|
-
p.setUpdated_at(currentTime);
|
386
|
-
|
387
|
-
p.setDelete_flag(0);
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
errors = PurchaseTableErrorsValidator.validate(p);
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
if (errors.size() > 0) {
|
396
|
-
|
397
|
-
errorsMap.put(i, errors);
|
398
|
-
|
399
|
-
} else {
|
400
|
-
|
401
|
-
em.getTransaction().begin();
|
402
|
-
|
403
|
-
em.persist(p);
|
404
|
-
|
405
|
-
em.getTransaction().commit();
|
406
|
-
|
407
|
-
}
|
408
|
-
|
409
|
-
}
|
410
|
-
|
411
|
-
}
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
em.close();
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
if(errorsMap.size() > 0 || errorList.size() > 0) {
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
Orderer login_orderer = (Orderer)request.getSession().getAttribute("login_orderer");
|
424
|
-
|
425
|
-
Connection select_connection = (Connection) request.getSession().getAttribute("select_connection");
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
request.setAttribute("login_orderer", login_orderer);
|
430
|
-
|
431
|
-
request.setAttribute("connection", select_connection);
|
432
|
-
|
433
|
-
request.setAttribute("_token", request.getSession().getId());
|
434
|
-
|
435
|
-
request.setAttribute("errorList", errorList);
|
436
|
-
|
437
|
-
request.setAttribute("errors", errors);
|
438
|
-
|
439
|
-
request.setAttribute("purchase", p);
|
440
|
-
|
441
|
-
request.setAttribute("errorsMap", errorsMap);
|
442
|
-
|
443
|
-
request.setAttribute("flush", "入力内容にエラーがあります");
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/views/purchases/new.jsp");
|
448
|
-
|
449
|
-
rd.forward(request, response);
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
} else {
|
454
|
-
|
455
|
-
request.getSession().setAttribute("flush", "登録が完了しました。");
|
456
|
-
|
457
|
-
response.sendRedirect(request.getContextPath() + "/orderers/index");
|
458
|
-
|
459
|
-
}
|
460
|
-
|
461
|
-
}
|
462
|
-
|
463
|
-
}
|
464
|
-
|
465
|
-
}
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
```
|
470
|
-
|
471
|
-
### 試したこと
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
errorsMapというHashMapを作りnullではない場合空白、nullの場合は値を表示させる処理をforEachで10回繰り返す
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
### 補足情報(FW/ツールのバージョンなど)
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
EclipseのNeon.3 Release (4.6.3)を使っています。
|
484
|
-
|
485
|
-
フレームワークはhibernateです。
|