본문 바로가기

2023.11.21-2024.05.31

20240227 스프링(공지 게시판-jsp만들기, detail만들기, 수정 삭제 버튼만들기, 페이징 ,배지(new))

noticeDetail> 컨트롤러부터 쭉 만들어야한다.

	//2024.02.27 noticeDetail?no=100
	@GetMapping("/noticeDetail")
	public String noticeDetail(@RequestParam(value="no", defaultValue="0", required = true) int no, Model model){
	
		if(no == 0) {
			return "rediredct:/error";
		} else {
			NoticeDTO detail = noticeService.detail(no);
			if(detail.getNno() == 0) {
				return "redirect:/error";
				
			}else {
				
				model.addAttribute("detail", detail);
				return "noticeDetail";
			}
		}
	}

 

 

notice.jsp> <tr> 사이에 넣으면 제목 줄 클릭해도 다음페이지로 넘어갈 수 있도록 하기

노티스 jsp

 

<td> 에 넣으면 한 칸 전부

 

>>여기서는  td에 넣어보자, nno 부분에 넣으면 된다.

<td class="title col-6" onclick="location.href='./noticeDetail?no=${row.nno }'">

 

 

 

noticeDetail.jsp

수정, 삭제> 버튼 만들기

<div class="col align-middle text-start">
	관리자
	<a      class="btn btn-primary" href="./noticeDel${detail.nno }">[삭제]</a>
	<button class="btn btn-primary" onclick="location.href='./noticeUpdate?no=${detail.nno}'">수정</button>
</div>

 

삭제는 a태그, 수정은 button 으로, 모양 넣어주기

 

 

 

삭제부분 삭제 누르면 글 삭제하고 넘어가게 만들기.

NoticeController>noticeDel 만들기

	//2024.02.27 noticeDel
		@GetMapping("/noticeDel{no}")
		public String noticeDel(@PathVariable("no") int no) {
			//System.out.println("@PathVariable : " + no); 값 들어오는거 확인
			//int 정상 int가 아니면 에러 난다. 
			noticeService.noticeDel(no);
			return "redirect:/notice";
		}

int일때 정상, int가 아니면 에러 난다. 

 

 

 

NoticeServic > noticeDel 있음

 

 

 

NoticeServiceImpl> noticeDel

	@Override
	public int noticeDel(int no) {
		return noticeDAO.noticeDel(no);
	}

 

 

 

NoticeDAO > noticeDel

	public int noticeDel(int no) {
		return sqlSession.update("notice.noticeDel", no);
	}

 

 

 

 

 

글 추가하는 쿼리문> 데이터 베이스에 엄청 넣으면 된다.

INSERT INTO notice (ntitle, ncontent) VALUES
('첫 번째 글', '첫 번째 글 내용'),
('두 번째 글', '두 번째 글 내용'),
('세 번째 글', '세 번째 글 내용'),
('세 번째 글', '세 번째 글 내용'),
('세 번째 글', '세 번째 글 내용'),
('세 번째 글', '세 번째 글 내용'),
('세 번째 글', '세 번째 글 내용'),
('세 번째 글', '세 번째 글 내용'),
('세 번째 글', '세 번째 글 내용'),
('세 번째 글', '세 번째 글 내용'),

('세 번째 글', '세 번째 글 내용');

 

 

 

 

페이징 만들기( board 복사 붙여넣기 하긴 했음. 하면서 다시 정리함)

 

NoticeController > notice 수정

	// 2024.02.27 요규사항 확인 psd/ 페이징
	@GetMapping("/notice")
	public String notice(@RequestParam(value = "pageNo", required = false) String no, Model model) {

		// pageNo가 오지 않는다면
		int currentPageNo = 1;
		if (util.str2Int(no) > 0) { // 정수=1, 정수가 아니면 currentPageNo
			currentPageNo = Integer.parseInt(no);
		}

		// 2024.02.20 전체 글 수 totalRecordCount (0, 1인 것들만 가져오기)
		int totalRecordCount = noticeService.totalRecordCount();
		// System.out.println("totalRecordCount : "+totalRecordCount);

		// pagination
		PaginationInfo paginationInfo = new PaginationInfo();
		paginationInfo.setCurrentPageNo(currentPageNo); // 현재 페이지 번호
		paginationInfo.setRecordCountPerPage(10); // 한 페이지에 게시되는 게시물 수
		paginationInfo.setPageSize(10); // 페이징 리스트의 사이즈 (페이징 숫자 수)
		paginationInfo.setTotalRecordCount(totalRecordCount); // 전체 게시물 건 수

		List<NoticeDTO> list = noticeService.noticeList(paginationInfo.getFirstRecordIndex()); // boardList -> change
																								// method
		model.addAttribute("list", list);

		// 페이징 관련 정보가 있는 PaginationInfo 객체를 모델에 반드시 넣어준다.
		model.addAttribute("paginationInfo", paginationInfo);
		model.addAttribute("pageNo", currentPageNo);

		return "notice"; // jsp file name

	}

 

 

NoticeService > noticeList 있음.

매개변수에 int startPpageNo 있어야한다. 

 

NoticeService> noticeList 수정

(int startPpageNo) 변경, 리턴 값에 startPpageNo 넣어주기

	@Override
	public List<NoticeDTO> noticeList(int startPpageNo) {
		return noticeDAO.noticeList(startPpageNo);
	}

 

NoticeService>totalRecordCount 만들기

	@Override
	public int totalRecordCount() {
		return noticeDAO.totalRecordCount();
	}

 

 

 

NoticeDAO>noticeList 변경

(int startPpageNo) 추가, 리턴값에 startPpageNo 추가

	public List<NoticeDTO> noticeList(int startPpageNo) {
		return sqlSession.selectList("notice.noticeList",startPpageNo);
	}

 

 

 

NoticeDAO>totalRecordCount 추가

	public int totalRecordCount() {
		
		return sqlSession.selectOne("notice.totalRecordCount");
	}

 

 

notice-mapper>noticeList 수정

	<select id="noticeList" resultType="notice">
		SELECT nno, nread, nlike, ntitle,
		if(
			date_format(now(), '%Y-%m-%d') = date_format(ndate, '%Y-%m-%d'),
			date_format(ndate, '%h:%i'),
			date_format(ndate, '%m-%d')
		) as ndate, 
		if(date_format(now(), '%Y-%m-%d') = date_format(ndate, '%Y-%m-%d'), 1, 0) as ndel
		FROM notice
		WHERE ndel=1
		ORDER BY nno DESC
		LIMIT #{startPpageNo}, 10
	</select>

 

 

notice.jsp> 페이징 추가

 

상단 <script type="text/javascript"> 추가

<script type="text/javascript">
	

//전자정부 페이징 이동하는 스크립트
function linkPage(pageNo){
	location.href = "./notice?pageNo="+pageNo;
}	
</script>

 

 

</table> 아래 페이징 추가

 

 <!-- 페이징 -->
<div class="m-2 bg-transparent" >
	<ui:pagination paginationInfo="${paginationInfo}" type="text" jsFunction="linkPage"/>
</div>

 

 

 

new 뱃지 달기

ndel 뒤에 img 태그 달아주기

<c:if test="${row.ndel eq 1}"><img alt="new" src="./img/new.png"></c:if>

 

>>오늘 올린 글에만 new가 붙을 수 있게 해주기