무회blog

spring MVC(모델2)방식,JAVA, MySQL , RESFUL, 게시판 만들기 , 002.01 (컨트롤러) 본문

카테고리 없음

spring MVC(모델2)방식,JAVA, MySQL , RESFUL, 게시판 만들기 , 002.01 (컨트롤러)

최무회 2020. 8. 24. 08:51

컨트롤러.002.01

com.board.controller

BoardController.java

 

package com.board.controller;
 
import java.util.List;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
 
import com.board.domain.BoardVO;
import com.board.mapper.BoardMapper;
 
@Controller
@RequestMapping("/board")
public class BoardController {
 
    @Autowired
    private BoardMapper boardMapper;
    
    
    //게시글 목록
    @RequestMapping(method=RequestMethod.GET)
    public ModelAndView list() throws Exception{
        
        List<BoardVO> list = boardMapper.boardList();
        
        return new ModelAndView("boardList","list",list);
    }
    
    //게시글 작성 페이지(GET)
    @RequestMapping(value="/post",method=RequestMethod.GET)
    public ModelAndView writeForm() throws Exception{
        
        return new ModelAndView("boardWrite");
    }
    
    //게시글 작성(POST)
    @RequestMapping(value="/post",method=RequestMethod.POST)
    public String write(@ModelAttribute("BoardVO") BoardVO board) throws Exception{
 
        boardMapper.boardInsert(board);
        
        return "redirect://localhost:8080/board";
    }
    
    //게시글 상세
    @RequestMapping(value="/{bno}",method=RequestMethod.GET)
    public ModelAndView view(@PathVariable("bno") int bno) throws Exception{
        
        BoardVO board = boardMapper.boardView(bno);
        boardMapper.hitPlus(bno);
        
        return new ModelAndView("boardView","board",board);
    }
    
    //게시글 수정 페이지(GET)
    @RequestMapping(value="/post/{bno}", method=RequestMethod.GET)
    public ModelAndView updateForm(@PathVariable("bno") int bno) throws Exception{
        
        BoardVO board = boardMapper.boardView(bno);
        
        return new ModelAndView("boardUpdate","board",board);
    }
    
    //게시글 수정(PATCH)
    @RequestMapping(value="/post/{bno}", method=RequestMethod.PATCH)
    public String update(@ModelAttribute("BoardVO")BoardVO board,@PathVariable("bno") int bno) throws Exception{
        
        boardMapper.boardUpdate(board);
        
        return "redirect://localhost:8080/board/"+bno;
    }
    
    //게시글 삭제(DELETE)
    @RequestMapping(value="/post/{bno}", method=RequestMethod.DELETE)
    public String delete(@PathVariable("bno") int bno) throws Exception{
        
        boardMapper.boardDelete(bno);
        
        return "redirect://localhost:8080/board";
    }
}
 


 

 

https://private.tistory.com/38?category=655784

 

스프링 부트 게시판 만들기4 - Rest방식의 게시글 목록/작성/수정/삭제

관련글 : 스프링 부트로 게시판 만들기1 - 프로젝트 생성 스프링 부트로 게시판 만들기2 - MyBatis, MySQL 연동 스프링 부트로 게시판 만들기3 - JSP 사용하기 사실 포스팅은 매일하는데 어제는 작성 완

private.tistory.com

 

Comments