<문자열 제어할 때 쓰기 좋은 객체 및 서비스 메소드 몇개> |
String array = array.trim();
// 양쪽 여백이 있을 시 삭제해줌
String[] arrays = arrays.split(" ");
// 사이에 여백이 있을 시 그것을 기준으로 배열에 나눠서 넣어줌
String[] arrays = arrays.trim().split(" ");
// 응용. 양쪽 여백을 지우고 사이에 있는 여백을 기준으로 나눠서 배열에 하나씩 넣어줌
List<String> a = Arrays.asList(string 배열);
// 배열로 굳이 2차원배열 같은 걸 돌릴 필요 없이 List의 서비스메소드들을 편하게 사용할 수 있음
a.removeAll(b)
// a리스트에서 b리스트를 빼서 a에 담는다
// 기존의 배열이 사라지기 때문에 a의 원본 배열은 두고 a라는 새로운 list 인스턴스로 만들어서 사용한다
sysout a_origin [84, 83, 82, 81, 80, 79, 78, 77, 76, 71]
sysout b [84, 83, 82, 81, 80, 71]
sysout a [79, 78, 77, 76]
<일괄공개 버튼 구현> |
<일괄공개 버튼 구현을 위한 현재 페이지의 id를 모아놓는 히든 값 설정 (JSP+JSTL+EL)> |
1
2
3
4
5
6
7
|
<c:set var="ids" value="" />
<c:forEach var="n" items="${list}">
<c:set var="ids" value="${ids} ${n.id}" />
</c:forEach>
<input type="hidden" name="ids" value="${ids}">
<input type="submit" class="btn-text btn-default" name="cmd" value="일괄공개">
<input type="submit" class="btn-text btn-default" name="cmd" value="일괄삭제">
|
cs |
type="hidden" 을 걸어두면 사용자(클라이언트)에게는 보이지 않는다
<체크된 id들과 위에서 얻어온 페이지 id들을 사용한 조건처리 (Servlet)> |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
String[] openIds = request.getParameterValues("open-id");
String[] delIds = request.getParameterValues("del-id");
String cmd = request.getParameter("cmd");
String ids_ = request.getParameter("ids");
String[] ids = ids_.trim().split(" ");
// jsp에서 받아올 때 맨앞에도 빈공백이 하나 더 있어서 [ , id1, id2...] 이런식으로 나와버림
// trim으로 양쪽 여백을 지운 상태에서 split 해야함 value="${ids} ${n.id}" 여기서 ids n.id 사이에
// 여백이 0번째에도 적용되서 그런 듯
NoticeService service = new NoticeService();
switch(cmd) {
case "일괄공개":
for(String openId : openIds)
System.out.printf("open id : %s\n", openId);
List<String> oids = Arrays.asList(openIds);
List<String> cids = new ArrayList(Arrays.asList(ids));
cids.removeAll(oids);
//바로 Arrays.asList(ids).removeAll(oids) 하는 건 정적배열 상태라 안됨
//그렇기에 바로 삭제나 추가를 할 수 없어 새로운 List에 담아 removeAll 해야함
System.out.println(ids_);
System.out.println(Arrays.asList(ids));
System.out.println(oids);
System.out.println(cids);
// 실제 Transaction 처리 부분이다
// service.pubNoticeList(openIds);
// service.closeNoticeList(cids);
// 일괄공개 기능이라는 한가지의 논리적 기능을 수행하지만 DB로 쿼리 명령문을 전달하는 동작은 두 함수에 걸쳐 두번 실행된다
// 어떠한 영향으로 둘중 하나의 동작만 실행 될 경우 그것이 Transaction 처리를 못한 것이 된다
service.pubNoticeAll(oids, cids);
// 바람직한 Transaction 처리 (이지만 서비스메소드 부분에서 아직 처리하지 않고 있다)
break;
case "일괄삭제":
int[] ids1 = new int[delIds.length];
for(int i=0; i<delIds.length;i++)
ids1[i] = Integer.parseInt(delIds[i]);
int result = service.deleteNoticeAll(ids1);
break;
}
// post 처리 후 자신의 get요청을 호출해 url 재요청
response.sendRedirect("list");
|
cs |
<사용자(Controller) 편의성 제공을 위한 서비스 메소드 오버로드> |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
public int pubNoticeAll(int[] oids, int[] cids){
List<String> oidsList = new ArrayList<>();
for (int i = 0; i < oids.length; i++) {
oidsList.add(String.valueOf(oids));
}
List<String> cidsList = new ArrayList<>();
for (int i = 0; i < cids.length; i++) {
cidsList.add(String.valueOf(cids));
}
// 스트링 리스트 객체를 만든다. for문 돌려서 문자열로 형변환해서 차곡차곡 담는다
// for문 하나로 해결될 걸 라이브러리를 쓰는 건 역으로 성능 저하를 일으킬 수 있다
// 아래의 오버로드에서는 스트링 리스트 타입을 매개변수로 받고있다
return pubNoticeAll(oidsList, cidsList);
}
public int pubNoticeAll(List<String> oids, List<String> cids){
// 위에서 정수 배열 인자로 호출당한 친구가 만들어 보내준 스트링 리스트를 이어받는다
String oidsCSV = String.join(",", oids);
String cidsCSV = String.join(",", cids);
// CSV 형으로 만들어서 아래의 친구를 호출한다
return pubNoticeAll(oidsCSV, cidsCSV);
}
public int pubNoticeAll(String oidsCSV, String cidsCSV){
// 원하는 타입의 자료형을 얻었다
int result = 0;
String sqlOpen = String.format("update notice set pub=1 where id in (%s)", oidsCSV);
String sqlClose = String.format("update notice set pub=0 where id in (%s)", cidsCSV);
// "+odisCSV+" 이러한 형식이 보기 싫다면 위처럼 format 하면 된다
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection(url, dID, dPW);
Statement stOpen = con.createStatement();
result += stOpen.executeUpdate(sqlOpen);
Statement stClose = con.createStatement();
result += stClose.executeUpdate(sqlClose);
// 현재 Transaction 처리를 하고 있지 않음. 해야한다.
stOpen.close();
stClose.close();
con.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// DB에 잘 등록되었다면 적용된 행의 갯수를 반환한다
return result;
}
|
cs |
oidsCSV에서 CSV의 뜻은 "콤마로 나눠진(Seperate) 값(Value)"이라는 뜻이라고 함
(하나의 문자열인데 "철수,민수,민지,슬기" 이런 식으로 들어있다)
'Development > Java' 카테고리의 다른 글
Maven 사용법 (0) | 2022.08.24 |
---|---|
Spring Framework, DI, IoC container #1 (0) | 2022.08.23 |
파일 등록과 사용 #11 (0) | 2022.08.22 |
Servlet / Jsp admin page #10 (0) | 2022.08.22 |
JDBC 제어 oracleDB SQL 게시판용 몇개 #9 (0) | 2022.08.21 |