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

質問編集履歴

3

追記

2021/05/17 06:16

投稿

usazk
usazk

スコア0

title CHANGED
File without changes
body CHANGED
@@ -66,4 +66,267 @@
66
66
  データ登録時と同じようにチェックボックスの値を取得し、それをもとに検索をしたかったのですが、
67
67
  JavaScriptやPHP、フレームワークを使用した情報が多く、自身の環境での方法が浮かびません。
68
68
  面倒な方法であってもできるだけフレームワークを使用せず勉強をすすめたいと思っております。
69
- ご回答いただけますと幸いです。
69
+ ご回答いただけますと幸いです。
70
+
71
+
72
+ 追記:
73
+ ```モデル
74
+ @Entity
75
+ @Table(name = "profiles")
76
+ @NamedQueries({
77
+ @NamedQuery(name = "getProfile", query = "SELECT p FROM Pro AS p ORDER BY p.id DESC"),
78
+ @NamedQuery(name = "getProfileCount", query = "SELECT COUNT(p) FROM Pro AS p"),
79
+ })
80
+
81
+ public class Pro {
82
+ @Id
83
+ @GeneratedValue(strategy = GenerationType.IDENTITY)
84
+ @Column(name = "id")
85
+ private Integer id;
86
+
87
+ @Column(name = "name", nullable = false)
88
+ private String name;
89
+
90
+ @Column(name = "favorite")
91
+ private String favorite;
92
+
93
+ public Integer getId() {
94
+ return id;
95
+ }
96
+
97
+ public void setId(Integer id) {
98
+ this.id = id;
99
+ }
100
+
101
+ public String getName() {
102
+ return name;
103
+ }
104
+
105
+ public void setName(String name) {
106
+ this.name = name;
107
+ }
108
+
109
+ public String getFavorite() {
110
+ return favorite;
111
+ }
112
+
113
+ public void setFavorite(String favorite) {
114
+ this.favorite = favorite;
115
+ }
116
+ }
117
+
118
+ ```
119
+ テーブル
120
+ ```profiles
121
+ +----------+---------------------------------+------+-----+---------+----------------+
122
+ | Field | Type | Null | Key | Default | Extra |
123
+ +----------+---------------------------------+------+-----+---------+----------------+
124
+ | id | int(11) | NO | PRI | NULL | auto_increment |
125
+ | favorite | set('りんご','バナナ','いちご') | YES | | NULL | |
126
+ | name | varchar(255) | NO | | NULL | |
127
+ +----------+---------------------------------+------+-----+---------+----------------+
128
+ +----+---------------+--------+
129
+ | id | favorite | name |
130
+ +----+---------------+--------+
131
+ | 1 | りんご | やまだ |
132
+ | 2 | りんご,バナナ | たなか |
133
+ | 3 | バナナ,いちご | おだ |
134
+ | 4 | いちご | なかい |
135
+ +----+---------------+--------+
136
+ ```
137
+ データ登録のJSP
138
+ ```new.JSP
139
+ <%@ page language="java" contentType="text/html; charset=UTF-8"
140
+ pageEncoding="UTF-8"%>
141
+ <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
142
+ <c:import url="../layout/app.jsp">
143
+ <c:param name="content">
144
+ <h2>プロフィール新規登録ページ</h2>
145
+
146
+ <form method="POST" action="<c:url value='/create' />">
147
+ <label for="name">名前</label>
148
+ <input type="text" name="name" value="${profile.name}" />
149
+ 好きな食べ物※複数可<br />
150
+ <label><input type="checkbox" name="favorite[]" value="りんご">りんご</label>&nbsp;&nbsp;
151
+ <label><input type="checkbox" name="favorite[]" value="バナナ">バナナ</label>&nbsp;&nbsp;
152
+ <label><input type="checkbox" name="favorite[]" value="いちご">いちご</label><br/>
153
+ <button type="submit">登録</button>
154
+ </form>
155
+
156
+ </c:param>
157
+ </c:import>
158
+ ```
159
+
160
+ データ登録のサーブレット
161
+ ```CreateServlet
162
+
163
+ /**
164
+ * Servlet implementation class CreateServlet
165
+ */
166
+ @WebServlet("/create")
167
+ public class CreateServlet extends HttpServlet {
168
+ private static final long serialVersionUID = 1L;
169
+
170
+ /**
171
+ * @see HttpServlet#HttpServlet()
172
+ */
173
+ public CreateServlet() {
174
+ super();
175
+ // TODO Auto-generated constructor stub
176
+ }
177
+
178
+ /**
179
+ * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
180
+ */
181
+ protected void doPost(HttpServletRequest request, HttpServletResponse response)
182
+ throws ServletException, IOException {
183
+ EntityManager em = DBUtil.createEntityManager();
184
+
185
+ Pro p = new Pro();
186
+ p.setName(request.getParameter("name"));
187
+ String[] favorite = request.getParameterValues("favorite");
188
+ String fvr_str = favorite[0];
189
+ for (int i = 1; i < favorite.length; i++) {
190
+ fvr_str += ("," + favorite[i]);
191
+ }
192
+ p.setFavorite(fvr_str);
193
+ List<String> errors = ProValidator.validate(p);
194
+ if (errors.size() > 0) {
195
+ em.close();
196
+
197
+ request.setAttribute("profile", p);
198
+ request.setAttribute("errors", errors);
199
+
200
+ RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/views/pro/new.jsp");
201
+ rd.forward(request, response);
202
+ } else {
203
+ em.getTransaction().begin();
204
+ em.persist(p);
205
+ em.getTransaction().commit();
206
+ em.close();
207
+ request.getSession().setAttribute("flush", "登録が完了しました。");
208
+
209
+ response.sendRedirect(request.getContextPath() + "/");
210
+
211
+ }
212
+
213
+ }
214
+ }
215
+
216
+ ```
217
+ 一覧表示と検索をするJSP
218
+ ```index.JSP
219
+ <%@ page language="java" contentType="text/html; charset=UTF-8"
220
+ pageEncoding="UTF-8"%>
221
+
222
+ <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
223
+
224
+
225
+ <c:import url="../layout/app.jsp">
226
+ <c:param name="content">
227
+ <c:if test="${flush != null}">
228
+ <div id="flush_success">
229
+ <c:out value="${flush}"></c:out>
230
+ </div>
231
+ </c:if>
232
+ <h2>プロフィール</h2>
233
+ <table id="profile_list">
234
+ <tbody>
235
+ <tr>
236
+
237
+ <th class="name">名前</th>
238
+ <th class="favorite">好きな食べ物</th>
239
+
240
+ </tr>
241
+ <c:forEach var="profile" items="${profiles}" varStatus="status">
242
+ <tr class="row${status.count % 2}">
243
+
244
+ <td class="name"><c:out value="${profile.name}" /></td>
245
+
246
+ <td class="favorite"><c:out value="${profile.favorite}" /></td>
247
+ </tr>
248
+
249
+ </c:forEach>
250
+ </tbody>
251
+ </table>
252
+
253
+ <div id="pagination">
254
+ (全${profiles_count}件)<br />
255
+ <c:forEach var="i" begin="1" end="${((profiles_count -1) /15 )+ 1}"
256
+ step="1">
257
+ <c:choose>
258
+ <c:when test="${i == page}">
259
+ <c:out value="${i}" />&nbsp;
260
+ </c:when>
261
+ <c:otherwise>
262
+ <a href="<c:url value='/profiles/index?page=${i}'/>"><c:out
263
+ value="${i}" /></a>&nbsp;
264
+ </c:otherwise>
265
+ </c:choose>
266
+ </c:forEach>
267
+ </div>
268
+
269
+ <p>
270
+ <a href="<c:url value='/new' />">プロフィール登録</a>
271
+ </p>
272
+ <h2>プロフィール検索</h2>
273
+
274
+ <form method="GET" action="<c:url value='/search' />">
275
+ 好きな食べ物で検索※複数可<br />
276
+ <label><input type="checkbox" name="favorite[]" value="りんご">りんご</label>&nbsp;&nbsp;
277
+ <label><input type="checkbox" name="favorite[]" value="バナナ">バナナ</label>&nbsp;&nbsp;
278
+ <label><input type="checkbox" name="favorite[]" value="いちご">いちご</label><br/>
279
+ <button type="submit">検索</button>
280
+ </form>
281
+ </c:param>
282
+ </c:import>
283
+ ```
284
+ 検索処理のサーブレット
285
+ ```SearchServlet
286
+ package controllers;
287
+
288
+ import java.io.IOException;
289
+
290
+ import javax.persistence.EntityManager;
291
+ import javax.servlet.ServletException;
292
+ import javax.servlet.annotation.WebServlet;
293
+ import javax.servlet.http.HttpServlet;
294
+ import javax.servlet.http.HttpServletRequest;
295
+ import javax.servlet.http.HttpServletResponse;
296
+
297
+ import utils.DBUtil;
298
+
299
+ /**
300
+ * Servlet implementation class Search
301
+ */
302
+ @WebServlet("/search")
303
+ public class Search extends HttpServlet {
304
+ private static final long serialVersionUID = 1L;
305
+
306
+ /**
307
+ * @see HttpServlet#HttpServlet()
308
+ */
309
+ public Search() {
310
+ super();
311
+ // TODO Auto-generated constructor stub
312
+ }
313
+
314
+ /**
315
+ * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
316
+ */
317
+ protected void doGet(HttpServletRequest request, HttpServletResponse response)
318
+ throws ServletException, IOException {
319
+ EntityManager em = DBUtil.createEntityManager();
320
+
321
+ String[] favorite = request.getParameterValues("favorite");
322
+ String fvr_str = favorite[0];
323
+ for (int i = 1; i < favorite.length; i++) {
324
+ fvr_str += ("," + favorite[i]);
325
+ }
326
+
327
+ }
328
+
329
+
330
+ }
331
+
332
+ ```

2

誤字の修正

2021/05/17 06:16

投稿

usazk
usazk

スコア0

title CHANGED
File without changes
body CHANGED
@@ -66,13 +66,4 @@
66
66
  データ登録時と同じようにチェックボックスの値を取得し、それをもとに検索をしたかったのですが、
67
67
  JavaScriptやPHP、フレームワークを使用した情報が多く、自身の環境での方法が浮かびません。
68
68
  面倒な方法であってもできるだけフレームワークを使用せず勉強をすすめたいと思っております。
69
- ご回答いただけますと幸いです。
69
+ ご回答いただけますと幸いです。
70
-
71
- 返信:
72
- m.ts10806様
73
- 再度返信ありがとうございます。
74
- >違います。入力コントロールのname属性に[]とすることで配列として送信されるので作る必要ないです。
75
- 解釈が間違っていたようで申し訳ございません。
76
- 変更した方法も間違っていますでしょうか?
77
- もし差し支えなければ具体的にどのように書けばいいのか教えていただけますでしょうか。
78
- データ送信の時点でにつまずいてしまっていますが、実現したいことはその先にある、登録データからチェックボックスの情報に該当する内容を検索する方法なのでご教示いただけますと幸いです。

1

回答へのコメント

2021/05/16 13:30

投稿

usazk
usazk

スコア0

title CHANGED
File without changes
body CHANGED
@@ -47,7 +47,7 @@
47
47
  好きな食べ物※複数可<br />
48
48
      <label><input type="checkbox" name="favorite" value="りんご">りんご</label>&nbsp;&nbsp;
49
49
      <label><input type="checkbox" name="favorite" value="バナナ">バナナ</label>&nbsp;&nbsp;
50
-    <label><input type="checkbox" name="favorite" value="バナナ">バナナ</label>&nbsp;&nbsp;
50
+    <label><input type="checkbox" name="favorite" value="いちご">いちご</label>&nbsp;&nbsp;
51
51
  ```
52
52
  データ登録のサーブレット
53
53
  ```creaate servlet
@@ -56,14 +56,23 @@
56
56
  Prof p = new Prof();
57
57
  p.setName(request.getParameter("name"));
58
58
  String[] favorite = request.getParameterValues("favorite");
59
- String fvr_str = mood[0];
59
+ String fvr_str = favorite[0];
60
60
  for (int i = 1; i < favorite.length; i++) {
61
61
  fvr_str += ("," + favorite[i]);
62
62
  }
63
- m.setFavorite(fvr_str);
63
+ p.setFavorite(fvr_str);
64
64
  ```
65
65
 
66
66
  データ登録時と同じようにチェックボックスの値を取得し、それをもとに検索をしたかったのですが、
67
67
  JavaScriptやPHP、フレームワークを使用した情報が多く、自身の環境での方法が浮かびません。
68
68
  面倒な方法であってもできるだけフレームワークを使用せず勉強をすすめたいと思っております。
69
- ご回答いただけますと幸いです。
69
+ ご回答いただけますと幸いです。
70
+
71
+ 返信:
72
+ m.ts10806様
73
+ 再度返信ありがとうございます。
74
+ >違います。入力コントロールのname属性に[]とすることで配列として送信されるので作る必要ないです。
75
+ 解釈が間違っていたようで申し訳ございません。
76
+ 変更した方法も間違っていますでしょうか?
77
+ もし差し支えなければ具体的にどのように書けばいいのか教えていただけますでしょうか。
78
+ データ送信の時点でにつまずいてしまっていますが、実現したいことはその先にある、登録データからチェックボックスの情報に該当する内容を検索する方法なのでご教示いただけますと幸いです。