스프링 게시판 스터디 6 - Write 구현하기
December 03, 2018
- Write_view : 글을 작성할 수 있는 Form 이 있는 공간
- Write : Write_view에서 작성한 글이 DB에 저장되는 공간
6-1 Controller 작성
@Controller
public class BController {
private static final Logger logger = LoggerFactory.getLogger(BController.class);
@Autowired
private SqlSession sqlSession;
@RequestMapping("/list")
public String list(Model model) {
logger.info("list()");
IDao dao = sqlSession.getMapper(IDao.class);
model.addAttribute("list", dao.list());
return "list";
}
@RequestMapping("/delete")
public String list(HttpServletRequest request) {
logger.info("delete()");
IDao dao = sqlSession.getMapper(IDao.class);
dao.delete(Integer.parseInt(request.getParameter("bId")));
return "redirect:list";
}
@RequestMapping("write_view")
public String write_view() {
logger.info("write_view()");
return "write_view";
}
@RequestMapping("/write")
public String write(HttpServletRequest request) {
logger.info("write()");
IDao dao = sqlSession.getMapper(IDao.class);
dao.write(request.getParameter("bUsername"), request.getParameter("bTitle"), request.getParameter("bContent"));
return "redirect:list";
}
}
6-2 IDao 작성
package com.javalec.bbs_prac.dao;
import java.util.ArrayList;
import com.javalec.bbs_prac.dto.BDto;
public interface IDao {
public ArrayList<BDto> list();
public void delete(int bId);
public void write(String bUsername, String bTitle, String bContent);
}
6-3 IDao.xml (Mapper) 작성
<?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.javalec.bbs_prac.dao.IDao">
<select id="list" resultType="com.javalec.bbs_prac.dto.BDto">
SELECT * FROM BOARD ORDER BY 1 DESC
</select>
<delete id="delete">
DELETE FROM BOARD WHERE BID = #{param1}
</delete>
<insert id="write">
INSERT INTO BOARD VALUES(BOARD_SEQ.NEXTVAL, #{param1}, #{param2}, #{param3}, SYSDATE, 0)
</insert>
</mapper>
6-4 write_view.jsp 작성
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://cdn.ckeditor.com/ckeditor5/11.1.1/classic/ckeditor.js"></script>
</head>
<body>
<div class="container">
<form action="write" method="POST">
<div class="container">
</div>
<div class="form-group">
<label for="bUsername">작성자</label>
<input type="text" class="form-control" id="bUsername" name="bUsername" />
</div>
<div class="form-group">
<label for="btitle">글 제목</label>
<input type="text" class="form-control" id="bTitle" name="bTitle" />
</div>
<div class="form-group">
<label for="bContent">글 내용</label>
<textarea name="bContent" id="bContent" class="form-control"></textarea>
</div>
<button type="submit" class="btn btn-default">글쓰기</button>
<a class="btn btn-default" href="list" >목록</a>
</form>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</body>
</html>