質問編集履歴
2
レイアウト
test
CHANGED
File without changes
|
test
CHANGED
@@ -10,33 +10,435 @@
|
|
10
10
|
|
11
11
|
#### コード
|
12
12
|
|
13
|
+
```ここに言語を入力
|
14
|
+
|
13
15
|
**ビーン**
|
14
16
|
|
17
|
+
package jp.co.sss.shop.bean;
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
/**
|
22
|
+
|
23
|
+
* 会員情報クラス
|
24
|
+
|
25
|
+
*
|
26
|
+
|
27
|
+
* @author SystemShared
|
28
|
+
|
29
|
+
*/
|
30
|
+
|
31
|
+
public class UserBean {
|
32
|
+
|
33
|
+
/**
|
34
|
+
|
35
|
+
* 会員ID
|
36
|
+
|
37
|
+
*/
|
38
|
+
|
15
39
|
private Integer id;
|
16
40
|
|
41
|
+
/**
|
42
|
+
|
43
|
+
* 会員メールアドレス
|
44
|
+
|
45
|
+
*/
|
46
|
+
|
47
|
+
private String email;
|
48
|
+
|
49
|
+
/**
|
50
|
+
|
51
|
+
* パスワード
|
52
|
+
|
53
|
+
*/
|
54
|
+
|
55
|
+
private String password;
|
56
|
+
|
57
|
+
/**
|
58
|
+
|
59
|
+
* 会員名
|
60
|
+
|
61
|
+
*/
|
62
|
+
|
63
|
+
private String name;
|
64
|
+
|
65
|
+
/**
|
66
|
+
|
67
|
+
* 郵便番号
|
68
|
+
|
69
|
+
*/
|
70
|
+
|
71
|
+
private String postalCode;
|
72
|
+
|
73
|
+
/**
|
74
|
+
|
75
|
+
* 住所
|
76
|
+
|
77
|
+
*/
|
78
|
+
|
79
|
+
private String address;
|
80
|
+
|
81
|
+
/**
|
82
|
+
|
83
|
+
* 電話番号
|
84
|
+
|
85
|
+
*/
|
86
|
+
|
87
|
+
private String phoneNumber;
|
88
|
+
|
89
|
+
/**
|
90
|
+
|
91
|
+
* 権限
|
92
|
+
|
93
|
+
*/
|
94
|
+
|
95
|
+
private Integer authority;
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
/**
|
102
|
+
|
103
|
+
* 削除フラグ 0:未削除、1:削除済み
|
104
|
+
|
105
|
+
*/
|
106
|
+
|
107
|
+
private Integer deleteFlag;
|
108
|
+
|
109
|
+
/**
|
110
|
+
|
111
|
+
* 登録日付
|
112
|
+
|
113
|
+
*/
|
114
|
+
|
115
|
+
private String insertDate;
|
116
|
+
|
117
|
+
|
118
|
+
|
17
|
-
public Integer getId() {
|
119
|
+
public Integer getId() {
|
18
120
|
|
19
121
|
return id;
|
20
122
|
|
21
123
|
}
|
22
124
|
|
125
|
+
|
126
|
+
|
23
127
|
public void setId(Integer id) {
|
24
128
|
|
25
129
|
this.id = id;
|
26
130
|
|
131
|
+
}
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
public String getEmail() {
|
136
|
+
|
137
|
+
return email;
|
138
|
+
|
139
|
+
}
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
public void setEmail(String email) {
|
144
|
+
|
145
|
+
this.email = email;
|
146
|
+
|
147
|
+
}
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
public String getPassword() {
|
152
|
+
|
153
|
+
return password;
|
154
|
+
|
155
|
+
}
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
public void setPassword(String password) {
|
160
|
+
|
161
|
+
this.password = password;
|
162
|
+
|
163
|
+
}
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
public String getName() {
|
168
|
+
|
169
|
+
return name;
|
170
|
+
|
171
|
+
}
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
public void setName(String name) {
|
176
|
+
|
177
|
+
this.name = name;
|
178
|
+
|
179
|
+
}
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
public String getPostalCode() {
|
184
|
+
|
185
|
+
return postalCode;
|
186
|
+
|
187
|
+
}
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
public void setPostalCode(String postalCode) {
|
192
|
+
|
193
|
+
this.postalCode = postalCode;
|
194
|
+
|
195
|
+
}
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
public String getAddress() {
|
200
|
+
|
201
|
+
return address;
|
202
|
+
|
203
|
+
}
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
public void setAddress(String address) {
|
208
|
+
|
209
|
+
this.address = address;
|
210
|
+
|
211
|
+
}
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
public String getPhoneNumber() {
|
216
|
+
|
217
|
+
return phoneNumber;
|
218
|
+
|
219
|
+
}
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
public void setPhoneNumber(String phoneNumber) {
|
224
|
+
|
225
|
+
this.phoneNumber = phoneNumber;
|
226
|
+
|
227
|
+
}
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
public Integer getAuthority() {
|
232
|
+
|
233
|
+
return authority;
|
234
|
+
|
235
|
+
}
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
public void setAuthority(Integer authority) {
|
240
|
+
|
241
|
+
this.authority = authority;
|
242
|
+
|
243
|
+
}
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
public Integer getDeleteFlag() {
|
250
|
+
|
251
|
+
return deleteFlag;
|
252
|
+
|
253
|
+
}
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
public void setDeleteFlag(Integer deleteFlag) {
|
258
|
+
|
259
|
+
this.deleteFlag = deleteFlag;
|
260
|
+
|
261
|
+
}
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
public String getInsertDate() {
|
266
|
+
|
267
|
+
return insertDate;
|
268
|
+
|
269
|
+
}
|
270
|
+
|
271
|
+
|
272
|
+
|
273
|
+
public void setInsertDate(String insertDate) {
|
274
|
+
|
275
|
+
this.insertDate = insertDate;
|
276
|
+
|
277
|
+
}
|
278
|
+
|
279
|
+
}
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
|
284
|
+
|
285
|
+
```
|
286
|
+
|
287
|
+
```ここに言語を入力
|
288
|
+
|
289
|
+
|
290
|
+
|
27
291
|
|
28
292
|
|
29
293
|
**コントローラ**
|
30
294
|
|
295
|
+
package jp.co.sss.shop.controller.order;
|
296
|
+
|
297
|
+
|
298
|
+
|
299
|
+
import java.util.ArrayList;
|
300
|
+
|
301
|
+
import java.util.List;
|
302
|
+
|
303
|
+
|
304
|
+
|
305
|
+
import javax.servlet.http.HttpSession;
|
306
|
+
|
307
|
+
|
308
|
+
|
309
|
+
import org.springframework.beans.BeanUtils;
|
310
|
+
|
311
|
+
import org.springframework.beans.factory.annotation.Autowired;
|
312
|
+
|
313
|
+
import org.springframework.data.domain.Page;
|
314
|
+
|
315
|
+
import org.springframework.data.domain.Pageable;
|
316
|
+
|
317
|
+
import org.springframework.stereotype.Controller;
|
318
|
+
|
319
|
+
import org.springframework.ui.Model;
|
320
|
+
|
321
|
+
import org.springframework.web.bind.annotation.ModelAttribute;
|
322
|
+
|
323
|
+
import org.springframework.web.bind.annotation.PathVariable;
|
324
|
+
|
325
|
+
import org.springframework.web.bind.annotation.RequestMapping;
|
326
|
+
|
327
|
+
import org.springframework.web.bind.annotation.RequestMethod;
|
328
|
+
|
329
|
+
|
330
|
+
|
331
|
+
import jp.co.sss.shop.bean.OrderBean;
|
332
|
+
|
333
|
+
import jp.co.sss.shop.bean.OrderItemBean;
|
334
|
+
|
335
|
+
import jp.co.sss.shop.bean.UserBean;
|
336
|
+
|
337
|
+
import jp.co.sss.shop.entity.Order;
|
338
|
+
|
339
|
+
import jp.co.sss.shop.entity.OrderItem;
|
340
|
+
|
341
|
+
import jp.co.sss.shop.form.OrderShowForm;
|
342
|
+
|
343
|
+
import jp.co.sss.shop.repository.OrderRepository;
|
344
|
+
|
345
|
+
import jp.co.sss.shop.util.PriceCalc;
|
346
|
+
|
347
|
+
/**
|
348
|
+
|
349
|
+
* 注文管理 一覧表示機能(一般用)のコントローラクラス
|
350
|
+
|
351
|
+
*
|
352
|
+
|
353
|
+
* @author SystemShared
|
354
|
+
|
355
|
+
*/
|
356
|
+
|
357
|
+
@Controller
|
358
|
+
|
359
|
+
public class OrderShowCustomerController {
|
360
|
+
|
361
|
+
/**
|
362
|
+
|
363
|
+
* 注文情報
|
364
|
+
|
365
|
+
*/
|
366
|
+
|
367
|
+
@Autowired
|
368
|
+
|
369
|
+
OrderRepository orderRepository;
|
370
|
+
|
371
|
+
|
372
|
+
|
373
|
+
/**
|
374
|
+
|
375
|
+
* セッション
|
376
|
+
|
377
|
+
*/
|
378
|
+
|
379
|
+
@Autowired
|
380
|
+
|
381
|
+
HttpSession session;
|
382
|
+
|
383
|
+
|
384
|
+
|
385
|
+
|
386
|
+
|
387
|
+
|
388
|
+
|
389
|
+
/**
|
390
|
+
|
391
|
+
* 注文情報一覧表示処理
|
392
|
+
|
393
|
+
*
|
394
|
+
|
395
|
+
* @param model
|
396
|
+
|
397
|
+
* Viewとの値受渡し
|
398
|
+
|
399
|
+
* @param form
|
400
|
+
|
401
|
+
* 表示用注文情報
|
402
|
+
|
403
|
+
* @param session
|
404
|
+
|
405
|
+
* セッション情報
|
406
|
+
|
407
|
+
* @param pageable
|
408
|
+
|
409
|
+
* ページング情報
|
410
|
+
|
411
|
+
* @return "order/list/order_list_admin" 注文情報 一覧画面へ
|
412
|
+
|
413
|
+
*/
|
414
|
+
|
415
|
+
@RequestMapping(path = "/order/list", method = RequestMethod.GET)
|
416
|
+
|
417
|
+
public String showOrderList(Model model, @ModelAttribute OrderShowForm form,
|
418
|
+
|
419
|
+
Pageable pageable) {
|
420
|
+
|
421
|
+
|
422
|
+
|
423
|
+
|
424
|
+
|
425
|
+
// ログイン中のユーザの注文情報を取得
|
426
|
+
|
31
|
-
UserBean userBean = new UserBean();
|
427
|
+
UserBean userBean = new UserBean();
|
32
|
-
|
428
|
+
|
429
|
+
|
430
|
+
|
33
|
-
userBean.setId(getInteger(id));
|
431
|
+
userBean.setId(getInteger(id));
|
34
|
-
|
432
|
+
|
433
|
+
|
434
|
+
|
35
|
-
Page<Order> orderList = orderRepository.findByUserIdOrderByInsertDateDesc(id, pageable);
|
435
|
+
Page<Order> orderList = orderRepository.findByUserIdOrderByInsertDateDesc(id, pageable);
|
436
|
+
|
437
|
+
|
438
|
+
|
36
|
-
|
439
|
+
// 注文情報リストを生成
|
37
|
-
|
38
|
-
|
440
|
+
|
39
|
-
List<OrderBean> orderBeanList = new ArrayList<OrderBean>();
|
441
|
+
List<OrderBean> orderBeanList = new ArrayList<OrderBean>();
|
40
442
|
|
41
443
|
for (Order order : orderList){
|
42
444
|
|
@@ -52,12 +454,250 @@
|
|
52
454
|
|
53
455
|
|
54
456
|
|
457
|
+
//注文時点の単価を合計するための一時変数
|
458
|
+
|
459
|
+
int total = 0;
|
460
|
+
|
461
|
+
|
462
|
+
|
463
|
+
//orderレコードから紐づくOrderItemのListを取り出す
|
464
|
+
|
465
|
+
List<OrderItem> orderItemList = order.getOrderItemsList();
|
466
|
+
|
467
|
+
|
468
|
+
|
469
|
+
for(OrderItem orderItem : orderItemList) {
|
470
|
+
|
471
|
+
|
472
|
+
|
473
|
+
//購入時単価 * 買った個数をもとめて、合計に加算
|
474
|
+
|
475
|
+
total += ( orderItem.getPrice() * orderItem.getQuantity() );
|
476
|
+
|
477
|
+
}
|
478
|
+
|
479
|
+
//Orderに改めて注文時点の単価をセット
|
480
|
+
|
481
|
+
orderBean.setTotal(total);
|
482
|
+
|
483
|
+
|
484
|
+
|
485
|
+
orderBeanList.add(orderBean);
|
486
|
+
|
487
|
+
}
|
488
|
+
|
489
|
+
|
490
|
+
|
491
|
+
// 注文情報リストをViewへ渡す
|
492
|
+
|
493
|
+
model.addAttribute("pages", orderList);
|
494
|
+
|
495
|
+
model.addAttribute("orders", orderBeanList);
|
496
|
+
|
497
|
+
model.addAttribute("url", "/order/list");
|
498
|
+
|
499
|
+
|
500
|
+
|
501
|
+
return "order/list/order_list";
|
502
|
+
|
503
|
+
|
504
|
+
|
505
|
+
}
|
506
|
+
|
507
|
+
```
|
508
|
+
|
509
|
+
```
|
510
|
+
|
511
|
+
**リポジトリ**
|
512
|
+
|
513
|
+
package jp.co.sss.shop.repository;
|
514
|
+
|
515
|
+
|
516
|
+
|
517
|
+
import org.springframework.data.domain.Page;
|
518
|
+
|
519
|
+
import org.springframework.data.domain.Pageable;
|
520
|
+
|
521
|
+
import org.springframework.data.jpa.repository.JpaRepository;
|
522
|
+
|
523
|
+
import org.springframework.data.jpa.repository.Query;
|
524
|
+
|
525
|
+
import org.springframework.stereotype.Repository;
|
526
|
+
|
527
|
+
|
528
|
+
|
529
|
+
import jp.co.sss.shop.entity.Order;
|
530
|
+
|
531
|
+
import jp.co.sss.shop.util.JPQLConstant;
|
532
|
+
|
533
|
+
|
534
|
+
|
535
|
+
/**
|
536
|
+
|
537
|
+
* ordersテーブル用リポジトリ
|
538
|
+
|
539
|
+
*
|
540
|
+
|
541
|
+
* @author System Shared
|
542
|
+
|
543
|
+
*/
|
544
|
+
|
545
|
+
@Repository
|
546
|
+
|
547
|
+
|
548
|
+
|
549
|
+
public interface OrderRepository extends JpaRepository<Order, Integer> {
|
550
|
+
|
551
|
+
|
552
|
+
|
553
|
+
Order findTop1ByOrderByInsertDateDesc();
|
554
|
+
|
555
|
+
|
556
|
+
|
557
|
+
// 会員IDに該当する注文情報を注文日付順で検索
|
558
|
+
|
559
|
+
Page<Order> findByUserIdOrderByInsertDateDesc(int userId, Pageable pageable);
|
560
|
+
|
561
|
+
```
|
562
|
+
|
563
|
+
```
|
564
|
+
|
565
|
+
**ログイン**
|
566
|
+
|
567
|
+
package jp.co.sss.shop.validator;
|
568
|
+
|
569
|
+
|
570
|
+
|
571
|
+
import javax.servlet.http.HttpSession;
|
572
|
+
|
573
|
+
import javax.validation.ConstraintValidator;
|
574
|
+
|
575
|
+
import javax.validation.ConstraintValidatorContext;
|
576
|
+
|
577
|
+
|
578
|
+
|
579
|
+
import org.springframework.beans.BeanWrapper;
|
580
|
+
|
581
|
+
import org.springframework.beans.BeanWrapperImpl;
|
582
|
+
|
583
|
+
import org.springframework.beans.factory.annotation.Autowired;
|
584
|
+
|
585
|
+
|
586
|
+
|
587
|
+
import jp.co.sss.shop.annotation.LoginCheck;
|
588
|
+
|
589
|
+
import jp.co.sss.shop.bean.UserBean;
|
590
|
+
|
591
|
+
import jp.co.sss.shop.entity.User;
|
592
|
+
|
593
|
+
import jp.co.sss.shop.repository.UserRepository;
|
594
|
+
|
595
|
+
import jp.co.sss.shop.util.Constant;
|
596
|
+
|
597
|
+
|
598
|
+
|
599
|
+
/**
|
600
|
+
|
601
|
+
* ログインチェックの独自検証クラス
|
602
|
+
|
603
|
+
*
|
604
|
+
|
605
|
+
* @author System Shared
|
606
|
+
|
607
|
+
*/
|
608
|
+
|
609
|
+
public class LoginValidator implements ConstraintValidator<LoginCheck, Object> {
|
610
|
+
|
611
|
+
private String email;
|
612
|
+
|
613
|
+
private String password;
|
614
|
+
|
615
|
+
|
616
|
+
|
617
|
+
@Autowired
|
618
|
+
|
619
|
+
UserRepository userRepository;
|
620
|
+
|
621
|
+
|
622
|
+
|
623
|
+
@Autowired
|
624
|
+
|
625
|
+
HttpSession session;
|
626
|
+
|
627
|
+
|
628
|
+
|
629
|
+
@Override
|
630
|
+
|
631
|
+
public void initialize(LoginCheck annotation) {
|
632
|
+
|
633
|
+
this.email = annotation.fieldEmail();
|
634
|
+
|
635
|
+
this.password = annotation.fieldPassword();
|
636
|
+
|
637
|
+
}
|
638
|
+
|
639
|
+
|
640
|
+
|
641
|
+
@Override
|
642
|
+
|
643
|
+
public boolean isValid(Object value, ConstraintValidatorContext context) {
|
644
|
+
|
645
|
+
BeanWrapper beanWrapper = new BeanWrapperImpl(value);
|
646
|
+
|
647
|
+
|
648
|
+
|
649
|
+
String email = (String) beanWrapper.getPropertyValue(this.email);
|
650
|
+
|
651
|
+
String password = (String) beanWrapper.getPropertyValue(this.password);
|
652
|
+
|
653
|
+
|
654
|
+
|
655
|
+
User user = userRepository.findByEmailAndDeleteFlag(email, Constant.NOT_DELETED);
|
656
|
+
|
657
|
+
|
658
|
+
|
659
|
+
if (user != null && password.equals(user.getPassword())) {
|
660
|
+
|
661
|
+
UserBean userBean = new UserBean();
|
662
|
+
|
663
|
+
|
664
|
+
|
665
|
+
userBean.setId(user.getId());
|
666
|
+
|
667
|
+
userBean.setName(user.getName());
|
668
|
+
|
669
|
+
userBean.setAuthority(user.getAuthority());
|
670
|
+
|
671
|
+
|
672
|
+
|
673
|
+
// セッションスコープにログインしたユーザの情報を登録
|
674
|
+
|
675
|
+
session.setAttribute("user", userBean);
|
676
|
+
|
677
|
+
return true;
|
678
|
+
|
679
|
+
}
|
680
|
+
|
681
|
+
else {
|
682
|
+
|
683
|
+
return false;
|
684
|
+
|
685
|
+
}
|
686
|
+
|
687
|
+
}
|
688
|
+
|
689
|
+
}
|
690
|
+
|
691
|
+
|
692
|
+
|
693
|
+
```
|
694
|
+
|
55
695
|
### エラー内容
|
56
696
|
|
57
697
|
コントローラーのidが2つとも(idを変数に解決できません)というエラーが出る状態です。
|
58
698
|
|
59
699
|
またゴール地点としては
|
60
700
|
|
61
|
-
|
701
|
+
セッションに登録されたidの情報のみ取ってくる事ができたらと思っています。
|
62
702
|
|
63
703
|
説明下手ですみません。また現状のコードの書き方の違いや違和感などありましたらご指摘お願いします。
|
1
コントローラー
test
CHANGED
File without changes
|
test
CHANGED
@@ -54,7 +54,7 @@
|
|
54
54
|
|
55
55
|
### エラー内容
|
56
56
|
|
57
|
-
idが2つとも(idを変数に解決できません)というエラーが出る状態です。
|
57
|
+
コントローラーのidが2つとも(idを変数に解決できません)というエラーが出る状態です。
|
58
58
|
|
59
59
|
またゴール地点としては
|
60
60
|
|