-
[SPRING] SPRING 게시판 (5) - CRUD [게시물 작성]JAVA/SPRING 2020. 3. 14. 17:27728x90
게시글 작성 View 생성
// views/index.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html> <html lang="en" dir="ltr"> <head> <meta charset="utf-8"> <title></title> <style> #list{ border: 1px solid black; } th, td { border: 1px solid #444444; } </style> </head> <body> <table id="list"> <thead> <tr> <th>NO</th> <th>TITLE</th> <th>WRITER</th> <th>COUNT</th> <th>DATE</th> </tr> </thead> <tbody> <c:forEach var="item" items="${boardList}"> <tr> <td>${item.bno}</td> <td>${item.title}</td> <td>${item.writer}</td> <td>${item.count}</td> <td>${item.createTime}</td> </tr> </c:forEach> </tbody> </table> <!-- 페이지 이동 버튼 추가 --> <button type="button" id="write">WRITE</button> </body> </html> <!-- Jquery --> <script src="https://code.jquery.com/jquery-3.4.1.js" integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU=" crossorigin="anonymous"></script> <script> //게시글 작성 페이지 이동 $('#write').click(function(){ location.href = "/board/posts"; }) </script>
// views/write.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html> <html lang="en" dir="ltr"> <head> <meta charset="utf-8"> <title></title> </head> <body> <form action="posts" method="post"> <table> <tr> <td><label>Title</label></td> <td><input type="text" name="title"></td> </tr> <tr> <td><label>Content</label></td> <td><input type="text" name="content"></td> </tr> <tr> <td> <button type="submit">WRITE</button> </td> </tr> </table> </form> </body> </html> <!-- Jquery --> <script src="https://code.jquery.com/jquery-3.4.1.js" integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU=" crossorigin="anonymous"></script>
Mapper 작성
// mappers/BoardMapper.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.ms.board.Dao.BoardDao"> <select id="boardList" resultType="com.ms.board.Vo.BoardVo"> SELECT * FROM board </select> <insert id="write"> INSERT INTO board (title, content, writer, createTime, editTime) VALUES (#{title}, #{content}, "writer", now(), now()) </insert> </mapper>
Dao 작성
// Dao/BoardDao.java ... //추가 public void insertPosts(BoardVo posts) throws Exception{ sqlSession.insert(Namespace + ".write", posts); }
테스트 케이스 작성
// test/boardDaoTest.java ... //추가 @Test public void testInsertPosts() throws Exception{ BoardVo posts = new BoardVo(); posts.setTitle("test"); posts.setContent("test"); boardDao.insertPosts(posts); }
Service 작성
// Service/BoardService.java ... //추가 public void insertPosts(BoardVo posts) throws Exception{ boardDao.insertPosts(posts); }
Controller 작성
// Controller/BoardController.java ... //추가 //게시글 작성 @RequestMapping(value="/posts", method=RequestMethod.GET) public ModelAndView posts() throws Exception{ ModelAndView model = new ModelAndView(); model.setViewName("write"); return model; } //게시글 작성 처리 @RequestMapping(value="/posts", method=RequestMethod.POST) public String createPosts(BoardVo posts) throws Exception{ boardService.insertPosts(posts); return "redirect:/boardList"; }
728x90'JAVA > SPRING' 카테고리의 다른 글
[SPRING] SRPING 게시판 (7) - CRUD [게시글 삭제] (0) 2020.03.16 [SPRING] SPRING 게시판 (6) - CRUD [게시물 상세보기] (0) 2020.03.16 [SPRING] SPRING 게시판 (4) - CRUD [게시물 리스트] (0) 2020.03.12 [SPRING] SPRING 게시판 (3) - DB 연동2 (0) 2020.03.11 [SPRING] Resource specification not allowed here for source level below 1.7 Error (0) 2020.03.11