質問編集履歴

1

失礼しました、修正しました。

2020/08/25 04:09

投稿

ypk
ypk

スコア80

test CHANGED
File without changes
test CHANGED
@@ -68,6 +68,8 @@
68
68
 
69
69
  に加えてID(A01-0100)の左から3文字(A01)だけを抜き出してallGenreListに追加することは可能でしょうか。
70
70
 
71
+
72
+
71
73
  イメージとしてはallGenreListに
72
74
 
73
75
 
@@ -76,43 +78,61 @@
76
78
 
77
79
 
78
80
 
79
- が格納されているというこんにちは。現在Javaの学習をしているものです。
81
+ の3つが格納されているというイメージです。
82
+
83
+
84
+
80
-
85
+ 拙い説明で申し訳ございませんが、お力を貸していただけると嬉しいです。
86
+
81
- こちらの質問に関連したものになります。
87
+ どうぞよろくお願いいます。
82
-
83
-
84
-
85
- https://teratail.com/questions/287089
88
+
86
-
87
-
88
-
89
- getメソッドを用いて取得した英数字交じりのID(A01-0100)の左から3文字だけを抜き出したいと考えています。
89
+
90
-
91
-
92
-
90
+
93
- ###したいこと
91
+ ###現在の私のソースコード
94
-
95
-
96
-
92
+
93
+
94
+
97
- ```Java
95
+ ```DTO
98
-
96
+
99
- ArrayList<MainGenreDTO> allGenreList = new ArrayList<>();
97
+ public class MainGenreDTO {
98
+
100
-
99
+ private String id;
100
+
101
+ private String name;
102
+
103
+
104
+
101
- if (mainGenreList != null) {
105
+ public MainGenreDTO(String id, String name) {
106
+
102
-
107
+ this.id = id;
108
+
109
+ this.name = name;
110
+
111
+ }
112
+
113
+
114
+
115
+ public String getId() {
116
+
117
+ return id;
118
+
119
+ }
120
+
103
- for (int i = 0; i < mainGenreList.size(); ++i) {
121
+ public void setId(String id) {
122
+
104
-
123
+ this.id = id;
124
+
105
-
125
+ }
106
-
126
+
107
- GenreDTO genreInfo = mainGenreList.get(i);
127
+ public String getName() {
108
-
109
-
110
-
128
+
111
- MainGenreDTO mainGenre = new MainGenreDTO(genreInfo.getId(), genreInfo.getName());
129
+ return name;
130
+
112
-
131
+ }
113
-
114
-
132
+
115
- allGenreList.add(mainGenre);
133
+ public void setName(String name) {
134
+
135
+ this.name = name;
116
136
 
117
137
  }
118
138
 
@@ -122,210 +142,112 @@
122
142
 
123
143
 
124
144
 
125
- にて
126
-
127
-
128
-
129
- ```ここに言語を入力
130
-
131
- allGenreList.add(mainGenre);
132
-
133
- ```
134
-
135
-
136
-
137
- にセットする情報を
138
-
139
-
140
-
141
- ```ここに言語を入力
142
-
143
- //IDと名前
144
-
145
- genreInfo.getId(), genreInfo.getName()
146
-
147
- ```
148
-
149
- に加えてID(A01-0100)の左から3文字(A01)だけを抜き出してallGenreListに追加することは可能でしょうか。
150
-
151
- イメージとしてはallGenreListに
152
-
153
-
154
-
155
- > ID、名前、左から3文字だけ抜き出したID
156
-
157
-
158
-
159
- の3つが格納されているというイメージです。
160
-
161
-
162
-
163
- 拙い説明で申し訳ございませんが、お力を貸していただけると嬉しいです。
164
-
165
- どうぞよろしくお願いいたします。
166
-
167
-
168
-
169
- ###現在の私のソースコード
170
-
171
-
172
-
173
- ```DTO
174
-
175
- public class MainGenreDTO {
176
-
177
- private String id;
178
-
179
- private String name;
180
-
181
-
182
-
183
- public MainGenreDTO(String id, String name) {
184
-
185
- this.id = id;
186
-
187
- this.name = name;
188
-
189
- }
190
-
191
-
192
-
193
- public String getId() {
194
-
195
- return id;
196
-
197
- }
198
-
199
- public void setId(String id) {
200
-
201
- this.id = id;
202
-
203
- }
204
-
205
- public String getName() {
206
-
207
- return name;
208
-
209
- }
210
-
211
- public void setName(String name) {
212
-
213
- this.name = name;
145
+ ```Servlet
146
+
147
+ import java.io.IOException;
148
+
149
+ import java.sql.SQLException;
150
+
151
+ import java.util.ArrayList;
152
+
153
+
154
+
155
+ import javax.servlet.ServletException;
156
+
157
+ import javax.servlet.annotation.WebServlet;
158
+
159
+ import javax.servlet.http.HttpServlet;
160
+
161
+ import javax.servlet.http.HttpServletRequest;
162
+
163
+
164
+
165
+ import javax.servlet.http.HttpServletResponse;
166
+
167
+
168
+
169
+ import common.LoggerTester;
170
+
171
+ import dao.GenreDAO;
172
+
173
+ import dto.GenreDTO;
174
+
175
+
176
+
177
+ public class InitializationListener implements ServletContextListener {
178
+
179
+ public void contextInitialized(ServletContextEvent event) {
180
+
181
+ ServletContext context = event.getServletContext();
182
+
183
+
184
+
185
+ // ログの出力
186
+
187
+ LoggerTester lt = new LoggerTester();
188
+
189
+ lt.outActionLog("InitializationListener.java", "処理開始");
190
+
191
+
192
+
193
+ GenreDAO bn = new GenreDAO();
194
+
195
+ ArrayList<GenreDTO> maingenreList = bn.MainGenresAll();
196
+
197
+
198
+
199
+ ArrayList<MainGenreDTO> allGenreList = new ArrayList<>();
200
+
201
+ if (mainGenreList != null) {
202
+
203
+ for (int i = 0; i < mainGenreList.size(); ++i) {
204
+
205
+
206
+
207
+ GenreDTO genreInfo = mainGenreList.get(i);
208
+
209
+
210
+
211
+ MainGenreDTO mainGenre = new MainGenreDTO(genreInfo.getId(), genreInfo.getName());
212
+
213
+
214
+
215
+ allGenreList.add(mainGenre);
216
+
217
+ }
218
+
219
+ }
220
+
221
+
222
+
223
+     
224
+
225
+     //DTOにセットしたmaingenreidとmaingenrenameをallgenreListにDTO型として格納する
226
+
227
+      ArrayList<GenreDTO> allgenreList =
228
+
229
+
230
+
231
+
232
+
233
+ // genreListデータをapplicationスコープで保存
234
+
235
+ context.setAttribute("allgenreList", allgenreList);
236
+
237
+
238
+
239
+ }
240
+
241
+
242
+
243
+ public void contextDestoryed(ServletContextEvent event) {
244
+
245
+ ServletContext context = event.getServletContext();
246
+
247
+ context.removeAttribute("allgenreList"); // 削除処理
214
248
 
215
249
  }
216
250
 
217
251
  }
218
252
 
219
253
  ```
220
-
221
-
222
-
223
- ```Servlet
224
-
225
- import java.io.IOException;
226
-
227
- import java.sql.SQLException;
228
-
229
- import java.util.ArrayList;
230
-
231
-
232
-
233
- import javax.servlet.ServletException;
234
-
235
- import javax.servlet.annotation.WebServlet;
236
-
237
- import javax.servlet.http.HttpServlet;
238
-
239
- import javax.servlet.http.HttpServletRequest;
240
-
241
-
242
-
243
- import javax.servlet.http.HttpServletResponse;
244
-
245
-
246
-
247
- import common.LoggerTester;
248
-
249
- import dao.GenreDAO;
250
-
251
- import dto.GenreDTO;
252
-
253
-
254
-
255
- public class InitializationListener implements ServletContextListener {
256
-
257
- public void contextInitialized(ServletContextEvent event) {
258
-
259
- ServletContext context = event.getServletContext();
260
-
261
-
262
-
263
- // ログの出力
264
-
265
- LoggerTester lt = new LoggerTester();
266
-
267
- lt.outActionLog("InitializationListener.java", "処理開始");
268
-
269
-
270
-
271
- GenreDAO bn = new GenreDAO();
272
-
273
- ArrayList<GenreDTO> maingenreList = bn.MainGenresAll();
274
-
275
-
276
-
277
- ArrayList<MainGenreDTO> allGenreList = new ArrayList<>();
278
-
279
- if (mainGenreList != null) {
280
-
281
- for (int i = 0; i < mainGenreList.size(); ++i) {
282
-
283
-
284
-
285
- GenreDTO genreInfo = mainGenreList.get(i);
286
-
287
-
288
-
289
- MainGenreDTO mainGenre = new MainGenreDTO(genreInfo.getId(), genreInfo.getName());
290
-
291
-
292
-
293
- allGenreList.add(mainGenre);
294
-
295
- }
296
-
297
- }
298
-
299
-
300
-
301
-     
302
-
303
-     //DTOにセットしたmaingenreidとmaingenrenameをallgenreListにDTO型として格納する
304
-
305
-      ArrayList<GenreDTO> allgenreList =
306
-
307
-
308
-
309
-
310
-
311
- // genreListデータをapplicationスコープで保存
312
-
313
- context.setAttribute("allgenreList", allgenreList);
314
-
315
-
316
-
317
- }
318
-
319
-
320
-
321
- public void contextDestoryed(ServletContextEvent event) {
322
-
323
- ServletContext context = event.getServletContext();
324
-
325
- context.removeAttribute("allgenreList"); // 削除処理
326
-
327
- }
328
-
329
- }
330
-
331
- ```