Stack/Spring

[Spring] CRUD Project 제작 2 - 글쓰기 / 읽기 / 수정

7ingout 2022. 9. 13. 16:57

이전 글

2022.09.13 - [Back-end/Spring] - [Spring] CRUD Project 제작 1

 

[Spring] CRUD Project 제작 1

https://velog.io/@emawlrdl/Spring-project-%EC%A0%9C%EC%9E%91-%EA%B3%BC%EC%A0%95-8yk5n8bogp 이클립스 상단매뉴 Help -> MarketPlace -> STS 설치.. WAS(Web Application Server) = 아파치 톰캣 was에 대한 설..

7ingout.tistory.com

 


 

1. 글쓰기 구현

* 글쓰기는 GET & POST 2중으로 구성해야 한다

 

GET : 단순 글쓰기 페이지 VIEW 

POST : 글쓰기 작업 완료 후 DB전송 및 listAll.jsp에 구현

 

views에 regist.jsp 파일 생성

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>글쓰기</title>
</head>
<form method="post">
	<body>
		<p>
			<label>제목</label><input type="text" name="title">
		</p>
		<p>
			<label>작성자</label><input type="text" name="writer" size="15">
		</p>
		<label>내용</label>
		<p>
			<textarea rows="15" cols="65" name="content"></textarea>
		<p>
			<button type="submit">등록</button>
</form>
</body>
</html>

 

controller 구현

BoardController.java에 메소드 추가

package com.myp.controller;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

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

@Controller // 컨트롤러임을 명시
@RequestMapping(value = "/") // 주소 패턴
public class BoardController {			
	
	@Inject // 주입(심부름꾼) 명시
	private BoardService service; // Service 호출을 위한 객체생성		
	
	@RequestMapping(value= "/listAll", method = RequestMethod.GET) // 주소 호출 명시 . 호출하려는 주소 와 REST 방식설정 (GET)
	public void listAll(Model model)throws Exception { // 메소드 인자값은 model 인터페이스(jsp전달 심부름꾼)				
		model.addAttribute("list",service.listAll()); // jsp에 심부름할 내역(서비스 호출)			
	}
	
	@RequestMapping(value = "/regist", method = RequestMethod.GET) // GET 방식으로 페이지 호출	
	public void registerGET(BoardVO board, Model model) throws Exception {		
		
	}	
	
	@RequestMapping(value = "/regist", method = RequestMethod.POST) // POST방식으로 내용 전송	
	public String registPOST(BoardVO board, RedirectAttributes rttr) throws Exception { // 인자값으로 REDIRECT 사용 	
		service.regist(board); // 글작성 서비스 호출
		
	return "redirect:/listAll"; // 작성이 완료된 후, 목록페이지로 리턴
	}
}

 

 

2. 글읽기 구현

read.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%><%@ page session="false"%><!DOCTYPE html>
 
<html>
<head>
<title>글읽기</title>
</head>
<form>
	<body>
		<p>
			<label>글번호</label> <input type="text" name="bno"
				value="${boardVO.bno}" readonly="readonly">
		</p>
		<p>
			<label>제목</label> <input type="text" name="title"
				style="background-color: #B0E0E6;" value="${boardVO.title}"
				readonly="readonly">
		</p>
		<p>
			<label>작성자</label> <input type="text" name="writer" size="15"
				value="${boardVO.writer}" readonly="readonly">
		<p>
			<label>내용</label>
			<textarea name=content rows="10" cols="70"
				  style="background-color: #B0E0E6;"     readonly="readonly">${boardVO.content}</textarea>
			<br>   
			<button type="submit" formaction="modify" formmethod="get">수정</button>
			<button type="submit" formaction="remove" formmethod="post">삭제</button>
			<button type="submit" formaction="listAll" formmethod="get">목록</button>
	</body>
</form>
</html>

 

BoardController.java에 메소드 추가

package com.myp.controller;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

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

@Controller // 컨트롤러임을 명시
@RequestMapping(value = "/") // 주소 패턴
public class BoardController {			
	
	@Inject // 주입(심부름꾼) 명시
	private BoardService service; // Service 호출을 위한 객체생성		
	
	@RequestMapping(value= "/listAll", method = RequestMethod.GET) // 주소 호출 명시 . 호출하려는 주소 와 REST 방식설정 (GET)
	public void listAll(Model model)throws Exception { // 메소드 인자값은 model 인터페이스(jsp전달 심부름꾼)				
		model.addAttribute("list",service.listAll()); // jsp에 심부름할 내역(서비스 호출)			
	}
	
	@RequestMapping(value = "/regist", method = RequestMethod.GET) // GET 방식으로 페이지 호출	
	public void registerGET(BoardVO board, Model model) throws Exception {		
		
	}	
	
	@RequestMapping(value = "/regist", method = RequestMethod.POST) // POST방식으로 내용 전송	
	public String registPOST(BoardVO board, RedirectAttributes rttr) throws Exception { // 인자값으로 REDIRECT 사용 	
		service.regist(board); // 글작성 서비스 호출
		
	return "redirect:/listAll"; // 작성이 완료된 후, 목록페이지로 리턴
	}
	
	@RequestMapping(value = "/read", method = RequestMethod.GET) // GET 방식으로 페이지 호출
	public void read(@RequestParam("bno")int bno, Model model) throws Exception{
		// 인자값은 파라미터 값으로 기본키인 글번호를 기준으로 Model을 사용하여 불러옴
		model.addAttribute(service.read(bno)); // read 서비스 호출
	}

}

 

3. 글수정 구현

** 글수정과 글삭제는 글읽기(READ.JSP)페이지에서 작동

** 글수정은 GET/POST 2중으로 구성

 

modify.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page session="false"%>

<!DOCTYPE>
<html>
<head>
<title>글수정</title>
</head>
<body>
글 수 정     페 이 지
<form action="modify" method = "post">
    <body>
    <p><label>글번호</label> <input type="text" name ="bno" value ="${boardVO.bno}" readonly="readonly"></p>
    <p><label>제목</label ><input type="text" name ="title" value ="${boardVO.title}" ></p>
    <p><label>작성자</label> <input type="text" name="writer" size="15" value = "${boardVO.writer}"></p>
    <label>내용</label>
    <textarea name=content rows ="10" cols="70" >${boardVO.content}</textarea><br>

    <button type ="submit">완료</button>
 	</body>

 </form>

</html>

 

BoardController.java에 메소드 추가

package com.myp.controller;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

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

@Controller // 컨트롤러임을 명시
@RequestMapping(value = "/") // 주소 패턴
public class BoardController {			
	
	@Inject // 주입(심부름꾼) 명시
	private BoardService service; // Service 호출을 위한 객체생성		
	
	@RequestMapping(value= "/listAll", method = RequestMethod.GET) // 주소 호출 명시 . 호출하려는 주소 와 REST 방식설정 (GET)
	public void listAll(Model model)throws Exception { // 메소드 인자값은 model 인터페이스(jsp전달 심부름꾼)				
		model.addAttribute("list",service.listAll()); // jsp에 심부름할 내역(서비스 호출)			
	}
	
	@RequestMapping(value = "/regist", method = RequestMethod.GET) // GET 방식으로 페이지 호출	
	public void registerGET(BoardVO board, Model model) throws Exception {		
		
	}	
	
	@RequestMapping(value = "/regist", method = RequestMethod.POST) // POST방식으로 내용 전송	
	public String registPOST(BoardVO board, RedirectAttributes rttr) throws Exception { // 인자값으로 REDIRECT 사용 	
		service.regist(board); // 글작성 서비스 호출
		
	return "redirect:/listAll"; // 작성이 완료된 후, 목록페이지로 리턴
	}
	
	@RequestMapping(value = "/read", method = RequestMethod.GET) // GET 방식으로 페이지 호출
	public void read(@RequestParam("bno")int bno, Model model) throws Exception{
		// 인자값은 파라미터 값으로 기본키인 글번호를 기준으로 Model을 사용하여 불러옴
		model.addAttribute(service.read(bno)); // read 서비스 호출
	}

	@RequestMapping(value = "/modify", method = RequestMethod.GET) // GET 방식으로 페이지 호출
	  public void modifyGET(int bno, Model model) throws Exception {
	    model.addAttribute(service.read(bno)); // 수정을 위한 글읽기 서비스 호출
	  }

	@RequestMapping(value = "/modify", method = RequestMethod.POST)// POST방식으로 데이터 전송
	  public String modifyPOST(BoardVO board, RedirectAttributes rttr) throws Exception {
	    service.modify(board); // 글수정 서비스 호출
	    return "redirect:/listAll"; // 수정이 완료된 후, 목록페이지로 리턴
	  }
    @RequestMapping(value = "/remove", method = RequestMethod.POST)// POST방식으로 데이터 전송
     	public String removePOST(@RequestParam("bno") int bno, RedirectAttributes rttr) throws Exception{
          service.remove(bno); // 글삭제 서비스 호출		 
          return "redirect:/listAll"; // 삭제가 완료된 후, 목록페이지로 리턴  
      }
}