[JSP/Servlet] 18. 나의 게시물개발자가 되기까지 (2023.08.16~2024.04.15)/[Servlet] Basic Web2023. 12. 12. 20:18
Table of Contents
마이페이지에서 내가 쓴 게시물을 볼 수 있는 버튼을 만들었다.
member > mypage.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>마이페이지</h1>
<form action="/memb/modify" method="post">
<table>
<!-- ...(기존 코드) -->
</table>
<button type="submit">수정</button>
<a href="/memb/remove?${ses.id }"><button type="button">회원탈퇴</button></a>
<a href="/memb/myList"><button type="button">나의 게시물</button></a>
</form>
<!-- ...(기존 코드) -->
</body>
</html>
MemberController.java
package controller;
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 javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import domain.BoardVO;
import domain.MemberVO;
import service.MemberService;
import service.MemberServiceImpl;
@WebServlet("/memb/*")
public class MemberController extends HttpServlet {
// ... (기본 코드)
case "myList":
try {
log.info(">>> mine check 1");
HttpSession ses = request.getSession();
MemberVO mvo = (MemberVO) ses.getAttribute("ses");
String writer = mvo.getId();
log.info(">>> ses mvo >>> {} " + mvo);
List<BoardVO> myList = msv.myList(writer);
log.info(">>> myList >>> {} " + myList);
request.setAttribute("myList", myList);
destPage = "/member/myList.jsp";
} catch (Exception e) {
log.info("mine error !!");
e.printStackTrace();
}
break;
}
// ... (기본 코드)
}
MemberService.interface
package service;
import java.util.List;
import domain.BoardVO;
import domain.MemberVO;
public interface MemberService {
int register(MemberVO mvo);
MemberVO login(MemberVO mvo);
int lastLogin(String id);
int modify(MemberVO updateMvo);
int remove(String id);
List<MemberVO> getList();
List<BoardVO> myList(String writer);
}
MemberServiceImpl.java
package service;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import domain.BoardVO;
import domain.MemberVO;
import repository.MemberDAO;
import repository.MemberDAOImpl;
public class MemberServiceImpl implements MemberService {
// ... (기존 코드)
@Override
public List<BoardVO> myList(String writer) {
log.info(">>> myList check 2");
return mdao.selectList(writer);
}
}
MemberDAO.interface
package repository;
import java.util.List;
import domain.BoardVO;
import domain.MemberVO;
public interface MemberDAO {
int insert(MemberVO mvo);
MemberVO login(MemberVO mvo);
int lastLogin(String id);
int update(MemberVO updateMvo);
int delete(String id);
List<MemberVO> selectList();
List<BoardVO> selectList(String writer);
}
MemeberDAOImpl.java
package repository;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import domain.BoardVO;
import domain.MemberVO;
import orm.DatabaseBuilder;
public class MemberDAOImpl implements MemberDAO {
// ... (기존 코드)
@Override
public List<BoardVO> selectList(String writer) {
log.info(">>> myList check 3");
return sql.selectList("MemberMapper.myList", writer);
}
}
memberMappler.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="MemberMapper">
<!-- ... (기존 코드) -->
<select id="myList" resultType="bvo">
select * from board where writer = #{writer}
order by bno desc
</select>
</mapper>
member > myList.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>나의 게시물</title>
</head>
<body>
<h1>나의 게시물</h1>
<hr>
<br>
<c:if test="${not empty myList }">
<table>
<tr>
<!-- nno, title, writer, regdate, readcount -->
<th>No</th>
<th>제목</th>
<th>작성자</th>
<th>등록일</th>
<th>조회수</th>
</tr>
<c:forEach items="${myList }" var="bvo">
<tr>
<!-- title 눌렀을 때 게시글 상세로 이동할 수 있게 -->
<td>${bvo.bno }</td>
<td><a href="/brd/detail?bno=${bvo.bno }">${bvo.title }</a></td>
<td>${bvo.writer }</td>
<td>${bvo.regdate }</td>
<td>${bvo.readcount }</td>
</tr>
</c:forEach>
</table>
<br>
</c:if>
<c:if test="${empty myList}">
<h4>등록된 게시글이 없습니다.</h4>
<br><a href="/brd/register"><button>글쓰기</button></a>
</c:if>
</body>
</html>
계정으로 쓴 게시물이 있을 때
계정으로 쓴 게시물이 없을 때
[JSP/Servlet] 18. 나의 게시물
(다음 게시물 예고편)
[JSP/Servlet] 19. 화면 꾸미기
728x90
@rlozlr :: 얼렁뚱땅 개발자
얼렁뚱땅 주니어 개발자
포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!