스프링 게시판 스터디 7 - View 구현하기
December 03, 2018
7-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";
}
@RequestMapping("/content_view")
public String content_view(HttpServletRequest request, Model model) {
logger.info("content_view");
IDao dao = sqlSession.getMapper(IDao.class);
model.addAttribute("list", dao.content_view(Integer.parseInt(request.getParameter("bId"))));
return "content_view";
}
}
7-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);
// return type이 BDto 객체다
public BDto content_view(int bId);
}
7-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>
<select id="content_view" resultType="com.javalec.bbs_prac.dto.BDto">
SELECT * FROM BOARD WHERE BID = #{param1}
</select>
</mapper>
7-4 content_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">
<style>
.jumbotron {
text-align:center;
}
</style>
</head>
<body>
<div class="jumbotron">
<h1>${list.bTitle}</h1>
<h3>${list.bUsername}</h3>
<p>${list.bContent}</p>
<p>
<a class="btn btn-primary btn-lg" href="list" role="button">목록</a>
</p>
</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>