質問編集履歴

2

本文

2023/02/08 08:48

投稿

Pocchi
Pocchi

スコア3

test CHANGED
@@ -1 +1 @@
1
- バリデーション、DB検索のエラーについて
1
+ バリデーション、DB検索などのエラーコードのまとめ方
test CHANGED
@@ -1,86 +1,74 @@
1
1
  ### 実現したいこと
2
2
  ログイン画面で、入力チェックをするバリデーションと入力したデータでDBを検索し、データを照合するチェックを行い、エラーがあればエラー画面へ、エラーがなければindex画面へ、と遷移させたいです。
3
3
 
4
- ### 前提
4
+ ### 質問詳細
5
-
6
- 現在の書き方だと、最初に入力項目へのバリデーションをして、
5
+ 現在の書き方だと、バリデーションとDB検索一つのif文であらわしていますが
7
- バリデーションOKのデータのみ後続に進み、
8
- DBの検索を行う、という流れにしていますが、
9
- DBの検索にも「データがない」「不正な引」などエラーが出るため
6
+ エラー、try-catch時と複エラー画面に遷移す書き方になるため
10
- エラー画面への遷移2回書くことになります。
7
+ 書き方冗長気味なことが気になります。
11
-
12
- エラー画面への遷移は1回のみの遷移して、バリデー、DB検索時のエラーとまとめて書きたいのですが、ソースの組み方が分からず皆さまのアドバイスを頂ければ幸いです。
8
+ もっとシンプルな書き方があれば教えて頂けいでしょうか?
13
9
 
14
10
  ### 該当のソースコード
15
11
 
16
- ```ここに言語名を入力
12
+ ```Java
13
+ */
17
14
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
18
- throws ServletException, IOException, NoResultException{
15
+ throws ServletException, IOException, NoResultException {
19
- // TODO Auto-generated method stub
20
16
 
21
- System.out.println("LoginservletPost");
17
+ String code = request.getParameter("emp_number");
18
+ String password = request.getParameter("emp_pass");
19
+ String error = "";
22
20
 
23
- EntityManager em = DBUtil.createEntityManager();
21
+ EntityManager em = DBUtil.createEntityManager();
24
22
 
23
+ //入力エラーチェック
24
+ if (code == null || code.equals("")) {
25
+ //社員番号ブランクエラーによるエラー画面遷移
26
+ error = "社員番号がブランクです";
25
- String code = request.getParameter("emp_number");
27
+ request.setAttribute("error", error);
26
- String password = request.getParameter("emp_pass");
28
+ RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/views/error/loginerrors.jsp");
29
+ rd.forward(request, response);
27
30
 
31
+ } else if (password == null || password.equals("")) {
32
+ //パスワードブランクエラーによるエラー画面遷移
28
- System.out.println("emp_number" + code);
33
+ error = "パスワードがブランクです";
29
- System.out.println("password" + password);
34
+ request.setAttribute("error", error);
35
+ RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/views/error/loginerrors.jsp");
36
+ rd.forward(request, response);
30
37
 
38
+ } else {
39
+ //入力バリデーションクリア
40
+ try {
41
+ Employees employees = (Employees) em
31
- List<String> errors = new ArrayList<String>();
42
+ .createNativeQuery("select * from employees where code =:code", Employees.class)
43
+ .setParameter("code", code)
44
+ .getSingleResult();
32
45
 
33
- //社員番号のブランクチェック
34
- if (code == null || code.equals("")) {
46
+ request.setAttribute("employees", employees);
47
+ RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/views/layout/index.jsp");
35
- errors.add("社員番号を入力してください");
48
+ rd.forward(request, response);
36
- }
37
49
 
38
- //パスワードのブランクチェック
39
- if (password == null || password.equals("")) {
40
- errors.add("パスワードを入力してください");
41
- }
42
-
43
- //入力内容にエラーがあるかどうか
44
- if (errors.size() > 0) {
50
+ } catch (NoResultException e) {
45
- //エラありログインエラー
51
+ //検索デタ0件
52
+ e.printStackTrace();
46
- System.out.println("ログイン入力エラー");
53
+ error = "社員番号未登録です";
47
- request.setAttribute("loginError", "true");
48
- request.setAttribute("errors", errors);
54
+ request.setAttribute("error", error);
49
55
  RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/views/error/loginerrors.jsp");
50
56
  rd.forward(request, response);
51
- } else {
52
- //エラーなしログイン成功
53
57
 
54
- try {
55
-
56
- Employees employees = (Employees) em
58
+ } catch (NonUniqueResultException e) {
57
- .createNativeQuery("select * from employees where code =:code",Employees.class)
58
- .setParameter("code", code)
59
+ //検索データが複数件
59
- .getSingleResult();
60
+ e.printStackTrace();
60
-
61
- System.out.println("ログイン成功");
61
+ error = "社員番号が複数あります";
62
- request.setAttribute("employees", employees);
62
+ request.setAttribute("error", error);
63
- RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/views/layout/index.jsp");
63
+ RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/views/error/loginerrors.jsp");
64
- rd.forward(request, response);
64
+ rd.forward(request, response);
65
- } catch (ServletException e) {
66
- // TODO 自動生成された catch ブロック
67
- e.printStackTrace();
68
- } catch (NoResultException e) {
69
- System.out.println("ログイン社員番号未登録エラー");
70
- // TODO 自動生成された catch ブロック
71
- errors.add("社員番号が登録されていません");
72
- request.setAttribute("loginError", "true");
73
- request.setAttribute("errors", errors);
74
- RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/views/error/loginerrors.jsp");
75
- rd.forward(request, response);
76
-
77
-
78
- e.printStackTrace();
79
- }
80
65
 
81
66
  }
82
67
 
68
+ }
69
+
83
70
  }
71
+
84
72
  }
85
73
  ```
86
74
 

1

本文修正

2023/02/08 07:04

投稿

Pocchi
Pocchi

スコア3

test CHANGED
File without changes
test CHANGED
@@ -3,7 +3,6 @@
3
3
 
4
4
  ### 前提
5
5
 
6
- ここに質問の内容を詳しく書いてください。
7
6
  現在の書き方だと、最初に入力項目へのバリデーションをして、
8
7
  バリデーションOKのデータのみ後続に進み、
9
8
  DBの検索を行う、という流れにしていますが、