JX405기_비트/Spring Framework

Day45-5 Spring Framework 게시판 만들기 글 수정/삭제 하기

_하루살이_ 2023. 3. 15. 23:00

showOne.jsp 수정하기

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<c:if test="${logInId eq result.writerId}">
    <a href="/board/update/${result.id}">수정하기</a>
    <a href="/board/delete/${result.id}">삭제하기</a>
</c:if>

JSTL 추가하고 if문 사용해서 로그인한 사람과 작성한 사람의 아이디가 일치하는지 여부에 따라

  • 일치하면 수정삭제 버튼 뜸
  • 불일치하면 아무것도 뜨지 않음

 

 

추가된 showOne.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
글번호 : ${result.id} <br>
제목 : ${result.title} <br>
작성자 : ${result.writerId} <br>
작성일 : ${result.entryDate} <br>
수정일 : ${result.modifyDate} <br>
내용 ${result.content} <br>
<c:if test="${logInId eq result.writerId}">
    <a href="/board/update/${result.id}">수정하기</a>
    <a href="/board/delete/${result.id}">삭제하기</a>
</c:if>
</body>
</html>

 

 

BoardController.java에 @GetMapping 추가하기 

  • 로그인여부 확인하기
  • 게시판 글이 존재하는지 여부와 작성자ID와 로그인한 사람ID 일치여부 확인
  • 모든 if 문 통과한다면 BoardDTO값을 model에 추가하고 /board/update인 jsp 파일로 이동
@GetMapping("update/{id}")
    public String showUpdate(HttpSession session, RedirectAttributes redirectAttributes, Model model, @PathVariable int id){
        UserDTO logIn = (UserDTO) session.getAttribute("logIn");
        if (logIn == null){
            redirectAttributes.addFlashAttribute("message", "다시 로그인해주세요.");
            return "redirect:/";
        }

        BoardDTO b = boardService.selectOne(id);
        if (b == null || b.getWriterId() != logIn.getId()){
            redirectAttributes.addFlashAttribute("message", "유효하지 않은 접근입니다.");
            return "redirect:/board/showAll/1";
        }

        model.addAttribute("result", b);

        return "/board/update";
    }

 

 

추가된 BoardController.java

package com.bit.spring.controller;

import com.bit.spring.model.BoardDTO;
import com.bit.spring.model.UserDTO;
import com.bit.spring.service.BoardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.time.chrono.MinguoDate;
import java.util.ArrayList;

@Controller
@RequestMapping("/board/")
public class BoardController {
    BoardService boardService;
    @Autowired
    public BoardController(BoardService boardService){
        this.boardService = boardService;
    }
    @GetMapping("showAll/{pageNo}")
    public String showAll(HttpSession session, RedirectAttributes redirectAttributes, Model model, @PathVariable int pageNo){
        if (session.getAttribute("logIn") == null){
            redirectAttributes.addFlashAttribute("message", "다시 로그인해주세요.");
            return "redirect:/";
        }

        ArrayList<BoardDTO> list = boardService.selectAll(pageNo);

        model.addAttribute("list", list);

        return "/board/showAll";
    }

    @GetMapping("showOne/{id}")
    public String showOne(HttpSession session, RedirectAttributes redirectAttributes, Model model, @PathVariable int id){
        if (session.getAttribute("logIn") == null){
            redirectAttributes.addFlashAttribute("message", "다시 로그인해주세요.");
            return "redirect:/";
        }

        BoardDTO b = boardService.selectOne(id);
        if (b == null){
            redirectAttributes.addFlashAttribute("message", "존재하지 않는 글 번호입니다.");
            return "redirect:/board/showAll/1";
        }

        model.addAttribute("result", b);
        int logInId = ((UserDTO)session.getAttribute("logIn")).getId();
        model.addAttribute("logInId", logInId);

        return "/board/showOne";
    }

    @GetMapping("update/{id}")
    public String showUpdate(HttpSession session, RedirectAttributes redirectAttributes, Model model, @PathVariable int id){
        UserDTO logIn = (UserDTO) session.getAttribute("logIn");
        if (logIn == null){
            redirectAttributes.addFlashAttribute("message", "다시 로그인해주세요.");
            return "redirect:/";
        }

        BoardDTO b = boardService.selectOne(id);
        if (b == null || b.getWriterId() != logIn.getId()){
            redirectAttributes.addFlashAttribute("message", "유효하지 않은 접근입니다.");
            return "redirect:/board/showAll/1";
        }

        model.addAttribute("result", b);

        return "/board/update";
    }
    
}

 

 

 

 

update.jsp 추가하기

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
  <form method="post" action="/board/update">
    <input type="hidden" name="id" value="${result.id}">
    <input type="text" name="title" value="${result.title}">
    <input type="text" name="content" value="${result.content}">
    <button type="submit">수정하기</button>    
  </form>
</body>
</html>

 

 

 

 

BoardController.java에 @PostMapping 추가하기

@PostMapping("update")
public String updateBoard(HttpSession session, BoardDTO boardDTO){
    UserDTO logIn = (UserDTO) session.getAttribute("logIn");
    if (logIn == null){
        return "redirect:/";
    }

    BoardDTO origin = boardService.selectOne(boardDTO.getId());
    if (origin == null){
        return "redirect:/board/showAll/1";
    }

    origin.setTitle(boardDTO.getTitle());
    origin.setContent(boardDTO.getContent());
    boardService.update(origin);

    return "redirect:/board/showOne/"+boardDTO.getId();
}

 

 

 

 

추가된 BoardController.java

package com.bit.spring.controller;

import com.bit.spring.model.BoardDTO;
import com.bit.spring.model.UserDTO;
import com.bit.spring.service.BoardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.time.chrono.MinguoDate;
import java.util.ArrayList;

@Controller
@RequestMapping("/board/")
public class BoardController {
    BoardService boardService;
    @Autowired
    public BoardController(BoardService boardService){
        this.boardService = boardService;
    }
    @GetMapping("showAll/{pageNo}")
    public String showAll(HttpSession session, RedirectAttributes redirectAttributes, Model model, @PathVariable int pageNo){
        if (session.getAttribute("logIn") == null){
            redirectAttributes.addFlashAttribute("message", "다시 로그인해주세요.");
            return "redirect:/";
        }

        ArrayList<BoardDTO> list = boardService.selectAll(pageNo);

        model.addAttribute("list", list);

        return "/board/showAll";
    }

    @GetMapping("showOne/{id}")
    public String showOne(HttpSession session, RedirectAttributes redirectAttributes, Model model, @PathVariable int id){
        if (session.getAttribute("logIn") == null){
            redirectAttributes.addFlashAttribute("message", "다시 로그인해주세요.");
            return "redirect:/";
        }

        BoardDTO b = boardService.selectOne(id);
        if (b == null){
            redirectAttributes.addFlashAttribute("message", "존재하지 않는 글 번호입니다.");
            return "redirect:/board/showAll/1";
        }

        model.addAttribute("result", b);
        int logInId = ((UserDTO)session.getAttribute("logIn")).getId();
        model.addAttribute("logInId", logInId);

        return "/board/showOne";
    }

    @GetMapping("update/{id}")
    public String showUpdate(HttpSession session, RedirectAttributes redirectAttributes, Model model, @PathVariable int id){
        UserDTO logIn = (UserDTO) session.getAttribute("logIn");
        if (logIn == null){
            redirectAttributes.addFlashAttribute("message", "다시 로그인해주세요.");
            return "redirect:/";
        }

        BoardDTO b = boardService.selectOne(id);
        if (b == null || b.getWriterId() != logIn.getId()){
            redirectAttributes.addFlashAttribute("message", "유효하지 않은 접근입니다.");
            return "redirect:/board/showAll/1";
        }

        model.addAttribute("result", b);

        return "/board/update";
    }

    @PostMapping("update")
    public String updateBoard(HttpSession session, BoardDTO boardDTO){
        UserDTO logIn = (UserDTO) session.getAttribute("logIn");
        if (logIn == null){
            return "redirect:/";
        }

        BoardDTO origin = boardService.selectOne(boardDTO.getId());
        if (origin == null){
            return "redirect:/board/showAll/1";
        }

        origin.setTitle(boardDTO.getTitle());
        origin.setContent(boardDTO.getContent());
        boardService.update(origin);

        return "redirect:/board/showOne/"+boardDTO.getId();
    }
}

 

 

 

BoardService.java에 update 추가하기

public void update(BoardDTO boardDTO){
    String query = "UPDATE `board` SET `title` = ?, `content` = ? , `modify_date` = NOW() WHERE `id` = ?";
    try {
        PreparedStatement preparedStatement = connection.prepareStatement(query);
        preparedStatement.setString(1, boardDTO.getTitle());
        preparedStatement.setString(2, boardDTO.getContent());
        preparedStatement.setInt(3, boardDTO.getId());

        preparedStatement.executeUpdate();
        preparedStatement.close();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

 

 

 

 

 

BoardService에 delete추가하기

public void delete(int id){
    String query = "DELETE FROM `board` WHERE `id` = ?";
    try {
        PreparedStatement preparedStatement = connection.prepareStatement(query);
        preparedStatement.setInt(1, id);

        preparedStatement.executeUpdate();
        preparedStatement.close();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

 

 

 

 

추가된 BoardService.java

package com.bit.spring.controller;

import com.bit.spring.model.BoardDTO;
import com.bit.spring.model.UserDTO;
import com.bit.spring.service.BoardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.time.chrono.MinguoDate;
import java.util.ArrayList;

@Controller
@RequestMapping("/board/")
public class BoardController {
    BoardService boardService;
    @Autowired
    public BoardController(BoardService boardService){
        this.boardService = boardService;
    }
    @GetMapping("showAll/{pageNo}")
    public String showAll(HttpSession session, RedirectAttributes redirectAttributes, Model model, @PathVariable int pageNo){
        if (session.getAttribute("logIn") == null){
            redirectAttributes.addFlashAttribute("message", "다시 로그인해주세요.");
            return "redirect:/";
        }

        ArrayList<BoardDTO> list = boardService.selectAll(pageNo);

        model.addAttribute("list", list);

        return "/board/showAll";
    }

    @GetMapping("showOne/{id}")
    public String showOne(HttpSession session, RedirectAttributes redirectAttributes, Model model, @PathVariable int id){
        if (session.getAttribute("logIn") == null){
            redirectAttributes.addFlashAttribute("message", "다시 로그인해주세요.");
            return "redirect:/";
        }

        BoardDTO b = boardService.selectOne(id);
        if (b == null){
            redirectAttributes.addFlashAttribute("message", "존재하지 않는 글 번호입니다.");
            return "redirect:/board/showAll/1";
        }

        model.addAttribute("result", b);
        int logInId = ((UserDTO)session.getAttribute("logIn")).getId();
        model.addAttribute("logInId", logInId);

        return "/board/showOne";
    }

    @GetMapping("update/{id}")
    public String showUpdate(HttpSession session, RedirectAttributes redirectAttributes, Model model, @PathVariable int id){
        UserDTO logIn = (UserDTO) session.getAttribute("logIn");
        if (logIn == null){
            redirectAttributes.addFlashAttribute("message", "다시 로그인해주세요.");
            return "redirect:/";
        }

        BoardDTO b = boardService.selectOne(id);
        if (b == null || b.getWriterId() != logIn.getId()){
            redirectAttributes.addFlashAttribute("message", "유효하지 않은 접근입니다.");
            return "redirect:/board/showAll/1";
        }

        model.addAttribute("result", b);

        return "/board/update";
    }

    @PostMapping("update")
    public String updateBoard(HttpSession session, BoardDTO boardDTO){
        UserDTO logIn = (UserDTO) session.getAttribute("logIn");
        if (logIn == null){
            return "redirect:/";
        }

        BoardDTO origin = boardService.selectOne(boardDTO.getId());
        if (origin == null){
            return "redirect:/board/showAll/1";
        }

        origin.setTitle(boardDTO.getTitle());
        origin.setContent(boardDTO.getContent());
        boardService.update(origin);

        return "redirect:/board/showOne/" + boardDTO.getId();
    }

    @GetMapping("delete/{id}")
    public String delete(HttpSession session, RedirectAttributes redirectAttributes, @PathVariable int id){
        UserDTO logIn = (UserDTO) session.getAttribute("logIn");
        if (logIn == null){
            redirectAttributes.addFlashAttribute("message", "다시 로그인해주세요.");
            return "redirect:/";
        }

        BoardDTO b = boardService.selectOne(id);
        if (b == null || b.getWriterId() != logIn.getId()){
            redirectAttributes.addFlashAttribute("message", "유효하지 않은 접근입니다.");
            return "redirect:/board/showAll/1";
        }

        boardService.delete(id);
        return  "redirect:/board/showAll/1";
    }
}

 

 

 

BoardController.java에 delete 추가하기

@GetMapping("delete/{id}")
public String delete(HttpSession session, RedirectAttributes redirectAttributes, @PathVariable int id){
    UserDTO logIn = (UserDTO) session.getAttribute("logIn");
    if (logIn == null){
        redirectAttributes.addFlashAttribute("message", "다시 로그인해주세요.");
        return "redirect:/";
    }

    BoardDTO b = boardService.selectOne(id);
    if (b == null || b.getWriterId() != logIn.getId()){
        redirectAttributes.addFlashAttribute("message", "유효하지 않은 접근입니다.");
        return "redirect:/board/showAll/1";
    }

    boardService.delete(id);
    return  "redirect:/board/showAll/1";
}

 

 

 

추가된 BoardController.java

package com.bit.spring.controller;

import com.bit.spring.model.BoardDTO;
import com.bit.spring.model.UserDTO;
import com.bit.spring.service.BoardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.time.chrono.MinguoDate;
import java.util.ArrayList;

@Controller
@RequestMapping("/board/")
public class BoardController {
    BoardService boardService;
    @Autowired
    public BoardController(BoardService boardService){
        this.boardService = boardService;
    }
    @GetMapping("showAll/{pageNo}")
    public String showAll(HttpSession session, RedirectAttributes redirectAttributes, Model model, @PathVariable int pageNo){
        if (session.getAttribute("logIn") == null){
            redirectAttributes.addFlashAttribute("message", "다시 로그인해주세요.");
            return "redirect:/";
        }

        ArrayList<BoardDTO> list = boardService.selectAll(pageNo);

        model.addAttribute("list", list);

        return "/board/showAll";
    }

    @GetMapping("showOne/{id}")
    public String showOne(HttpSession session, RedirectAttributes redirectAttributes, Model model, @PathVariable int id){
        if (session.getAttribute("logIn") == null){
            redirectAttributes.addFlashAttribute("message", "다시 로그인해주세요.");
            return "redirect:/";
        }

        BoardDTO b = boardService.selectOne(id);
        if (b == null){
            redirectAttributes.addFlashAttribute("message", "존재하지 않는 글 번호입니다.");
            return "redirect:/board/showAll/1";
        }

        model.addAttribute("result", b);
        int logInId = ((UserDTO)session.getAttribute("logIn")).getId();
        model.addAttribute("logInId", logInId);

        return "/board/showOne";
    }

    @GetMapping("update/{id}")
    public String showUpdate(HttpSession session, RedirectAttributes redirectAttributes, Model model, @PathVariable int id){
        UserDTO logIn = (UserDTO) session.getAttribute("logIn");
        if (logIn == null){
            redirectAttributes.addFlashAttribute("message", "다시 로그인해주세요.");
            return "redirect:/";
        }

        BoardDTO b = boardService.selectOne(id);
        if (b == null || b.getWriterId() != logIn.getId()){
            redirectAttributes.addFlashAttribute("message", "유효하지 않은 접근입니다.");
            return "redirect:/board/showAll/1";
        }

        model.addAttribute("result", b);

        return "/board/update";
    }

    @PostMapping("update")
    public String updateBoard(HttpSession session, BoardDTO boardDTO){
        UserDTO logIn = (UserDTO) session.getAttribute("logIn");
        if (logIn == null){
            return "redirect:/";
        }

        BoardDTO origin = boardService.selectOne(boardDTO.getId());
        if (origin == null){
            return "redirect:/board/showAll/1";
        }

        origin.setTitle(boardDTO.getTitle());
        origin.setContent(boardDTO.getContent());
        boardService.update(origin);

        return "redirect:/board/showOne/" + boardDTO.getId();
    }

    @GetMapping("delete/{id}")
    public String delete(HttpSession session, RedirectAttributes redirectAttributes, @PathVariable int id){
        UserDTO logIn = (UserDTO) session.getAttribute("logIn");
        if (logIn == null){
            redirectAttributes.addFlashAttribute("message", "다시 로그인해주세요.");
            return "redirect:/";
        }

        BoardDTO b = boardService.selectOne(id);
        if (b == null || b.getWriterId() != logIn.getId()){
            redirectAttributes.addFlashAttribute("message", "유효하지 않은 접근입니다.");
            return "redirect:/board/showAll/1";
        }

        boardService.delete(id);
        return  "redirect:/board/showAll/1";
    }
}