[스프링] READ - 글 보기
29 Dec 2021 -
2 minute read
JSP파일 만들기
/board/read.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<meta charset="UTF-8">
<title>read</title>
</head>
<body>
<h2>Read Page</h2>
<table class="table table-striped table-bordered border-primary">
<tbody>
<tr>
<th>글번호</th>
<td>${data.num}</td>
</tr>
<tr>
<th>작성일</th>
<td>
<fmt:formatDate value="${data.date}" pattern="yyyy-MM-dd" />
</td>
</tr>
<tr>
<th>이름</th>
<td>${data.uName}</td>
</tr>
<tr>
<th>제목</th>
<td>${data.subject}</td>
</tr>
<tr>
<th>내용</th>
<td>${data.content}</td>
</tr>
</tbody>
</table>
<button type="button" class="btn btn-outline-info"><a href="/board/list">돌아가기</a></button>
</body>
</html>
Controller
// read.jsp 페이지로 이동
@RequestMapping(value="/read", method = RequestMethod.GET)
public String getRead() {
return "board/read";
}
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.springtest.mappers.board">
<!-- 리스트 출력 -->
<select id="list" resultType="com.springtest.domain.TestVO">
select
num, uName, subject, content, date
from springtest
</select>
<!-- 게시글 등록 -->
<insert id="create">
insert into springtest
(uName, subject, content, date)
values
(#{uName}, #{subject}, #{content}, NOW())
</insert>
<!-- 게시글 읽기 -->
<select id="read" resultType="com.springtest.domain.TestVO">
select
num, uName, subject, content, date
from springtest where num = #{num}
</select>
</mapper>
DAO
- DAO
package com.springtest.dao;
import java.util.List;
import com.springtest.domain.TestVO;
public interface testDAO {
public List<TestVO> list();
public void create(TestVO vo);
public TestVO read(int num); // 이 부분!
}
- DAOImpl
package com.springtest.dao;
import java.util.List;
import javax.inject.Inject;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;
import com.springtest.domain.TestVO;
@Repository
public class testDAOImpl implements testDAO {
@Inject
private SqlSession sqlSession;
private static String namespace = "com.springtest.mappers.board";
// list
@Override
public List<TestVO> list() {
return sqlSession.selectList(namespace + ".list");
}
// create
@Override
public void create(TestVO vo) {
sqlSession.insert(namespace + ".create", vo);
}
// 이 부분!
// read
@Override
public TestVO read(int num) {
return sqlSession.selectOne(namespace + ".read", num);
}
}
Service
- Service
package com.springtest.service;
import java.util.List;
import com.springtest.domain.TestVO;
public interface testService {
public List<TestVO> list();
public void create(TestVO vo);
public TestVO read(int num); // ☜
}
- ServiceImpl
package com.springtest.service;
import java.util.List;
import javax.inject.Inject;
import org.springframework.stereotype.Service;
import com.springtest.dao.testDAO;
import com.springtest.domain.TestVO;
@Service
public class testServiceImpl implements testService {
@Inject
private testDAO dao;
@Override
public List<TestVO> list() {
return dao.list();
}
@Override
public void create(TestVO vo) {
dao.create(vo);
}
// ☜
@Override
public TestVO read(int num) {
return dao.read(num);
}
}
다시 Controller
package com.springtest.controller;
import java.util.List;
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 com.springtest.domain.TestVO;
import com.springtest.service.testService;
@Controller
@RequestMapping("/board/*")
public class TestController {
@Inject
private testService service;
// list.jsp
@RequestMapping(value="/list", method = RequestMethod.GET)
public String getList(Model model) throws Exception{
List<TestVO> list = service.list();
model.addAttribute("list", list);
return "board/list";
}
// create.jsp 페이지로 이동
@RequestMapping(value="/create", method = RequestMethod.GET)
public String getCreate() throws Exception{
return "board/create";
}
// create.jsp 게시물 작성하기
@RequestMapping(value="/create", method = RequestMethod.POST)
public String postCreate(TestVO vo) throws Exception{
service.create(vo);
return "redirect:list";
}
// read.jsp 페이지로 이동
@RequestMapping(value="/read", method = RequestMethod.GET)
public String getRead(Model model, int num) {
TestVO data = service.read(num);
model.addAttribute("data", data);
return "board/read";
}
}