반응형
Admin 페이지를 위한 서비스 목록 추가하기
일괄 공개할 서비스 함수
체크한 게시글을 넘기기 위해 게시 목록 출력
pubNoticeAll(ids)
일괄 삭제할 서비스 함수
removeNoticeAll(ids)
여러 개를 삭제할 함수
공지사항에 글을 쓰기 위한 함수
insertNotice(notice)
자세한 페이지에서 수정, 삭제 함수
deleteNotice(id)
updateNotice(Notice)
index페이지에서 최신 공지사항 함수
getNoticeNewestList()
public int removeNoticeAll(int[] ids){ //몇개가 삭제됬는지
return 0;
}
public int pubNoticeAll(int[] ids){ //몇개가 공개됬는지
return 0;
}
public int insertNotice(Notice notice){ //추가됬으면 1 아니면 0을 반환
return 0;
}
public int deleteNotice(int id){ //삭제됬으면 1 아니면 0
return 0;
}
public int updateNotice(Notice notice){
return 0;
}
public List<Notice> getNoticeNewestList(){
return null;
}
관리자를 위한 Controller생성
package com.newlec.web.controller.admin.notice;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.newlec.web.entity.NoticeView;
import com.newlec.web.service.NoticeService;
@WebServlet("/admin/notice/list")
public class ListController extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String field_ = request.getParameter("f"); //검색목록
String query_ = request.getParameter("q"); //검색어
String page_ = request.getParameter("p"); //현재페이지번호
String field = "title";
if(field_ != null && !field_.equals("") )
field = field_;
String query ="";
if(query_ != null && !query_.equals(""))
query = query_;
int page = 1;
if(page_ != null && !page_.equals(""))
page = Integer.parseInt(page_);
NoticeService service = new NoticeService();
List<NoticeView> list = service.getNoticeList(field,query,page);
int count = service.getNoticeCount(field,query);
request.setAttribute("list", list);
request.setAttribute("count", count);
request.getRequestDispatcher("/WEB-INF/VIEW/admin/board/notice/list.jsp").forward(request, response);
}
}
연결되는 view
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<!DOCTYPE html>
<html>
<head>
<title>코딩 전문가를 만들기 위한 온라인 강의 시스템</title>
<meta charset="UTF-8">
<title>공지사항목록</title>
<link href="/css/customer/layout.css" type="text/css" rel="stylesheet" />
<style>
#visual .content-container {
height: inherit;
display: flex;
align-items: center;
background: url("/images/mypage/visual.png") no-repeat center;
}
</style>
</head>
<body>
<!-- header 부분 -->
<header id="header">
<div class="content-container">
<!-- ---------------------------<header>--------------------------------------- -->
<h1 id="logo">
<a href="/index.html"> <img src="/images/logo.png" alt="뉴렉처 온라인" />
</a>
</h1>
<section>
<h1 class="hidden">헤더</h1>
<nav id="main-menu">
<h1>메인메뉴</h1>
<ul>
<li><a href="/guide">학습가이드</a></li>
<li><a href="/course">강좌선택</a></li>
<li><a href="/answeris/index">AnswerIs</a></li>
</ul>
</nav>
<div class="sub-menu">
<section id="search-form">
<h1>강좌검색 폼</h1>
<form action="/course">
<fieldset>
<legend>과정검색필드</legend>
<label>과정검색</label> <input type="text" name="q" value="" /> <input
type="submit" value="검색" />
</fieldset>
</form>
</section>
<nav id="acount-menu">
<h1 class="hidden">회원메뉴</h1>
<ul>
<li><a href="/index.html">HOME</a></li>
<li><a href="/member/login.html">로그인</a></li>
<li><a href="/member/agree.html">회원가입</a></li>
</ul>
</nav>
<nav id="member-menu" class="linear-layout">
<h1 class="hidden">고객메뉴</h1>
<ul class="linear-layout">
<li><a href="/member/home"><img
src="/images/txt-mypage.png" alt="마이페이지" /></a></li>
<li><a href="/notice/list.html"><img
src="/images/txt-customer.png" alt="고객센터" /></a></li>
</ul>
</nav>
</div>
</section>
</div>
</header>
<!-- --------------------------- <visual> --------------------------------------- -->
<!-- visual 부분 -->
<div id="visual">
<div class="content-container"></div>
</div>
<!-- --------------------------- <body> --------------------------------------- -->
<div id="body">
<div class="content-container clearfix">
<!-- --------------------------- aside --------------------------------------- -->
<!-- aside 부분 -->
<aside class="aside">
<h1>ADMIN PAGE</h1>
<nav class="menu text-menu first margin-top">
<h1>마이페이지</h1>
<ul>
<li><a href="/admin/index.html">관리자홈</a></li>
<li><a href="/teacher/index.html">선생님페이지</a></li>
<li><a href="/student/index.html">수강생페이지</a></li>
</ul>
</nav>
<nav class="menu text-menu">
<h1>알림관리</h1>
<ul>
<li><a href="/admin/board/notice/list.html">공지사항</a></li>
</ul>
</nav>
</aside>
<!-- --------------------------- main --------------------------------------- -->
<main class="main">
<h2 class="main title">공지사항</h2>
<div class="breadcrumb">
<h3 class="hidden">경로</h3>
<ul>
<li>home</li>
<li>고객센터</li>
<li>공지사항</li>
</ul>
</div>
<div class="search-form margin-top first align-right">
<h3 class="hidden">공지사항 검색폼</h3>
<form class="table-form">
<fieldset>
<legend class="hidden">공지사항 검색 필드</legend>
<label class="hidden">검색분류</label> <select name="f">
<option value="title">제목</option>
<option value="writerId">작성자</option>
</select> <label class="hidden">검색어</label> <input type="text" name="q"
value="" /> <input class="btn btn-search" type="submit"
value="검색" />
</fieldset>
</form>
</div>
<div class="notice margin-top">
<h3 class="hidden">공지사항 목록</h3>
<table class="table">
<thead>
<tr>
<th class="w60">번호</th>
<th class="expand">제목</th>
<th class="w100">작성자</th>
<th class="w100">작성일</th>
<th class="w60">조회수</th>
<th class="w40">공개</th>
<th class="w40">삭제</th>
</tr>
</thead>
<tbody>
<c:forEach var="n" items="${list}">
<tr>
<td>${n.id}</td>
<td class="title indent text-align-left"><a
href="detail?id=${n.id}">${n.title}></a><span>${n.cmtCount}</span></td>
<td>${n.writerId}</td>
<td><fmt:formatDate pattern="yyyy-MM-dd hh:mm:ss"
value="${n.regdate}" /></td>
<td>${n.hit}</td>
<td><input type="checkbox" name="open"></td>
<td><input type="checkbox" name="del"></td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
<c:set var="page" value="${(empty param.p)?1:param.p}"></c:set>
<c:set var="startNum" value="${page -(page-1)%5}"></c:set>
<c:set var="lastNum" value="${fn:substringBefore(Math.ceil(count/10),'.')}"></c:set>
<div class="indexer margin-top align-right">
<h3 class="hidden">현재 페이지</h3>
<div>
<span class="text-orange text-strong">${(empty param.p)?1:param.p}</span>
/ ${lastNum} pages
</div>
</div>
<div class="text-align-right margin-top">
<input type="submit" class="btn-text btn-default" value="일괄공개">
<input type="submit" class="btn-text btn-default" value="일괄삭제">
<a class="btn-text btn-default" href="reg.html">글쓰기</a>
</div>
<div class="margin-top align-center pager">
<div>
<c:if test="${startNum>1}">
<a href="?p=${startNum-1}&f=&q=" class="btn btn-prev">이전</a>
</c:if>
<c:if test="${startNum<=1}">
<span class="btn btn-prev" onclick="alert('이전 페이지가 없습니다.');">이전</span>
</c:if>
</div>
<ul class="-list- center">
<c:forEach var="i" begin="0" end="4">
<c:if test="${(startNum+i) <=lastNum)}">
<li><a
class="-text- ${(page==(startNum+i))?'orange':''} bold"
href="?p=${startNum+i}&f=${param.f}&q=${param.q}">${startNum+i}</a></li>
</c:if>
</c:forEach>
</ul>
<div>
<c:if test="${startNum+4 < lastNum}">
<a href="?p=${startNum+4}&f=&q=" class="btn btn-next">다음</a>
</c:if>
<c:if test="${startNum+4 >= lastNum}">
<span class="btn btn-next" onclick="alert('다음 페이지가 없습니다.');">다음</span>
</c:if>
</div>
</div>
</main>
</div>
</div>
<!-- ------------------- <footer> --------------------------------------- -->
<footer id="footer">
<div class="content-container">
<h2 id="footer-logo">
<img src="/images/logo-footer.png" alt="회사정보">
</h2>
<div id="company-info">
<dl>
<dt>주소:</dt>
<dd>서울특별시</dd>
<dt>관리자메일:</dt>
<dd>admin@newlecture.com</dd>
</dl>
<dl>
<dt>사업자 등록번호:</dt>
<dd>111-11-11111</dd>
<dt>통신 판매업:</dt>
<dd>신고제 1111 호</dd>
</dl>
<dl>
<dt>상호:</dt>
<dd>뉴렉처</dd>
<dt>대표:</dt>
<dd>홍길동</dd>
<dt>전화번호:</dt>
<dd>111-1111-1111</dd>
</dl>
<div id="copyright" class="margin-top">Copyright ⓒ
newlecture.com 2012-2014 All Right Reserved. Contact
admin@newlecture.com for more information</div>
</div>
</div>
</footer>
</body>
</html>
다중 선택 값 post 하기
체크박스를 선택했하고 submit버튼을 눌렀을 때 id를 전달하도록 한다.
<form action="list" method="post">
<div class="notice margin-top">
<h3 class="hidden">공지사항 목록</h3>
<table class="table">
<thead>
<tr>
<th class="w60">번호</th>
<th class="expand">제목</th>
<th class="w100">작성자</th>
<th class="w100">작성일</th>
<th class="w60">조회수</th>
<th class="w40">공개</th>
<th class="w40">삭제</th>
</tr>
</thead>
<tbody>
<c:forEach var="n" items="${list}">
<tr>
<td>${n.id}</td>
<td class="title indent text-align-left"><a
href="detail?id=${n.id}">${n.title}></a><span>${n.cmtCount}</span></td>
<td>${n.writerId}</td>
<td><fmt:formatDate pattern="yyyy-MM-dd hh:mm:ss"
value="${n.regdate}" /></td>
<td>${n.hit}</td>
<td><input type="checkbox" name="open-id" value="${n.id}"></td>
<td><input type="checkbox" name="del-id" value="${n.id}"></td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
<c:set var="page" value="${(empty param.p)?1:param.p}"></c:set>
<c:set var="startNum" value="${page -(page-1)%5}"></c:set>
<c:set var="lastNum"
value="${fn:substringBefore(Math.ceil(count/10),'.')}"></c:set>
<div class="indexer margin-top align-right">
<h3 class="hidden">현재 페이지</h3>
<div>
<span class="text-orange text-strong">${(empty param.p)?1:param.p}</span>
/ ${lastNum} pages
</div>
</div>
<div class="text-align-right margin-top">
<input type="submit" class="btn-text btn-default" value="일괄공개">
<input type="submit" class="btn-text btn-default" value="일괄삭제">
<a class="btn-text btn-default" href="reg.html">글쓰기</a>
</div>
</form>
이렇게 설정을 하면 같은 키 open-id, del-id로 다른 값들이 오는데
배열로 그 값들을 받습니다.
String[] openIds = request.getParameterValues("open-id");
String[] delIds = request.getParameterValues("del-id");
이제 submit이 다중이므로 구분할 필요가 있습니다.
input 태그에 name을 cmd로 동일하게 준 뒤 value값을 컨트롤러에서 받아 서비스 함수에게 id값을 넘겨 처리하도록 합니다.
<input type="submit" class="btn-text btn-default" name="cmd" value="일괄공개">
<input type="submit" class="btn-text btn-default" name="cmd" value="일괄삭제">
String cmd = request.getParameter("cmd");
switch(cmd) {
case "일괄공개":
break;
case "일괄삭제":
NoticeService service = new NoticeService();
int []ids = new int[delIds.length];
for(int i =0; i<delIds.length; i++)
ids[i]= Integer.parseInt(delIds[i]);
int result = service.deleteNoticeAll(ids);
break;
}
response.sendRedirect("list");
post요청에서 get요청을 해 공지사항 목록을 보여줍니다.
그때 dopost메서드에서 response.sendRedirect("list");
재요청합니다.
deleteNoticeAll();이라는 메서드를 정의합니다.
public int deleteNoticeAll(int[] ids) {
int result =0;
String params = "";
for(int i =0 ; i <ids.length; i++) {
params += ids[i];
if(i < ids.length-1)
params +=",";
}
String sql = "DELETE NOTICE WHERE ID IN("+params+")";
String url = "jdbc:oracle:thin:@192.168.0.79:1521/xepdb1";
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection(url, "lec", "111");
Statement st =con.createStatement();
result = st.executeUpdate(sql);
st.close();
con.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}
쿼리문에 해당되는 id를 삽입할 때 where IN(1,2,3,4)로 할 것인데 prestatement를 사용할 수 없습니다.
따라서 넘겨받은 정수형 배열을 문자로 반복문을 돌아 변환한 뒤 쿼리문에 넣습니다.
반응형
'웹 프로그래밍 기초 > 자바기반의 웹&앱 응용SW 개발자' 카테고리의 다른 글
자바기반의 웹&앱 응용 SW개발자 양성과정 49일차 -77 (0) | 2020.05.04 |
---|---|
자바기반의 웹&앱 응용 SW개발자 양성과정 49일차 -76 (0) | 2020.05.04 |
자바기반의 웹&앱 응용 SW개발자 양성과정 48일차 -74 (0) | 2020.05.03 |
자바기반의 웹&앱 응용 SW개발자 양성과정 48일차 -73 (0) | 2020.05.03 |
자바기반의 웹&앱 응용 SW개발자 양성과정 47일차 -71 (0) | 2020.05.02 |
댓글