<사전작업 >1
|
JSP에서 사용자가 작성한 데이터를 Database(mariaDB)에 저장하는 Create 기능 구축
1. write (게시글 작성 페이지) .jsp 생성
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<title>게시판 리스트</title>
</head>
<body>
<h1>BOARD LIST</h1>
<hr>
<a href="/">홈</a>
<a href="/board/list">게시판 리스트</a>
<hr>
<br>
<form method="post">
<laber>작성자</laber>
<input type="text" id="writer" name="writer"/><br>
<laber>제목</laber>
<input type="text" id="title" name="title"/><br>
<laber>내용</laber>
<textarea rows="10" cols="50" name="content"></textarea><br>
<hr>
<button type="submit">전송</button>
</form>
</body>
</html>
2. Controller 에 write (게시글 작성 페이지) 메서드 GET방식 작성
package com.board.controller;
import java.lang.ProcessBuilder.Redirect;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.board.domain.BoardVO;
import com.board.service.BoardService;
@Controller
@RequestMapping("/board/*")
public class BoardController {
@Autowired
private BoardService service;
//--------------------------------게시물 리스트_GET ------------------------------
@GetMapping("/list")
public void getList(Model model) throws Exception{
List<BoardVO> list = service.list();
model.addAttribute("list",list);
logger.info("게시물 리스트 끝");
}
//--------------------------------게시글 삽입_GET ------------------------------
@GetMapping("/write")
public void getWrite() throws Exception{
}
//--------------------------------게시글 삽입_POST ------------------------------
@PostMapping("/write")
public String postWrite(BoardVO vo) throws Exception{
service.postWrite(vo);
return "redirect:/board/list";
}
//--------------------------------게시글 수정(update)_GET ------------------------------
@GetMapping("/update")
public void getUpdate() throws Exception{
}
}
//--------------------------------게시글 삽입_GET ------------------------------
@GetMapping("/write")
public void getWrite() throws Exception{
}
3. Controller 에 write (게시글 작성 페이지) 메서드 POST방식 작성
//--------------------------------게시글 삽입_POST ------------------------------
@PostMapping("/write")
public String postWrite(BoardVO vo) throws Exception{
service.postWrite(vo);
return "redirect:/board/list";
}
4.boardMapper.xml 에 게시글 삽입 쿼리문 작성
<!-- 게시글 삽입 -->
<insert id="write" parameterType="com.board.domain.BoardVO">
insert into tbl_board(writer, title, content)
value(#{writer},#{title},#{content})
</insert>
▶쿼리문이 정상 작동하는지 테스트
3. BoardDAO 및 BoardDAOImpl 게시글 삽입 부분 작성
//게시글 삽입
public void postWrite(BoardVO vo) throws Exception;
//-----------------------------게시글 삽입------------------------------
@Override
public void postWrite(BoardVO vo) throws Exception {
sql.insert(namespace+".write",vo);
}
4. BoardService 및 BoardServiceImpl 게시글 삽입 부분 작성
//게시글 삽입
public void postWrite(BoardVO vo) throws Exception;
//--------------------------게시물 삽입------------------------------
@Override
public void postWrite(BoardVO vo) throws Exception {
dao.getWrite(vo);
5. 동작 확인 테스트
'Spring > [CRUD기초]_게시판 만들기' 카테고리의 다른 글
[게시판]2. R(Read)_데이터 불러오기 (0) | 2022.05.07 |
---|
댓글