[스프링]MVC2방식 CRUD 게시판 만들기 10.글읽기 구현
* 게시판 목록에서 게시글을 누르면 작동되는 방식
(listAll.jsp의 일부)
1. read.jsp 생성
(views 폴더에 read.jsp 생성)
2. read.jsp 작성 (HTML5기법으로 다중FORM전송 include)
<%@ 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>
3. controller에 메소드 추가
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 서비스 호출
}
}
4. 테스트 화면