BoardController

 

package com.ohj.controller;

import java.util.List;

import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model; // 스프링은 모델이다.(모델 앤 뷰 객체)
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.ohj.domain.BoardVO;
import com.ohj.service.BoardService;

import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j;

@Controller
@Log4j
@RequestMapping("/board/*")
@AllArgsConstructor
public class BoardController {
   private BoardService service; 
   
   @GetMapping("/list")
   public void list(Model model) {
      log.info("list");
      model.addAttribute("list", service.getList()); // 반환타입 List<BoardVO> --> list.jsp
   }// list() END
   // 이 리스트 메소드가 호출되려면 /board/list 로 되어야 한다.
    // ModelAndView 에 값 실어주려면 setAttribute 인데 여기선 addAttribute 라는 메소드로 리스트란 명칭을 실어주는 것이다.
   
   @GetMapping("/register")
   public void register() {
      
   }//register() END 
   
   @PostMapping("/register")
   public String register(BoardVO board, RedirectAttributes rttr) {
      
      log.info("register:" + board);
      service.register(board);
      rttr.addFlashAttribute("result", board.getBno()); // 딱 1회만 전송할 수 있도록 만들어주는 메서드(addFlashAttribute)
      
      return "redirect:/board/list";
   }//register() END
   
   @GetMapping({"/get","/modify"}) //매개변수에서 url패턴명이 바로 model객체 내부로 들어가서 return 되는 것(모델 객체가 리턴)
   public void get(@RequestParam("bno") Long bno, Model model) { // @RequestParam("bno") 이 부분은 board/get?bno=n 과 같이 데이터 전달을 알려준다.(자동주입)
      //request.getParameter("bno")를 대신해주는 것이 @RequestParam("bno")
      
      log.info("/get or modify");
      model.addAttribute("board",service.get(bno));  //BoardVO 반환타입
   }//get() END
   
   @PostMapping("/modify")
   public String modify(BoardVO board, RedirectAttributes rttr) {
      log.info("modify : " + board);
      
      if (service.modify(board)) {
         rttr.addFlashAttribute("result", "success");
      }
      return "redirect:/board/list";
   }// modify() END
   
   //★삭제는 반드시 POST 방식으로 처리
   @PostMapping("/remove")
   public String remove(@RequestParam("bno") Long bno, RedirectAttributes rttr) {
      log.info("remove...." + bno);
      if(service.remove(bno)) {
         rttr.addFlashAttribute("result","success");
         
      }
      return "redirect:/board/list";
   }//remove() END
   
}//BoardController CLASS END

 

get.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri = "http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@include file="../includes/header.jsp" %>

<div class="row">
	<div class="col-lg-12">
		<h1 class="page-header">Board Read</h1>
	</div>
	<!-- /.col-lg-12 -->
</div>

<div class="row">
	<div class="col-lg-12">
		<div class="panel panel-default">
			<div class="panel-heading">Board Read Page</div>
			<!-- /.panel-heading -->
			<div class="panel-body">
				
				
					<div class="form-group">
						<label>Bno</label> <input class="form-control" name="bno" value='<c:out value="${board.bno}" />' readonly="readonly">
					</div>
					
					<div class="form-group">
						<label>Title</label> <input class="form-control" name="title" value='<c:out value="${board.title}" />' readonly="readonly">
					</div>
					
					<div class="form-group">
						<label>Text Area</label>
						<textarea class="form-control" rows="3" name="content" readonly="readonly"><c:out value="${board.content }" /></textarea>
					</div>
					
					<div class="form-group">
           				<label>Writer</label><input class = "form-control" name = "writer" value='<c:out value="${board.writer}" />' readonly="readonly"> 
           			</div>
           			
					<button data-oper="modify" class="btn btn-default" onclick="location.href='/board/modify?bno=<c:out value="${board.bno }"/>'">Modify</button>		
					<button data-oper="list" class="btn btn-info" onclick="location.href='/board/list'">List</button>

				
			</div>
			<!-- end panel body -->		
		</div>
		<!-- end panel body -->
	</div>
	<!-- end panel -->
</div>
<!-- /.row -->
<%@include file="../includes/footer.jsp"%>

 

 

modify.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri = "http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@include file="../includes/header.jsp" %>

<div class="row">
   <div class="col-lg-12">
      <h1 class="page-header">Board Register</h1>
   </div>
   <!-- /.col-lg-12 -->
</div>

<div class="row">
   <div class="col-lg-12">
      <div class="panel panel-default">
         <div class="panel-heading">Board Register</div>
         <!-- /.panel-heading -->
         <div class="panel-body">
            <form role="form" action="/board/modify" method="post">
            
               <div class="form-group">
                  <label>Bno</label> <input class="form-control" name="bno" value='<c:out value="${board.bno}" />' readonly="readonly">
               </div>
               
               <div class="form-group">
                  <label>Title</label> <input class="form-control" name="title" value='<c:out value="${board.title}" />' >
               </div>
               
               <div class="form-group">
                  <label>Text Area</label>
                  <textarea class="form-control" rows="3" name="content" ><c:out value="${board.content }" /></textarea>
               </div>
               
               <div class="form-group">
                       <label>Writer</label><input class = "form-control" name = "writer" value='<c:out value="${board.writer}" />'> 
                    </div>
                    
                    <div class="form-group">
                       <label>RegDate</label>
                       <input class="form-control" name = "regDate" 
                          value='<fmt:formatDate pattern ="yyyy/MM/dd" 
                          value ="${board.regdate}" />' readonly="readonly"> 
                    </div>
                    
                    <div class="form-group">
                       <label>Update Date</label>
                       <input class="form-control" name = "updateDate" 
                          value='<fmt:formatDate pattern ="yyyy/MM/dd" 
                          value ="${board.updateDate}" />' readonly="readonly"> 
                    </div>
                    
                    
               <button type="submit" data-oper="modify" class="btn btn-default">Modify</button>
               <button type="submit" data-oper="remove" class="btn btn-danger">Remove</button>
               <button type="submit" data-oper="list" class="btn btn-info">List</button>
               
            </form>
         </div>
         <!-- end panel body -->      
      </div>
      <!-- end panel body -->
   </div>
   <!-- end panel -->
</div>
<!-- /.row -->
<%@include file="../includes/footer.jsp"%>

<!-- remove JSP code -->
<script type="text/javascript">
$(document).ready(function(){
   var formObj=$("form");
   $('button').on("click",function(e){
      e.preventDefault();
      var operation = $(this).data("oper");
      console.log(operation);
      if(operation==='remove'){
         formObj.attr("action","/board/remove");
         
      }else if(operation==='list'){
         self.location="/board/list";
         return;
      }
      formObj.submit();
         
   });
});
</script>

+ Recent posts