본문 바로가기
개발 공부 Today I Learned

[국비 48일차 TIL] 관리자 페이지 설정 member, admin

by 개발자신입 2024. 1. 26.
반응형

 

 

member.jsp

 

<%@ 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" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>관리자 페이지</title>
<link href="./css/menu.css" rel="stylesheet">
<link href="../css/admin.css" rel="stylesheet"/>
<link href="../css/member.css" rel="stylesheet"/>
<script type="text/javascript" src="../js/menu.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js" integrity="sha512-v2CJ7UaYy4JwqLDIrZUI/4hqeoQieOmAZNXBeQyjo21dadnwR+8ZaIJVT8EE2iyI61OV8e6M8PP2/4hpQINQ/g==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/xeicon@2.3.3/xeicon.min.css">
<script>
$(function(){
	$('select[name=grade]').on("change", function(){
		let val = $(this).val();
		let mno = $(this).closest("tr").children().first().text();
		
		// 위의 두 값을 잡고, 가상 form에 담아서 전송하기
		let form = $('<form></form>');
		form.attr('method', 'post');
		form.attr('action', './members');
		form.append($('<input/>', {type:'hidden', name:'mno', value:mno }));
		form.append($('<input/>', {type:'hidden', name:'grade', value:val }));
		<c:if test="${param.grade ne null}">
		form.append($('<input/>', {type: 'hidden', name: 'currentgrade', value: ${param.grade}}));
		</c:if>
		form.appendTo('body');
		form.submit();
	});
});
	

</script>


</head>
	<h1>관리자 페이지</h1>
<!-- 	<h2><button onclick="url('../board')" id="board_button" name="board_button">게시판</button></h2> -->
	<!-- 2024-01-26 -->
	<div class="wrap">
		<div class="menu">
			<nav>
				<ul>
					<li onclick="url('./')"><i class="xi-home"></i> 관리자 페이지 </li>
					<li onclick="url('./members')"><i class="xi-users"></i> 회원 관리 </li>
					<li onclick="url('./board')"><i class="xi-paper"></i> 게시글 관리 </li>
					<li onclick="url('./comments')"><i class="xi-forum-o"></i> 댓글 관리 </li>
					<li onclick="url('./info')"><i class="xi-lock-o"></i> No.15 </li>
					<li></li>
					<li></li>
					<li></li>
					<li></li>
					<li></li>
					<li></li>
					<li></li>
					<li></li>
					<li></li>
					<li></li>
					<li></li>
					<li></li>
					<li></li>
					<li></li>
					<li></li>
					<li onclick="url('../board')"><i class="xi-bars"></i> 게시판</li>
					<li onclick="url('../index')"><i class="xi-home-o"></i> 메인 페이지</li>
					
				</ul>
			</nav>
		</div>
		<div class="main">
			<article>
				<h2>회원관리</h2>
				<div class="member-lists">
           		    <ul>
						<li onclick="url('./members?grade=0')"><i class="xi-close-circle-o"></i> 탈퇴</li>
						<li onclick="url('./members?grade=1')"><i class="xi-minus-circle-o"></i> 강퇴</li>
						<li onclick="url('./members?grade=2')"><i class="xi-check-circle-o"></i> 정지</li>
						<li onclick="url('./members?grade=5')"><i class="xi-label-o"></i> 정상</li>
						<li onclick="url('./members?grade=9')"><i class="xi-plus-square-o"></i> 관리자</li>               
              		 </ul>
          		  </div>			
				
				<table>
					<thead>
					<tr>
						<th>번호</th>
						<th>아이디</th>
						<th>이름</th>
						<th>가입일</th>
						<th>등급</th>
					</tr>
					</thead>
					<tbody>
						<c:forEach items="${list }" var="row">
						<tr class="row${row.mgrade }">
							<td class="d1">${row.mno }</td>
							<td class="title">${row.mid }</td>
							<td class="d1">${row.mname }</td>
							<td class="d2">${row.mdate }</td>
							<td class="d1">
								<select name="grade">
									<optgroup label="정지">
										<option <c:if test="${row.mgrade eq 0 }">selected="selected"</c:if> value="0">0 탈퇴</option>
										<option <c:if test="${row.mgrade eq 1 }">selected="selected"</c:if> value="1">1 강퇴</option>
										<option <c:if test="${row.mgrade eq 2 }">selected="selected"</c:if> value="2">2 정지</option>
									</optgroup>
									<optgroup label="정상">
										<option <c:if test="${row.mgrade eq 5 }">selected="selected"</c:if> value="5">5 평민</option>
										<option <c:if test="${row.mgrade eq 9 }">selected="selected"</c:if> value="9">9 관리자</option>
									</optgroup>
								</select>
							${row.mgrade }
							</td>
						</tr>
						</c:forEach>
					</tbody>
				</table><br><br>
				    페이징 필요<br><br>
			</article>
		</div>
	</div>
</body>
</html>

 

 

member.css

 

@charset "UTF-8";

article{
	margin: 10px;
	padding: 10px;
	width: calc(100%-20px);
	height: calc(100vh-20px); /* 높이 단위 vh (100%) */
	overflow: scroll;
}
table{
	width: 1000px;
	height: auto;
	border-collapse: collapse;
}
th{
	background-color: #382d72;
	height: 30px;
	line-height: 30px;
	color : white;
}
tr{
	height: 30px;
	line-height: 30px;
}
tr:hover{
	background-color: white;
}
td{
	text-align: center;
}
.d1{
	width: 10%;
}
.d2{
	width: 20%;
}
.title{
	width: 20%;
	text-align: left;
}
.row0{ /* 탈퇴 : 빨간색 */ 
	background-color: #dfdfdf;
}
.row1{ /* 강퇴 : 회색 */
	background-color: rgb(252, 211, 213);
}
.row2{ /* 정지 : 주황색 */
	background-color: rgb(255, 219, 202);
}
.row9{ /* 관리자 : 녹색 */
	background-color: rgb(222, 240, 219);
}

.member-lists{
   height: 30px;
   width: 100%;
   background-color: #afc6f3;
   margin-bottom: 10px;	
}

.member-lists ul{
	margin:0;
	padding: 0;
	list-style-type: none;
	height: 30px;
	line-height: 30px;
}
.member-lists ul li{
	width: 100px;
	display: inline-block;
	text-align: center;
	color: #261a82;
	font-weight: bold;
}
.member-lists ul li:hover {
	background-color: #84d2a7;
	color: black;
	font-weight: bold;
}

 

 

member.java

 

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
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.coffee.dao.AdminDAO;
import com.coffee.dao.MemberDAO;
import com.coffee.dto.MemberDTO;
import com.coffee.util.Util;


@WebServlet("/admin/members")
public class Members extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public Members() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println(request.getParameter("grade"));
		
		AdminDAO dao = new AdminDAO();
		List<MemberDTO> list = null;
		
		// url에서 grade 매개변수가 없거나 빈 문자열이면
		if(request.getParameter("grade") == null || request.getParameter("grade").equals("")) { 
			list = dao.memberList();
		} else { // 문자열을 정수로 변환한 후
			list = dao.memberList(Util.str2Int(request.getParameter("grade")));
		}
		// 회원 목록을 request 객체에 속성으로 설정
		request.setAttribute("list", list);
        
		RequestDispatcher rd = request.getRequestDispatcher("/admin/members.jsp"); // 포워딩
		rd.forward(request, response);
	}
	
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		
        // AdminDAO 클래스로 회원 등급 업데이트함.
		AdminDAO dao = new AdminDAO();
		int result = dao.memberUpdate(Util.str2Int(request.getParameter("grade")), Util.str2Int(request.getParameter("mno")));
		System.out.println(result);
		
		// 페이지 이동
		if(request.getParameter("currentgrade") == null) {			
			response.sendRedirect("./members");
		} else {
			response.sendRedirect("./members?grade=" + request.getParameter("currentgrade"));
		}
	}
}

 

 

AdminDAO

 

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.security.auth.message.callback.PrivateKeyCallback.Request;

import com.coffee.dto.MemberDTO;

public class AdminDAO extends AbstractDAO {
	
    // 모든 회원의 정보를 가져오는 메소드
	public List<MemberDTO> memberList() {
		List<MemberDTO> list = new ArrayList<MemberDTO>();
		Connection con = db.getConnection();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "SELECT mno, mid, mname, mdate, mgrade FROM member";

		try {
			pstmt = con.prepareStatement(sql);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				MemberDTO e = new MemberDTO();
				e.setMno(rs.getInt("mno"));
				e.setMid(rs.getString("mid"));
				e.setMname(rs.getString("mname"));
				e.setMdate(rs.getString("mdate"));
				e.setMgrade(rs.getInt("mgrade"));
				list.add(e);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			close(rs, pstmt, con);
		}
		return list;
	}

	// 특정 등급 회원 정보만 가져오는 메소드 (WHERE mgrade=?)
	public List<MemberDTO> memberList(int grade) {
		List<MemberDTO> list = new ArrayList<MemberDTO>();
		Connection con = db.getConnection();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "SELECT mno, mid, mname, mdate, mgrade FROM member WHERE mgrade=?";

		try {
			pstmt = con.prepareStatement(sql);
			pstmt.setInt(1, grade);
					
			rs = pstmt.executeQuery();
			while (rs.next()) {
				MemberDTO e = new MemberDTO();
				e.setMno(rs.getInt("mno"));
				e.setMid(rs.getString("mid"));
				e.setMname(rs.getString("mname"));
				e.setMdate(rs.getString("mdate"));
				e.setMgrade(rs.getInt("mgrade"));
				list.add(e);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			close(rs, pstmt, con);
		}
		return list;
	}

	public int memberUpdate(int grade, int mno) {
		Connection con = db.getConnection();
		PreparedStatement pstmt = null;
		String sql = "UPDATE member SET mgrade=? WHERE mno=?";
		int result = 0;

		try {
			pstmt = con.prepareStatement(sql);
			pstmt.setInt(1, grade);
			pstmt.setInt(2, mno);
			result = pstmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			close(null, pstmt, con);
		}
		return result;
	}

}
반응형

댓글