본문 바로가기
개발/Spring

[SpringBoot+IntelliJ+Oracle+Thymeleaf+Paging] 웹 게시판 만들기(2) - Controller

by 코딩하는 흰둥이 2023. 4. 20.

이전글

https://greed-yb.tistory.com/209

 

[SpringBoot+IntelliJ+Oracle+Thymeleaf+Paging] 웹 게시판 만들기(1) - 구성

Java 17 Maven Spring Boot 3.0.3 Oracle 11g IntelliJ Ultimate DBeaver 간단한 CRUD 만 구현해 놓았으며 순서대로 만들면서 올리려고 하였으나 시행착오가 생각보다 많이 생겨서 다 만들고서 완성본을 올린다 메서

greed-yb.tistory.com

 

 

Controller
package com.example.practice.controller;

import com.example.practice.service.board.BoardService;
import com.example.practice.util.PageMaker;
import com.example.practice.util.SearchCriteria;
import com.example.practice.vo.BoardVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
public class MainController {

    @Autowired
    private BoardService boardService;

    /**
     * 게시글 리스트
     * @param model
     * @param cri
     * @return
     * @throws Exception
     */
    @GetMapping("/")
    public String Main(Model model,@ModelAttribute SearchCriteria cri) throws Exception{
        model.addAttribute("vo" , boardService.boardSelect(cri));
        PageMaker pm = new PageMaker();

        pm.setCri(cri);
        pm.setTotalCount(boardService.totalCount(cri));
        model.addAttribute("pm", pm);

        return "index";
    }

    /**
     * 게시글 작성 페이지
     * @return
     * @throws Exception
     */
    @GetMapping("/board/create")
    public String boardCreate() throws Exception{
        return "BoardCreate";
    }

    /**
     * 게시글 생성
     * @param vo
     * @return
     * @throws Exception
     */
    @PostMapping("/board/create")
    public String boardCreate(@ModelAttribute BoardVo vo) throws Exception{
        boardService.boardCreate(vo);

        return "redirect:/";
    }

    /**
     * 게시글 상세 페이지
     * @param num
     * @param model
     * @return
     * @throws Exception
     */
    @GetMapping("/board/read")
    public String boardRead(@RequestParam Integer num , Model model) throws Exception{
        BoardVo vo = boardService.boardRead(num);
        model.addAttribute("vo" , vo);
        return "BoardRead";
    }

    /**
     * 게시글 삭제
     * @param num
     * @return
     * @throws Exception
     */
    @GetMapping("/board/delete")
    public String boardDelete(@RequestParam Integer num) throws Exception{
        boardService.boardDelete(num);
        return "redirect:/";
    }

    /**
     * 게시글 수정 하기
     * @param vo
     * @return
     * @throws Exception
     */
    @PostMapping("/board/update")
    public String boardUpdate(@ModelAttribute BoardVo vo) throws Exception {
        boardService.boardUpdate(vo);
        return "redirect:/";
    }

}

댓글