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

質問編集履歴

3

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

2016/09/12 06:30

投稿

zenexvs
zenexvs

スコア12

title CHANGED
File without changes
body CHANGED
@@ -188,4 +188,61 @@
188
188
 
189
189
  }
190
190
 
191
- ```
191
+ ```
192
+ <DAO>
193
+
194
+ ```ここに言語を入力
195
+ package dao;
196
+
197
+ import java.sql.ResultSet;
198
+ import java.sql.SQLException;
199
+ import java.util.ArrayList;
200
+ import java.util.HashMap;
201
+ import java.util.List;
202
+ import java.util.Map;
203
+
204
+ import form.*;
205
+
206
+
207
+ import org.springframework.beans.factory.annotation.Autowired;
208
+ import org.springframework.jdbc.core.JdbcTemplate;
209
+ import org.springframework.jdbc.core.RowMapper;
210
+ import org.springframework.stereotype.Repository;
211
+
212
+ @Repository
213
+ public class Dao {
214
+ @Autowired
215
+ private JdbcTemplate jdbcTemplate;
216
+
217
+
218
+
219
+ public List<Map<String,Object>> getUser(int i) {
220
+
221
+
222
+ List<Map<String, Object>> list = jdbcTemplate.queryForList("select * from jukousei where lesson_id= ?", i);
223
+
224
+
225
+
226
+
227
+ List<Map<String,Object>> tes = new ArrayList<Map<String,Object>>();
228
+
229
+ for(Map<String,Object> result:list){
230
+ Map<String,Object> us = new HashMap<>();
231
+ us.put("id", (long)result.get("jukou_id")) ;
232
+ us.put("name",(String)result.get("jukou_name")) ;
233
+ us.put("flag",(boolean)result.get("syusseki_flag")) ;
234
+ tes.add(us);
235
+ }
236
+
237
+
238
+ return tes;
239
+ }
240
+
241
+ }
242
+ ```
243
+ 選択したボタンによって受け取ってくるデータは変わりますが、2を選択して、Jsonに変換した
244
+ データは以下になります。
245
+
246
+ [{"flag":true,"name":"博","id":2},{"flag":false,"name":"倉田","id":4}]
247
+
248
+ 上記以外にもう一つ列名にIDが存在していて、それをもとに検索をかけています。

2

コメントの追記

2016/09/12 06:30

投稿

zenexvs
zenexvs

スコア12

title CHANGED
File without changes
body CHANGED
@@ -164,7 +164,7 @@
164
164
  public String UserreturnGET(String lesson){ //非同期で値を受け取ってきている。
165
165
  System.out.println(lesson);
166
166
  int i = Integer.parseInt(lesson);
167
- List<Map<String,Object>> ke = Dao.getUser(i); //mapをlistの中に入れて取ってきている。
167
+ List<Map<String,Object>> ke = Dao.getUser(i); //mapをlistの中に入れてデータベースからDAOで取ってきている。
168
168
  Gson gson = new Gson();//Jsonに変換するためのクラスのインスタンスを生成。
169
169
  System.out.println(gson.toJson(ke));
170
170
  return gson.toJson(ke);//Jsonに変換。

1

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

2016/09/12 02:33

投稿

zenexvs
zenexvs

スコア12

title CHANGED
File without changes
body CHANGED
@@ -1,3 +1,191 @@
1
1
  jQueryでlistの中に入っているmapをkeyごとに取り出し、JSPに表示させたいです。
2
2
  できればtext()で表示したいです。
3
- ご回答お願いします。
3
+ ご回答お願いします。
4
+
5
+ <JSP>
6
+ ```ここに言語を入力
7
+
8
+
9
+ <%@ page language="java" contentType="text/html; charset=UTF-8"```ここに言語を入力
10
+ <%@ pageEncoding="UTF-8"%>
11
+ <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
12
+ <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
13
+ <%@ taglib prefix="f" uri="http://www.springframework.org/tags/form"%>
14
+ <%@ page session="false" %>
15
+ <html>
16
+ <head>
17
+ <title>Home</title>
18
+ </head>
19
+ <body>
20
+ <f:form>
21
+
22
+ <div id="title_st">日次レッスン登録</div>
23
+
24
+ <div class="centering">
25
+ <p>原宿校 KWP1 2016/7/13 15:00~16:00</p>
26
+ </div>
27
+
28
+ <div class="centering">
29
+ <table class="tb_day_lesson_edit_search">
30
+ <tr>
31
+ <td>教室</td>
32
+ <td>
33
+ <select name="lesson_search1">
34
+ <option value="教室名1" disabled>
35
+ レッスン名を選択してください
36
+ </option>
37
+ <option value="教室名2">原宿校</option>
38
+ <option value="教室名3">妙典校</option>
39
+ <option value="教室名4">下北沢校</option>
40
+ </select>
41
+ <tr>
42
+ <td>レッスン名
43
+ <td>
44
+ <select name="lesson_search2">
45
+ <option value="0" disabled>
46
+ レッスン名を選択してください
47
+ </option>
48
+ <option value="1">KWP1</option>
49
+ <option value="2">KWP2</option>
50
+ <option value="3">KWP3</option>
51
+ </select>
52
+ <tr>
53
+ <td>レッスン日
54
+ <td>
55
+ <input size="5" type="text" name="jikan" id="date">
56
+ <tr>
57
+ <td>時間
58
+ <td>
59
+ <input size="5" type="text" name="jikan" value="16:00">~
60
+ <input size="5" type="text" name="fun" value="17:00">
61
+ </td>
62
+ </tr>
63
+ </table>
64
+
65
+ </div>
66
+
67
+ <div class="centering">
68
+ <table class="tb_day_lesson_edit_record" style="display: none;">
69
+
70
+ <%-- <c:forEach var="obj" items="${data}" begin="1" >
71
+ <tr>このような形式でkeyをとってlistの中の値を表示したい。
72
+ <td><c:out value="${obj.value}"/></td>
73
+ <td>
74
+ <input type="radio" name="example" value="サンプル">出
75
+ <input type="radio" name="example" value="サンプル">欠
76
+ </td>
77
+ </tr>
78
+
79
+ </c:forEach> --%>
80
+ </table>
81
+ </div>
82
+
83
+
84
+
85
+ </f:form>
86
+ <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
87
+
88
+ <script>
89
+ $(function() {
90
+
91
+ $('[name=lesson_search2]').change(function() {
92
+ $.ajax({
93
+
94
+
95
+ type : "GET",
96
+ url : "user", // リクエストURL
97
+ data : "lesson=" + $('[name=lesson_search2]').val(),
98
+ datatype: "json",
99
+ success: function(data){ //通信に成功したときに表示されるアラートです
100
+
101
+ $(".tb_day_lesson_edit_record").show();
102
+ },
103
+ error: function(data){
104
+ console.log(data);
105
+ alert("値はコンソール参照");
106
+ }
107
+
108
+ });
109
+ });
110
+ });
111
+ </script>
112
+
113
+ </body>
114
+
115
+ </html>
116
+
117
+
118
+ ```
119
+ <controller>
120
+
121
+ ```ここに言語を入力
122
+ package jp.co.xmdd;
123
+
124
+
125
+ import java.util.ArrayList;
126
+
127
+ import java.util.List;
128
+ import java.util.Locale;
129
+ import java.util.Map;
130
+
131
+
132
+
133
+ import org.springframework.beans.factory.annotation.Autowired;
134
+
135
+ import org.springframework.stereotype.Controller;
136
+ import org.springframework.ui.Model;
137
+ import org.springframework.web.bind.annotation.RequestMapping;
138
+ import org.springframework.web.bind.annotation.RequestMethod;
139
+ import org.springframework.web.bind.annotation.ResponseBody;
140
+
141
+ import com.google.gson.Gson;
142
+
143
+ import dao.Dao;
144
+
145
+
146
+ /**
147
+ * Handles requests for the application home page.
148
+ */
149
+ @Controller
150
+ public class HomeController {
151
+ @Autowired
152
+ private Dao Dao;
153
+
154
+
155
+
156
+ @RequestMapping(value = "/", method = RequestMethod.GET)
157
+ public String home(Locale locale, Model model) {
158
+
159
+ return "home";
160
+ }
161
+
162
+ @ResponseBody//こちらを使っています。
163
+ @RequestMapping(value = "/user",produces="text/html;charset=UTF-8", method = RequestMethod.GET )
164
+ public String UserreturnGET(String lesson){ //非同期で値を受け取ってきている。
165
+ System.out.println(lesson);
166
+ int i = Integer.parseInt(lesson);
167
+ List<Map<String,Object>> ke = Dao.getUser(i); //mapをlistの中に入れて取ってきている。
168
+ Gson gson = new Gson();//Jsonに変換するためのクラスのインスタンスを生成。
169
+ System.out.println(gson.toJson(ke));
170
+ return gson.toJson(ke);//Jsonに変換。
171
+ }
172
+
173
+
174
+
175
+ // @ResponseBody
176
+ // @RequestMapping(value = "/user",produces="text/plain;charset=UTF-8", method = RequestMethod.POST)
177
+ // public String UserreturnPOST(String lesson){
178
+ //
179
+ // int i = Integer.parseInt(lesson);
180
+ // List<Map<String,Object>> ke =Dao.getUser(i);
181
+ // System.out.println(ke);
182
+ // Gson gson = new Gson();
183
+ // System.out.println(gson.toJson(ke));
184
+ // return gson.toJson(ke);
185
+ //
186
+ // }
187
+
188
+
189
+ }
190
+
191
+ ```