SMALL
안녕하세요, 코린이의 코딩 학습기 채니 입니다.
개인 포스팅용으로 내용에 오류 및 잘못된 정보가 있을 수 있습니다.
관리자모드
header.jsp
<nav>
<ul class="main-nav">
<li class="home"><a href="<%= request.getContextPath() %>">Home</a></li>
<li class="notice"><a href="#">공지사항</a></li>
<li class="board"><a href="#">게시판</a></li>
<% if(loginMember != null && loginMember.getMemberRole() == MemberRole.A) { %>
<li class="admin"><a href="<%=request.getContextPath()%>/admin/memberList">회원관리</a></li>
<% } %>
</ul>
</nav>
관리자로 로그인이 되어있는 상태에서 접속해야만 '회원관리' 메뉴가 표시 되어야 하므로, 위와 같이 if문으로 분기처리를 해주었습니다. (enum은 동일한 enum객체를 공유하므로, 주소값 비교로도 충분)
admin.css
@CHARSET "UTF-8";
/*관리자 페이지 : 회원관리 */
section#memberList-container {text-align:center;}
section#memberList-container table#tbl-member {width:100%; border:1px solid gray; border-collapse:collapse;}
table#tbl-member th, table#tbl-member td {border:1px solid gray; padding:10px; }
section#memberList-container div#neck-container{padding:0px; height: 50px; background-color:rgba(0, 188, 212, 0.3);}
section#memberList-container div#search-container {margin:0 0 10px 0; padding:3px; float:left;}
/*페이징 : 페이지당회원수*/
section#memberList-container div#numPerPage-container{float:right; margin-top: 10px;}
section#memberList-container form#numPerPageFrm{display:inline;}
/*페이징 : 페이지바 */
section#memberList-container div#pageBar{margin-top:10px; text-align:center; background-color:rgba(0, 188, 212, 0.3);}
section#memberList-container div#pageBar span.cPage{color: #0066ff; margin-right: 5px;}
section#memberList-container div#pageBar a{margin-right: 5px; text-decoration: none;}
section#memberList-container div#pageBar a:hover {text-decoration: underlin
admin이 아니라면 접근할 수 없어야 하므로, filter로 처리
filter
AdminFilter
@WebFilter("/admin/*")
public class AdminFilter implements Filter {
/**
* @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpReq = (HttpServletRequest) request;
HttpServletResponse httpRes = (HttpServletResponse) response;
HttpSession session = httpReq.getSession();
Member loginMember = (Member)session.getAttribute("loginMember");
if(loginMember == null || loginMember.getMemberRole() != MemberRole.A) {
session.setAttribute("msg", "관리자만 접근 가능합니다.");
httpRes.sendRedirect(httpReq.getContextPath() + "/");
return;
}
chain.doFilter(request, response);
}
}
Controller
AdminMemberListServlet
@WebServlet("/admin/memberList")
public class AdminMemberListServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private MemberService memberService = new MemberService();
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
// 1. 사용자입력값
// 2. 업무로직
List<Member> memberList = memberService.findAll();
// 3. view단 응답처리
request.setAttribute("memberList", memberList);
request.getRequestDispatcher("/WEB-INF/views/admin/memberList.jsp").forward(request, response);
} catch(Exception e) {
e.printStackTrace();
throw e;
}
}
}
Service
MemberService
public List<Member> findAll() {
Connection conn = getConnection();
List<Member> memberList = memberDao.findAll(conn);
close(conn);
return memberList;
}
Dao
MemberDao
// 회원 전체 조회
// findAll = select * from member order by enroll_date desc
public List<Member> findAll(Connection conn) {
PreparedStatement pstmt = null;
ResultSet rset = null;
List<Member> memberList = new ArrayList<>();
String sql = prop.getProperty("findAll");
try {
pstmt = conn.prepareStatement(sql);
rset = pstmt.executeQuery();
while(rset.next()) {
Member member = handleMemberResultSet(rset);
memberList.add(member);
}
} catch (SQLException e) {
throw new MemberException("회원 전체 조회 오류!", e);
} finally {
close(rset);
close(pstmt);
}
return memberList;
}
private Member handleMemberResultSet(ResultSet rset) throws SQLException {
String memberId = rset.getString("member_id");
String password = rset.getString("password");
String memberName = rset.getString("member_name");
MemberRole memberRole = MemberRole.valueOf(rset.getString("member_role"));
String _gender = rset.getString("gender");
Gender gender = _gender != null ? Gender.valueOf(_gender) : null;
Date birthday = rset.getDate("birthday");
String email = rset.getString("email");
String phone = rset.getString("phone");
String hobby = rset.getString("hobby");
int point = rset.getInt("point");
Timestamp enrollDate = rset.getTimestamp("enroll_date");
return new Member(memberId, password, memberName, memberRole, gender, birthday, email, phone, hobby, point, enrollDate);
}
memberList.jsp
<%@page import="com.ce.mvc2.member.model.dto.Gender"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.text.DecimalFormat"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/views/common/header.jsp" %>
<%
List<Member> memberList = (List<Member>)request.getAttribute("memberList");
%>
<!-- 관리자용 admin.css link -->
<link rel="stylesheet" href="<%=request.getContextPath()%>/css/admin.css" />
<section id="memberList-container">
<h2>회원관리</h2>
<table id="tbl-member">
<thead>
<tr>
<th>아이디</th>
<th>이름</th>
<th>회원권한</th> <%-- select 태그로 처리 --%>
<th>성별</th>
<th>생년월일</th>
<th>이메일</th>
<th>전화번호</th>
<th>포인트</th> <%-- 세자리콤마 DecimalFormat --%>
<th>취미</th>
<th>가입일</th> <%-- 날짜형식 yyyy-MM-dd --%>
</tr>
</thead>
<tbody>
<% if(memberList == null || memberList.isEmpty()) { %>
<tr>
<td colspan="10" align="center">검색 결과가 없습니다.</td>
</tr>
<% } else {
for(Member member : memberList) { %>
<tr>
<td><%= member.getMemberId() %></td>
<td><%= member.getMemberName() %></td>
<td>
<select>
<option value="A" <%= MemberRole.A == member.getMemberRole() ? "selected" : ""%>>관리자</option>
<option value="U" <%= MemberRole.U == member.getMemberRole() ? "selected" : "" %>>일반</option>
</select>
</td>
<td><%= member.getGender() == null ? "" : (member.getGender() == Gender.M) ? "남" : "여" %></td>
<td><%= member.getBirthday() != null ? member.getBirthday() : "" %></td>
<td><%= member.getEmail() != null ? member.getEmail() : "" %></td>
<td><%= member.getPhone() %></td>
<td><%= new DecimalFormat("#,###").format(member.getPoint()) %></td>
<td><%= member.getHobby() != null ? member.getHobby() : "" %></td>
<td><%= new SimpleDateFormat("yyyy-MM-dd").format(member.getEnrollDate()) %></td>
</tr>
<% }
} %>
</tbody>
</table>
</section>
<%@ include file="/WEB-INF/views/common/footer.jsp" %>
회원 정보 리스트를 잘 불러오는 것을 확인할 수 있습니다.
LIST
'Java > Servlet & JSP' 카테고리의 다른 글
JSP) 검색기능 구현하기 (0) | 2022.07.01 |
---|---|
JSP) 기본 페이징 처리 (0) | 2022.06.28 |
JSP) error page - exception 발생 예외/status 발생 예외 (0) | 2022.06.27 |
JSP) 암호화 - salt 처리, 비밀번호 변경 페이지 생성 (0) | 2022.06.26 |
JSP) Filter - 생성, 생명주기, 중복코드/접근권한 제어하기 (0) | 2022.06.26 |