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

質問編集履歴

2

意図的に内容を抹消する行為にあたるため

2021/07/05 02:27

投稿

退会済みユーザー
title CHANGED
@@ -1,1 +1,1 @@
1
- 削、、、、、、、、、、、、、、、、、、、、
1
+ プルダウンをPOST送信後に選択した項目のまま保持させたい。
body CHANGED
@@ -1,1 +1,272 @@
1
+ いつもお世話になっております。
2
+ プルダウンで項目名を選択後にボタンのクリックで送信し、その項目名に合致する情報を表示するプログラムを作成しております。
3
+ 送信後のプルダウンを、選択した項目が初期状態になり、他の項目名もプルダウンにて確認できるようにしたいです。
4
+ 追加したコードで初期値を設定したのですが、これではプルダウンに1件しか表示されないため、初期値を設定しつつ他の項目をプルダウン内に表示したいのですが、
5
+ どのように修正、記述をすればよいのかが分からないためご教示いただければ幸いです。
6
+ よろしくお願いいたします。
7
+ ###表示画面
8
+ ```jsp
9
+ <%@ page contentType="text/html; charset=Windows-31J"%>
10
+ <%@ page import="jp.co.keyaki.bean.KuyakushoBean"%>
11
+ <%@ page import="java.util.List"%>
12
+ <%@ page import="java.util.ArrayList"%>
13
+ <% @SuppressWarnings("unchecked")
14
+ List<KuyakushoBean> list = (List<KuyakushoBean>) request.getAttribute("LIST");
15
+ %>
16
+ <% @SuppressWarnings("unchecked")
17
+ List<KuyakushoBean> name = (List<KuyakushoBean>) request.getAttribute("ku");
18
+ %>
19
+ <html>
20
+ <head>
21
+ <meta charset="UTF-8">
22
+ <title>23区区役所一覧</title>
23
+ </head>
24
+ <body>
1
- 削除。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
25
+ <div align="center">
26
+ 23区区役所一覧
27
+ <form action="list" method="post">
28
+ <select name="ku">
29
+ <%if (list != null ) { %>
30
+ <% for (int i = 0; i < list.size(); i++) { %>
31
+ <%KuyakushoBean KuyakushoBean= (KuyakushoBean)list.get(i);%>
32
+ <option value=<%=KuyakushoBean.getid() %>><%=KuyakushoBean.getku() %></option>
33
+ <% } %>
34
+ <% } %>
35
+          //追加した部分
36
+ <%if (name != null ) { %>
37
+ <% for (int i = 0; i < name.size(); i++) { %>
38
+ <%KuyakushoBean KuyakushoBean= (KuyakushoBean)name.get(i);%>
39
+ <option value=<%=KuyakushoBean.getid() %>selected><%=KuyakushoBean.getku() %></option>
40
+ <% } %>
41
+ <% } %>
42
+ </select> <input type="submit" value="送信">
43
+ </form>
44
+ <table border="1">
45
+ <tr>
46
+ <th>番号</th>
47
+ <th>区</th>
48
+ <th>郵便番号</th>
49
+ <th>住所</th>
50
+ <th>電話番号</th>
51
+ </tr>
52
+ <%if (list != null ) { %>
53
+ <% for (int j = 0; j < list.size(); j++) { %>
54
+ <%KuyakushoBean KuyakushoBean= (KuyakushoBean)list.get(j);%>
55
+ <tr>
56
+ <td><%=KuyakushoBean.getid()%></td>
57
+ <td><%=KuyakushoBean.getku()%></td>
58
+ <td><%=KuyakushoBean.getzip()%></td>
59
+ <td><%=KuyakushoBean.getaddress()%></td>
60
+ <td><%=KuyakushoBean.gettel()%></td>
61
+ </tr>
62
+ <% } %>
63
+ <% } %>
64
+ <%if(name != null) {%>
65
+ <% for (int i = 0; i < name.size(); i++) { %>
66
+ <%KuyakushoBean KuyakushoBean= (KuyakushoBean)name.get(i);%>
67
+ <tr>
68
+ <td><%=KuyakushoBean.getid()%></td>
69
+ <td><%=KuyakushoBean.getku()%></td>
70
+ <td><%=KuyakushoBean.getzip()%></td>
71
+ <td><%=KuyakushoBean.getaddress()%></td>
72
+ <td><%=KuyakushoBean.gettel()%></td>
73
+ </tr>
74
+ <% } %>
75
+ <% } %>
76
+ </table>
77
+ <input type="hidden" name="ku2">
78
+ </div>
79
+ </body>
80
+ </html>
81
+ ```
82
+ ###サーブレット
83
+ ```java
84
+ package jp.co.keyaki.controller;
85
+ import java.io.IOException;
86
+ import java.util.List;
87
+ import javax.servlet.RequestDispatcher;
88
+ import javax.servlet.ServletContext;
89
+ import javax.servlet.ServletException;
90
+ import javax.servlet.http.HttpServlet;
91
+ import javax.servlet.http.HttpServletRequest;
92
+ import javax.servlet.http.HttpServletResponse;
93
+ import jp.co.keyaki.bean.KuyakushoBean;
94
+ import jp.co.keyaki.service.KuyakushoSearch;
95
+ public class KuyakushoController extends HttpServlet {
96
+ public void doGet(HttpServletRequest request, HttpServletResponse response)
97
+ throws ServletException, IOException {
98
+ String target = null;
99
+ try {
100
+ KuyakushoSearch kuyakushoSearch = new KuyakushoSearch();
101
+ List<KuyakushoBean> list = kuyakushoSearch.list();
102
+ request.setAttribute("LIST", list);
103
+ target = "/kuyakusho.jsp";
104
+ } catch (Exception e) {
105
+ e.printStackTrace();
106
+ target = "/systemError.jsp";
107
+ } finally {
108
+ ServletContext context = this.getServletContext();
109
+ RequestDispatcher dispatcher = context.getRequestDispatcher(target);
110
+ dispatcher.forward(request, response);
111
+ }
112
+ }
113
+ public void doPost(HttpServletRequest request, HttpServletResponse response)
114
+ throws ServletException, IOException {
115
+ String target = null;
116
+ try{
117
+ int id = Integer.parseInt(request.getParameter("ku"));
118
+ KuyakushoSearch kuyakushoSearch = new KuyakushoSearch();
119
+ List<KuyakushoBean> name = kuyakushoSearch.name(id);
120
+ request.setAttribute("ku", name);
121
+ if(name == null) {
122
+ request.setAttribute("ku", name);
123
+ }
124
+ target = "/kuyakusho.jsp";
125
+ } catch (Exception e) {
126
+ e.printStackTrace();
127
+ target = "/systemError.jsp";
128
+ } finally {
129
+ ServletContext context = this.getServletContext();
130
+ RequestDispatcher dispatcher = context.getRequestDispatcher(target);
131
+ dispatcher.forward(request, response);
132
+ }
133
+ }
134
+ }
135
+ ```
136
+ ###検索処理
137
+ ```java
138
+ package jp.co.keyaki.service;
139
+ import java.sql.Connection;
140
+ import java.sql.DriverManager;
141
+ import java.sql.PreparedStatement;
142
+ import java.sql.ResultSet;
143
+ import java.sql.SQLException;
144
+ import java.util.ArrayList;
145
+ import java.util.List;
146
+ import org.postgresql.util.PSQLException;
147
+ import jp.co.keyaki.bean.KuyakushoBean;
148
+ public class KuyakushoSearch {
149
+ public List<KuyakushoBean> list()
150
+ throws ClassNotFoundException, SQLException {
151
+ List<KuyakushoBean> list = new ArrayList<>();
152
+ Connection connection = null;
153
+ PreparedStatement preparedStatement = null;
154
+ ResultSet resultSet = null;
155
+ KuyakushoBean KuyakushoBean = null;
156
+ try {
157
+ Class.forName("org.postgresql.Driver");
158
+ connection = DriverManager.getConnection(
159
+ jdbc:postgresql://【ホスト名】/【データベース名】", "【ユーザ名】", "【パスワード】");
160
+ String SQL = "SELECT id, ku, zip, address, tel FROM t_kuyakusho";
161
+ preparedStatement = connection.prepareStatement(SQL);
162
+ ResultSet rs = preparedStatement.executeQuery();
163
+ while (rs.next()) {
164
+ KuyakushoBean = new KuyakushoBean();
165
+ KuyakushoBean.setid(rs.getInt("id"));
166
+ KuyakushoBean.setku(rs.getString("ku"));
167
+ KuyakushoBean.setzip(rs.getString("zip"));
168
+ KuyakushoBean.setaddress(rs.getString("address"));
169
+ KuyakushoBean.settel(rs.getString("tel"));
170
+ list.add(KuyakushoBean);
171
+ }
172
+ } catch (ClassNotFoundException e) {
173
+ e.printStackTrace();
174
+ } catch (PSQLException e) {
175
+ e.printStackTrace();
176
+ } finally {
177
+ if (resultSet != null) {
178
+ resultSet.close();
179
+ }
180
+ if (preparedStatement != null) {
181
+ preparedStatement.close();
182
+ }
183
+ if (connection != null) {
184
+ connection.close();
185
+ }
186
+ }
187
+ return list;
188
+ }
189
+ public List<KuyakushoBean> name(int id)
190
+ throws ClassNotFoundException, SQLException {
191
+ List<KuyakushoBean> name = new ArrayList<>();
192
+ Connection connection = null;
193
+ PreparedStatement preparedStatement = null;
194
+ ResultSet resultSet = null;
195
+ KuyakushoBean KuyakushoBean = null;
196
+ try {
197
+ Class.forName("org.postgresql.Driver");
198
+ connection = DriverManager.getConnection(
199
+ "jdbc:postgresql://localhost:5432/lessondb", "postgres", "jobsupport");
200
+ String SQL = "SELECT id, ku, zip, address, tel FROM t_kuyakusho WHERE id = ?";
201
+ preparedStatement = connection.prepareStatement(SQL);
202
+ preparedStatement.setInt(1, id);
203
+ ResultSet rs = preparedStatement.executeQuery();
204
+ if (rs.next()) {
205
+ KuyakushoBean = new KuyakushoBean();
206
+ KuyakushoBean.setid(rs.getInt("id"));
207
+ KuyakushoBean.setku(rs.getString("ku"));
208
+ KuyakushoBean.setzip(rs.getString("zip"));
209
+ KuyakushoBean.setaddress(rs.getString("address"));
210
+ KuyakushoBean.settel(rs.getString("tel"));
211
+ name.add(KuyakushoBean);
212
+ }
213
+ } catch (ClassNotFoundException e) {
214
+ e.printStackTrace();
215
+ } catch (PSQLException e) {
216
+ e.printStackTrace();
217
+ } finally {
218
+ if (resultSet != null) {
219
+ resultSet.close();
220
+ }
221
+ if (preparedStatement != null) {
222
+ preparedStatement.close();
223
+ }
224
+ if (connection != null) {
225
+ connection.close();
226
+ }
227
+ }
228
+ return name;
229
+ }
230
+ }
231
+ ```
232
+ ###Bean
233
+ ```java
234
+ package jp.co.keyaki.bean;
235
+ public class KuyakushoBean {
236
+ private int id;
237
+ private String ku;
238
+ private String zip;
239
+ private String address;
240
+ private String tel;
241
+ public void setid(int id) {
242
+ this.id = id;
243
+ }
244
+ public int getid() {
245
+ return id;
246
+ }
247
+ public void setku(String ku) {
248
+ this.ku = ku;
249
+ }
250
+ public String getku() {
251
+ return ku;
252
+ }
253
+ public void setzip(String zip) {
254
+ this.zip = zip;
255
+ }
256
+ public String getzip() {
257
+ return zip;
258
+ }
259
+ public void setaddress(String address) {
260
+ this.address = address;
261
+ }
262
+ public String getaddress() {
263
+ return address;
264
+ }
265
+ public void settel(String tel) {
266
+ this.tel = tel;
267
+ }
268
+ public String gettel() {
269
+ return tel;
270
+ }
271
+ }
272
+ ```

1

削除

2021/07/05 02:27

投稿

退会済みユーザー
title CHANGED
@@ -1,1 +1,1 @@
1
- プルダウンをPOST送信後に選択した項目のまま保持させたい。
1
+ 削、、、、、、、、、、、、、、、、、、、、
body CHANGED
@@ -1,324 +1,1 @@
1
- いつもお世話になっております。
2
- プルダウンで項目名を選択後にボタンのクリックで送信し、その項目名に合致する情報を表示するプログラムを作成しております。
3
-
4
- 送信後のプルダウンを、選択した項目が初期状態になり、他の項目名もプルダウンにて確認できるようにしたいです。
5
-
6
- 追加したコードで初期値を設定したのですが、これではプルダウンに1件しか表示されないため、初期値を設定しつつ他の項目をプルダウン内に表示したいのですが、
7
- どのように修正、記述をすればよいのかが分からないためご教示いただければ幸いです。
8
-
9
- よろしくお願いいたします。
10
-
11
-
12
-
13
-
14
-
15
-
16
- ###表示画面
17
- ```jsp
18
- <%@ page contentType="text/html; charset=Windows-31J"%>
19
- <%@ page import="jp.co.keyaki.bean.KuyakushoBean"%>
20
- <%@ page import="java.util.List"%>
21
- <%@ page import="java.util.ArrayList"%>
22
- <% @SuppressWarnings("unchecked")
23
- List<KuyakushoBean> list = (List<KuyakushoBean>) request.getAttribute("LIST");
24
- %>
25
- <% @SuppressWarnings("unchecked")
26
- List<KuyakushoBean> name = (List<KuyakushoBean>) request.getAttribute("ku");
27
- %>
28
-
29
- <html>
30
- <head>
31
- <meta charset="UTF-8">
32
- <title>23区区役所一覧</title>
33
- </head>
34
- <body>
35
- <div align="center">
1
+ 削除。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
36
- 23区区役所一覧
37
- <form action="list" method="post">
38
- <select name="ku">
39
- <%if (list != null ) { %>
40
- <% for (int i = 0; i < list.size(); i++) { %>
41
- <%KuyakushoBean KuyakushoBean= (KuyakushoBean)list.get(i);%>
42
- <option value=<%=KuyakushoBean.getid() %>><%=KuyakushoBean.getku() %></option>
43
- <% } %>
44
- <% } %>
45
-          //追加した部分
46
- <%if (name != null ) { %>
47
- <% for (int i = 0; i < name.size(); i++) { %>
48
- <%KuyakushoBean KuyakushoBean= (KuyakushoBean)name.get(i);%>
49
- <option value=<%=KuyakushoBean.getid() %>selected><%=KuyakushoBean.getku() %></option>
50
- <% } %>
51
- <% } %>
52
- </select> <input type="submit" value="送信">
53
- </form>
54
- <table border="1">
55
- <tr>
56
- <th>番号</th>
57
- <th>区</th>
58
- <th>郵便番号</th>
59
- <th>住所</th>
60
- <th>電話番号</th>
61
- </tr>
62
- <%if (list != null ) { %>
63
- <% for (int j = 0; j < list.size(); j++) { %>
64
- <%KuyakushoBean KuyakushoBean= (KuyakushoBean)list.get(j);%>
65
- <tr>
66
- <td><%=KuyakushoBean.getid()%></td>
67
- <td><%=KuyakushoBean.getku()%></td>
68
- <td><%=KuyakushoBean.getzip()%></td>
69
- <td><%=KuyakushoBean.getaddress()%></td>
70
- <td><%=KuyakushoBean.gettel()%></td>
71
- </tr>
72
- <% } %>
73
- <% } %>
74
- <%if(name != null) {%>
75
- <% for (int i = 0; i < name.size(); i++) { %>
76
- <%KuyakushoBean KuyakushoBean= (KuyakushoBean)name.get(i);%>
77
- <tr>
78
- <td><%=KuyakushoBean.getid()%></td>
79
- <td><%=KuyakushoBean.getku()%></td>
80
- <td><%=KuyakushoBean.getzip()%></td>
81
- <td><%=KuyakushoBean.getaddress()%></td>
82
- <td><%=KuyakushoBean.gettel()%></td>
83
- </tr>
84
- <% } %>
85
- <% } %>
86
- </table>
87
- <input type="hidden" name="ku2">
88
- </div>
89
- </body>
90
- </html>
91
- ```
92
- ###サーブレット
93
- ```java
94
- package jp.co.keyaki.controller;
95
-
96
- import java.io.IOException;
97
- import java.util.List;
98
-
99
- import javax.servlet.RequestDispatcher;
100
- import javax.servlet.ServletContext;
101
- import javax.servlet.ServletException;
102
- import javax.servlet.http.HttpServlet;
103
- import javax.servlet.http.HttpServletRequest;
104
- import javax.servlet.http.HttpServletResponse;
105
-
106
- import jp.co.keyaki.bean.KuyakushoBean;
107
- import jp.co.keyaki.service.KuyakushoSearch;
108
-
109
- public class KuyakushoController extends HttpServlet {
110
-
111
- public void doGet(HttpServletRequest request, HttpServletResponse response)
112
- throws ServletException, IOException {
113
-
114
- String target = null;
115
-
116
- try {
117
- KuyakushoSearch kuyakushoSearch = new KuyakushoSearch();
118
- List<KuyakushoBean> list = kuyakushoSearch.list();
119
- request.setAttribute("LIST", list);
120
- target = "/kuyakusho.jsp";
121
-
122
- } catch (Exception e) {
123
- e.printStackTrace();
124
- target = "/systemError.jsp";
125
- } finally {
126
- ServletContext context = this.getServletContext();
127
- RequestDispatcher dispatcher = context.getRequestDispatcher(target);
128
- dispatcher.forward(request, response);
129
- }
130
- }
131
- public void doPost(HttpServletRequest request, HttpServletResponse response)
132
- throws ServletException, IOException {
133
- String target = null;
134
- try{
135
- int id = Integer.parseInt(request.getParameter("ku"));
136
- KuyakushoSearch kuyakushoSearch = new KuyakushoSearch();
137
- List<KuyakushoBean> name = kuyakushoSearch.name(id);
138
- request.setAttribute("ku", name);
139
- if(name == null) {
140
- request.setAttribute("ku", name);
141
- }
142
- target = "/kuyakusho.jsp";
143
- } catch (Exception e) {
144
- e.printStackTrace();
145
- target = "/systemError.jsp";
146
- } finally {
147
- ServletContext context = this.getServletContext();
148
- RequestDispatcher dispatcher = context.getRequestDispatcher(target);
149
- dispatcher.forward(request, response);
150
- }
151
- }
152
- }
153
- ```
154
- ###検索処理
155
- ```java
156
- package jp.co.keyaki.service;
157
-
158
- import java.sql.Connection;
159
- import java.sql.DriverManager;
160
- import java.sql.PreparedStatement;
161
- import java.sql.ResultSet;
162
- import java.sql.SQLException;
163
- import java.util.ArrayList;
164
- import java.util.List;
165
-
166
- import org.postgresql.util.PSQLException;
167
-
168
- import jp.co.keyaki.bean.KuyakushoBean;
169
-
170
- public class KuyakushoSearch {
171
-
172
- public List<KuyakushoBean> list()
173
- throws ClassNotFoundException, SQLException {
174
- List<KuyakushoBean> list = new ArrayList<>();
175
- Connection connection = null;
176
- PreparedStatement preparedStatement = null;
177
- ResultSet resultSet = null;
178
- KuyakushoBean KuyakushoBean = null;
179
-
180
- try {
181
- Class.forName("org.postgresql.Driver");
182
- connection = DriverManager.getConnection(
183
- jdbc:postgresql://【ホスト名】/【データベース名】", "【ユーザ名】", "【パスワード】");
184
-
185
- String SQL = "SELECT id, ku, zip, address, tel FROM t_kuyakusho";
186
- preparedStatement = connection.prepareStatement(SQL);
187
- ResultSet rs = preparedStatement.executeQuery();
188
-
189
- while (rs.next()) {
190
- KuyakushoBean = new KuyakushoBean();
191
- KuyakushoBean.setid(rs.getInt("id"));
192
- KuyakushoBean.setku(rs.getString("ku"));
193
- KuyakushoBean.setzip(rs.getString("zip"));
194
- KuyakushoBean.setaddress(rs.getString("address"));
195
- KuyakushoBean.settel(rs.getString("tel"));
196
- list.add(KuyakushoBean);
197
- }
198
-
199
- } catch (ClassNotFoundException e) {
200
- e.printStackTrace();
201
-
202
- } catch (PSQLException e) {
203
- e.printStackTrace();
204
-
205
- } finally {
206
- if (resultSet != null) {
207
- resultSet.close();
208
- }
209
- if (preparedStatement != null) {
210
- preparedStatement.close();
211
- }
212
- if (connection != null) {
213
- connection.close();
214
- }
215
- }
216
- return list;
217
- }
218
-
219
-
220
- public List<KuyakushoBean> name(int id)
221
- throws ClassNotFoundException, SQLException {
222
- List<KuyakushoBean> name = new ArrayList<>();
223
- Connection connection = null;
224
- PreparedStatement preparedStatement = null;
225
- ResultSet resultSet = null;
226
- KuyakushoBean KuyakushoBean = null;
227
-
228
- try {
229
- Class.forName("org.postgresql.Driver");
230
- connection = DriverManager.getConnection(
231
- "jdbc:postgresql://localhost:5432/lessondb", "postgres", "jobsupport");
232
-
233
- String SQL = "SELECT id, ku, zip, address, tel FROM t_kuyakusho WHERE id = ?";
234
- preparedStatement = connection.prepareStatement(SQL);
235
- preparedStatement.setInt(1, id);
236
- ResultSet rs = preparedStatement.executeQuery();
237
-
238
- if (rs.next()) {
239
- KuyakushoBean = new KuyakushoBean();
240
- KuyakushoBean.setid(rs.getInt("id"));
241
- KuyakushoBean.setku(rs.getString("ku"));
242
- KuyakushoBean.setzip(rs.getString("zip"));
243
- KuyakushoBean.setaddress(rs.getString("address"));
244
- KuyakushoBean.settel(rs.getString("tel"));
245
- name.add(KuyakushoBean);
246
- }
247
-
248
- } catch (ClassNotFoundException e) {
249
- e.printStackTrace();
250
-
251
- } catch (PSQLException e) {
252
- e.printStackTrace();
253
-
254
- } finally {
255
- if (resultSet != null) {
256
- resultSet.close();
257
- }
258
- if (preparedStatement != null) {
259
- preparedStatement.close();
260
- }
261
- if (connection != null) {
262
- connection.close();
263
- }
264
- }
265
- return name;
266
-
267
- }
268
- }
269
- ```
270
- ###Bean
271
- ```java
272
- package jp.co.keyaki.bean;
273
-
274
- public class KuyakushoBean {
275
-
276
- private int id;
277
- private String ku;
278
- private String zip;
279
- private String address;
280
- private String tel;
281
-
282
-
283
- public void setid(int id) {
284
- this.id = id;
285
- }
286
-
287
- public int getid() {
288
- return id;
289
- }
290
-
291
- public void setku(String ku) {
292
- this.ku = ku;
293
- }
294
-
295
- public String getku() {
296
- return ku;
297
- }
298
-
299
- public void setzip(String zip) {
300
- this.zip = zip;
301
- }
302
-
303
- public String getzip() {
304
- return zip;
305
- }
306
-
307
- public void setaddress(String address) {
308
- this.address = address;
309
- }
310
-
311
- public String getaddress() {
312
- return address;
313
- }
314
-
315
- public void settel(String tel) {
316
- this.tel = tel;
317
- }
318
-
319
- public String gettel() {
320
- return tel;
321
- }
322
-
323
- }
324
- ```