質問編集履歴

3

JSONデータとDAOのコードの追加。

2016/09/12 06:30

投稿

zenexvs
zenexvs

スコア12

test CHANGED
File without changes
test CHANGED
@@ -379,3 +379,117 @@
379
379
 
380
380
 
381
381
  ```
382
+
383
+ <DAO>
384
+
385
+
386
+
387
+ ```ここに言語を入力
388
+
389
+ package dao;
390
+
391
+
392
+
393
+ import java.sql.ResultSet;
394
+
395
+ import java.sql.SQLException;
396
+
397
+ import java.util.ArrayList;
398
+
399
+ import java.util.HashMap;
400
+
401
+ import java.util.List;
402
+
403
+ import java.util.Map;
404
+
405
+
406
+
407
+ import form.*;
408
+
409
+
410
+
411
+
412
+
413
+ import org.springframework.beans.factory.annotation.Autowired;
414
+
415
+ import org.springframework.jdbc.core.JdbcTemplate;
416
+
417
+ import org.springframework.jdbc.core.RowMapper;
418
+
419
+ import org.springframework.stereotype.Repository;
420
+
421
+
422
+
423
+ @Repository
424
+
425
+ public class Dao {
426
+
427
+ @Autowired
428
+
429
+ private JdbcTemplate jdbcTemplate;
430
+
431
+
432
+
433
+
434
+
435
+
436
+
437
+ public List<Map<String,Object>> getUser(int i) {
438
+
439
+
440
+
441
+
442
+
443
+ List<Map<String, Object>> list = jdbcTemplate.queryForList("select * from jukousei where lesson_id= ?", i);
444
+
445
+
446
+
447
+
448
+
449
+
450
+
451
+
452
+
453
+ List<Map<String,Object>> tes = new ArrayList<Map<String,Object>>();
454
+
455
+
456
+
457
+ for(Map<String,Object> result:list){
458
+
459
+ Map<String,Object> us = new HashMap<>();
460
+
461
+ us.put("id", (long)result.get("jukou_id")) ;
462
+
463
+ us.put("name",(String)result.get("jukou_name")) ;
464
+
465
+ us.put("flag",(boolean)result.get("syusseki_flag")) ;
466
+
467
+ tes.add(us);
468
+
469
+ }
470
+
471
+
472
+
473
+
474
+
475
+ return tes;
476
+
477
+ }
478
+
479
+
480
+
481
+ }
482
+
483
+ ```
484
+
485
+ 選択したボタンによって受け取ってくるデータは変わりますが、2を選択して、Jsonに変換した
486
+
487
+ データは以下になります。
488
+
489
+
490
+
491
+ [{"flag":true,"name":"博","id":2},{"flag":false,"name":"倉田","id":4}]
492
+
493
+
494
+
495
+ 上記以外にもう一つ列名にIDが存在していて、それをもとに検索をかけています。

2

コメントの追記

2016/09/12 06:30

投稿

zenexvs
zenexvs

スコア12

test CHANGED
File without changes
test CHANGED
@@ -330,7 +330,7 @@
330
330
 
331
331
  int i = Integer.parseInt(lesson);
332
332
 
333
- List<Map<String,Object>> ke = Dao.getUser(i); //mapをlistの中に入れて取ってきている。
333
+ List<Map<String,Object>> ke = Dao.getUser(i); //mapをlistの中に入れてデータベースからDAOで取ってきている。
334
334
 
335
335
  Gson gson = new Gson();//Jsonに変換するためのクラスのインスタンスを生成。
336
336
 

1

コードも載せて、やりたいことを明確にしました。

2016/09/12 02:33

投稿

zenexvs
zenexvs

スコア12

test CHANGED
File without changes
test CHANGED
@@ -3,3 +3,379 @@
3
3
  できればtext()で表示したいです。
4
4
 
5
5
  ご回答お願いします。
6
+
7
+
8
+
9
+ <JSP>
10
+
11
+ ```ここに言語を入力
12
+
13
+
14
+
15
+
16
+
17
+ <%@ page language="java" contentType="text/html; charset=UTF-8"```ここに言語を入力
18
+
19
+ <%@ pageEncoding="UTF-8"%>
20
+
21
+ <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
22
+
23
+ <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
24
+
25
+ <%@ taglib prefix="f" uri="http://www.springframework.org/tags/form"%>
26
+
27
+ <%@ page session="false" %>
28
+
29
+ <html>
30
+
31
+ <head>
32
+
33
+ <title>Home</title>
34
+
35
+ </head>
36
+
37
+ <body>
38
+
39
+ <f:form>
40
+
41
+
42
+
43
+ <div id="title_st">日次レッスン登録</div>
44
+
45
+
46
+
47
+ <div class="centering">
48
+
49
+ <p>原宿校 KWP1 2016/7/13 15:00~16:00</p>
50
+
51
+ </div>
52
+
53
+
54
+
55
+ <div class="centering">
56
+
57
+ <table class="tb_day_lesson_edit_search">
58
+
59
+ <tr>
60
+
61
+ <td>教室</td>
62
+
63
+ <td>
64
+
65
+ <select name="lesson_search1">
66
+
67
+ <option value="教室名1" disabled>
68
+
69
+ レッスン名を選択してください
70
+
71
+ </option>
72
+
73
+ <option value="教室名2">原宿校</option>
74
+
75
+ <option value="教室名3">妙典校</option>
76
+
77
+ <option value="教室名4">下北沢校</option>
78
+
79
+ </select>
80
+
81
+ <tr>
82
+
83
+ <td>レッスン名
84
+
85
+ <td>
86
+
87
+ <select name="lesson_search2">
88
+
89
+ <option value="0" disabled>
90
+
91
+ レッスン名を選択してください
92
+
93
+ </option>
94
+
95
+ <option value="1">KWP1</option>
96
+
97
+ <option value="2">KWP2</option>
98
+
99
+ <option value="3">KWP3</option>
100
+
101
+ </select>
102
+
103
+ <tr>
104
+
105
+ <td>レッスン日
106
+
107
+ <td>
108
+
109
+ <input size="5" type="text" name="jikan" id="date">
110
+
111
+ <tr>
112
+
113
+ <td>時間
114
+
115
+ <td>
116
+
117
+ <input size="5" type="text" name="jikan" value="16:00">~
118
+
119
+ <input size="5" type="text" name="fun" value="17:00">
120
+
121
+ </td>
122
+
123
+ </tr>
124
+
125
+ </table>
126
+
127
+
128
+
129
+ </div>
130
+
131
+
132
+
133
+ <div class="centering">
134
+
135
+ <table class="tb_day_lesson_edit_record" style="display: none;">
136
+
137
+
138
+
139
+ <%-- <c:forEach var="obj" items="${data}" begin="1" >
140
+
141
+ <tr>このような形式でkeyをとってlistの中の値を表示したい。
142
+
143
+ <td><c:out value="${obj.value}"/></td>
144
+
145
+ <td>
146
+
147
+ <input type="radio" name="example" value="サンプル">出
148
+
149
+ <input type="radio" name="example" value="サンプル">欠
150
+
151
+ </td>
152
+
153
+ </tr>
154
+
155
+
156
+
157
+ </c:forEach> --%>
158
+
159
+ </table>
160
+
161
+ </div>
162
+
163
+
164
+
165
+
166
+
167
+
168
+
169
+ </f:form>
170
+
171
+ <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
172
+
173
+
174
+
175
+ <script>
176
+
177
+ $(function() {
178
+
179
+
180
+
181
+ $('[name=lesson_search2]').change(function() {
182
+
183
+ $.ajax({
184
+
185
+
186
+
187
+
188
+
189
+ type : "GET",
190
+
191
+ url : "user", // リクエストURL
192
+
193
+ data : "lesson=" + $('[name=lesson_search2]').val(),
194
+
195
+ datatype: "json",
196
+
197
+ success: function(data){ //通信に成功したときに表示されるアラートです
198
+
199
+
200
+
201
+ $(".tb_day_lesson_edit_record").show();
202
+
203
+ },
204
+
205
+ error: function(data){
206
+
207
+ console.log(data);
208
+
209
+ alert("値はコンソール参照");
210
+
211
+ }
212
+
213
+
214
+
215
+ });
216
+
217
+ });
218
+
219
+ });
220
+
221
+ </script>
222
+
223
+
224
+
225
+ </body>
226
+
227
+
228
+
229
+ </html>
230
+
231
+
232
+
233
+
234
+
235
+ ```
236
+
237
+ <controller>
238
+
239
+
240
+
241
+ ```ここに言語を入力
242
+
243
+ package jp.co.xmdd;
244
+
245
+
246
+
247
+
248
+
249
+ import java.util.ArrayList;
250
+
251
+
252
+
253
+ import java.util.List;
254
+
255
+ import java.util.Locale;
256
+
257
+ import java.util.Map;
258
+
259
+
260
+
261
+
262
+
263
+
264
+
265
+ import org.springframework.beans.factory.annotation.Autowired;
266
+
267
+
268
+
269
+ import org.springframework.stereotype.Controller;
270
+
271
+ import org.springframework.ui.Model;
272
+
273
+ import org.springframework.web.bind.annotation.RequestMapping;
274
+
275
+ import org.springframework.web.bind.annotation.RequestMethod;
276
+
277
+ import org.springframework.web.bind.annotation.ResponseBody;
278
+
279
+
280
+
281
+ import com.google.gson.Gson;
282
+
283
+
284
+
285
+ import dao.Dao;
286
+
287
+
288
+
289
+
290
+
291
+ /**
292
+
293
+ * Handles requests for the application home page.
294
+
295
+ */
296
+
297
+ @Controller
298
+
299
+ public class HomeController {
300
+
301
+ @Autowired
302
+
303
+ private Dao Dao;
304
+
305
+
306
+
307
+
308
+
309
+
310
+
311
+ @RequestMapping(value = "/", method = RequestMethod.GET)
312
+
313
+ public String home(Locale locale, Model model) {
314
+
315
+
316
+
317
+ return "home";
318
+
319
+ }
320
+
321
+
322
+
323
+ @ResponseBody//こちらを使っています。
324
+
325
+ @RequestMapping(value = "/user",produces="text/html;charset=UTF-8", method = RequestMethod.GET )
326
+
327
+ public String UserreturnGET(String lesson){ //非同期で値を受け取ってきている。
328
+
329
+ System.out.println(lesson);
330
+
331
+ int i = Integer.parseInt(lesson);
332
+
333
+ List<Map<String,Object>> ke = Dao.getUser(i); //mapをlistの中に入れて取ってきている。
334
+
335
+ Gson gson = new Gson();//Jsonに変換するためのクラスのインスタンスを生成。
336
+
337
+ System.out.println(gson.toJson(ke));
338
+
339
+ return gson.toJson(ke);//Jsonに変換。
340
+
341
+ }
342
+
343
+
344
+
345
+
346
+
347
+
348
+
349
+ // @ResponseBody
350
+
351
+ // @RequestMapping(value = "/user",produces="text/plain;charset=UTF-8", method = RequestMethod.POST)
352
+
353
+ // public String UserreturnPOST(String lesson){
354
+
355
+ //
356
+
357
+ // int i = Integer.parseInt(lesson);
358
+
359
+ // List<Map<String,Object>> ke =Dao.getUser(i);
360
+
361
+ // System.out.println(ke);
362
+
363
+ // Gson gson = new Gson();
364
+
365
+ // System.out.println(gson.toJson(ke));
366
+
367
+ // return gson.toJson(ke);
368
+
369
+ //
370
+
371
+ // }
372
+
373
+
374
+
375
+
376
+
377
+ }
378
+
379
+
380
+
381
+ ```