testMapper.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="egovframework.example.test.service.TestMapper">
<!--게시글 목록 조회 -->
<select id="selectTest" resultType="TestVO">
SELECT
*
FROM test
ORDER BY
testId DESC
</select>
<!--게시글 삽입 -->
<insert id="insertTest" parameterType="TestVO">
<![CDATA[
INSERT INTO test(testTitle, testContent, testName, testDate)
VALUES(#{testTitle}, #{testContent}, '밥샵', now())
]]>
</insert>
<!--게시글 클릭시 detailView -->
<select id="selectDetail"
parameterType="egovframework.example.test.domain.TestVO"
resultType="egovframework.example.test.domain.TestVO">
<![CDATA[
SELECT *
FROM test
WHERE testId = #{testId}
]]>
</select>
<!--게시글 업데이트 -->
<update id="updateTest">
update test set
testTitle = #{testTitle}
,testContent = #{testContent}
where
testId = #{testId}
</update>
<!--게시글 삭제 -->
<delete id="deleteTest">
delete from test
where
testId = #{testId}
</delete>
</mapper>
TestController
package egovframework.example.test.web;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import egovframework.example.test.domain.TestVO;
import egovframework.example.test.service.TestService;
@Controller
public class TestController {
@Autowired
private TestService testServiceImpl;
@RequestMapping(value = "/testList.do")
public String testListDo(@ModelAttribute TestVO testVO, Model model) throws Exception {
model.addAttribute("list", testServiceImpl.selectTest(testVO));
return "test/testList";
}
// 글 작성 클릭시 글 작성 페이지로 이동
@RequestMapping(value = "/testRegister.do")
public String testRegister() {
return "test/testRegister";
}
// 글 작성 버튼 구현
@RequestMapping(value = "/insertTest.do")
public String write(@ModelAttribute("testVO") TestVO testVO, RedirectAttributes rttr) throws Exception {
testServiceImpl.insertTest(testVO);
return "redirect:testList.do";
}
// HttpServletRequest 객체안에 모든 데이터들이 들어가는데 getParameter메소드로 testId 원하는 데이터 가져옴
// 제목 클릭 시 상세보기
@RequestMapping(value = "/testDetail.do")
public String viewForm(@ModelAttribute("testVO") TestVO testVO, Model model, HttpServletRequest request)
throws Exception {
int testId = Integer.parseInt(request.getParameter("testId"));
testVO.setTestId(testId);
TestVO resultVO = testServiceImpl.selectDetail(testVO);
model.addAttribute("result", resultVO);
return "test/testDetail";
}
// 수정하기
@RequestMapping(value = "/updateTest.do")
public String updateTest(@ModelAttribute("testVO") TestVO testVO, HttpServletRequest request) throws Exception {
testServiceImpl.updateTest(testVO);
return "redirect:testList.do";
}
// 삭제하기
@RequestMapping(value = "/deleteTest.do")
public String deleteTest(@ModelAttribute("testVO") TestVO testVO) throws Exception {
testServiceImpl.deleteTest(testVO);
return "redirect:testList.do";
}
}
TestService
package egovframework.example.test.service;
import java.util.List;
import egovframework.example.test.domain.TestVO;
public interface TestService {
public List<TestVO> selectTest(TestVO testVO) throws Exception;
public void insertTest(TestVO testVO)throws Exception;
public TestVO selectDetail(TestVO testVO) throws Exception;
public void updateTest(TestVO testVO) throws Exception;
public void deleteTest(TestVO testVO) throws Exception;
}
TestServiceImpl
package egovframework.example.test.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import egovframework.example.test.dao.TestDAO;
import egovframework.example.test.domain.TestVO;
import egovframework.example.test.service.TestService;
@Service("testServiceImpl")
public class TestServiceImpl implements TestService {
@Autowired
private TestDAO testDAOService;
@Override
public List<TestVO> selectTest(TestVO testVO) throws Exception {
return testDAOService.selectTest(testVO);
}
@Override
public void insertTest(TestVO testVO) throws Exception {
testDAOService.insertTest(testVO);
}
@Override
public TestVO selectDetail(TestVO testVO) throws Exception {
/*TestVO resultVO = testDAOService.selectDetail(testVO);
return resultVO;*/
return testDAOService.selectDetail(testVO);
}
@Override
public void updateTest(TestVO testVO) throws Exception {
testDAOService.updateTest(testVO);
}
@Override
public void deleteTest(TestVO testVO) throws Exception {
testDAOService.deleteTest(testVO);
}
}
TestDAO
package egovframework.example.test.dao;
import java.util.List;
import egovframework.example.test.domain.TestVO;
public interface TestDAO {
List<TestVO> selectTest(TestVO testVO) throws Exception;
void insertTest(TestVO testVO) throws Exception;
TestVO selectDetail(TestVO testVO) throws Exception;
void updateTest(TestVO testVO) throws Exception;
void deleteTest(TestVO testVO) throws Exception;
}
TestDAOService
package egovframework.example.test.dao;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import egovframework.example.test.domain.TestVO;
import egovframework.example.test.service.TestMapper;
@Service("testDAOService")
public class TestDAOService implements TestDAO {
@Autowired
private SqlSession sqlSession;
@Override
public List<TestVO> selectTest(TestVO testVO) throws Exception {
TestMapper mapper = sqlSession.getMapper(TestMapper.class);
return mapper.selectTest(testVO);
}
@Override
public void insertTest(TestVO testVO) throws Exception {
TestMapper mapper = sqlSession.getMapper(TestMapper.class);
mapper.insertTest(testVO);
}
@Override
public TestVO selectDetail(TestVO testVO) throws Exception {
TestMapper mapper = sqlSession.getMapper(TestMapper.class);
return mapper.selectDetail(testVO);
}
@Override
public void updateTest(TestVO testVO) throws Exception {
TestMapper mapper = sqlSession.getMapper(TestMapper.class);
mapper.updateTest(testVO);
}
@Override
public void deleteTest(TestVO testVO) throws Exception {
TestMapper mapper = sqlSession.getMapper(TestMapper.class);
mapper.deleteTest(testVO);
}
}
TestMapper.java
package egovframework.example.test.service;
import java.util.List;
import egovframework.example.test.domain.TestVO;
public interface TestMapper {
// 게시물 리스트 조회
public List<TestVO> selectTest(TestVO testVO) throws Exception;
// 게시물 작성
public void insertTest(TestVO testVO) throws Exception;
// 게시물 조회
public TestVO selectDetail(TestVO testVO) throws Exception;
// 게시물 수정
public void updateTest(TestVO testVO) throws Exception;
// 게시물 삭제
public void deleteTest(TestVO testVO) throws Exception;
}
testList.jsp(게시판 목록 페이지)
<%@ page language="java" contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<html>
<head>
<!-- Bootstrap CSS -->
<link rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css"
integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS"
crossorigin="anonymous">
<link href="/css/test/test.css" rel="stylesheet" type="text/css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
</head>
<body>
<h1>게시판List</h1>
<div class="testlist">
<form id="boardForm" name="boardForm" method="post">
<table class="list_table">
<colgroup>
<col width="20%" />
<col width="50%" />
<col width="15%" />
<col width="15%" />
</colgroup>
<tbody>
<thead>
<tr>
<th>번호</th>
<th>제목</th>
<th>작성자</th>
<th>등록일자</th>
</tr>
</thead>
<!-- jstl 데이터베이스를 검색해 넘겨 받은 list 를 result 라는 이름으로 순차적으로 실행을 시키게 됨 java의 for문같이 순차적으로 실행시킴-->
<c:forEach items="${list}" var="result">
<tr>
<td><c:out value="${result.testId}" /></td>
<td><a href='#' onClick='fn_view(${result.testId})'><c:out
value="${result.testTitle }" /></a></td>
<td><c:out value="${result.testName}" /></td>
<td><c:out value="${result.testDate}" /></td>
</tr>
</c:forEach>
</tbody>
</table>
</form>
</div>
<div>
<button id="btn_write" type="button" class="btn_write">글작성</button>
</div>
<br>
</body>
<script type="text/javascript">
//글 작성 버튼 클릭 시 testRegister로 이동
$("#btn_write").click(function javascript_onclikc() {
$(location).attr('href', 'testRegister.do');
});
//글조회
// 어떤 게시물을 클릭했는지 게시물의 번호(testId)를 넘겨 줘야 함 따라서 게시물 클릭 이벤트에서 게시물의 번호를 인자 값으로 받습니다.
// get 방식으로 데이터를 전송합니다. 따라서 ? 연산자를 사용해 testId를 주소 뒤에 붙여 줍니다
function fn_view(testId){
var form = document.getElementById("boardForm");
var url = "<c:url value='/testDetail.do'/>";
url = url + "?testId=" + testId;
form.action = url;
form.submit();
}
</script>
</html>
testRegister.jsp(게시판 작성 페이지)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css"
integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp"
crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<script
src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"
integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
crossorigin="anonymous"></script>
<link href="/css/test/test.css" rel="stylesheet" type="text/css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script type="text/javascript" src="js/jquery-3.1.1.min.js"></script>
</head>
<body>
<div class="container">
<table class="table table-bordered">
<thead>
<h1>글쓰기</h1>
</thead>
<tbody>
<form id="form_test" action="insertTest.do" method="post"
encType="multiplart/form-data">
<tr>
<th>제목:</th>
<td><input type="text" placeholder="제목을 입력하세요. "
name="testTitle" class="form-control" /></td>
</tr>
<tr>
<th>내용:</th>
<td><textarea placeholder="내용을 입력하세요 . " name="testContent"
class="form-control" style="height: 200px;"></textarea></td>
</tr>
<tr>
<td colspan="2">
<button id="btn_register" type="button" class="btn_register">등록</button>
<button id="btn_previous" type="button" class="btn_previous">이전</button>
</tr>
</form>
</tbody>
</table>
</div>
</body>
<script type="text/javascript">
//글쓰기
$(document).on('click', '#btn_register', function(e) {
$("#form_test").submit();
});
//이전 클릭 시 testList로 이동
$("#btn_previous").click(function javascript_onclikc() {
$(location).attr('href', 'testList.do');
});
</script>
</html>
testDetail.jsp(게시판 상세보기, 수정, 삭제 페이지)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css"
integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp"
crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<script
src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"
integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
crossorigin="anonymous"></script>
<link href="/css/test/test.css" rel="stylesheet" type="text/css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
</head>
<body>
<div class="container">
<table class="table table-bordered">
<thead>
<h1>글 상세보기 Detail</h1>
</thead>
<tbody>
<form action="updateTest.do" id="viewForm" method="post"
encType="multiplart/form-data">
<tr>
<th>글번호:</th>
<td><input name="testId" type="text" value="${result.testId}"
class="form-control" readonly /></td>
</tr>
<tr>
<th>제목:</th>
<td><input type="text" value="${result.testTitle}"
name="testTitle" class="form-control" /></td>
</tr>
<tr>
<th>내용:</th>
<td><textarea name="testContent" class="form-control"
style="height: 200px;">${result.testContent}</textarea></td>
</tr>
<tr>
<td colspan="2">
<button id="btn_previous" type="button" class="btn_previous">이전</button>
<button id="btn_delete" type="button" class="btn_previous">삭제</button>
<button id="btn_modify" type="button" class="btn_register">수정</button>
</tr>
</form>
</tbody>
</table>
</div>
</body>
<script type="text/javascript">
$(document).on('click', '#btn_modify', function(e) {
if(confirm("정말 수정하시겠습니까 ?") == true){
$("#viewForm").submit();
}
else{
return ;
}
});
$(document).on('click', '#btn_delete', function(e) {
if(confirm("정말 삭제하시겠습니까 ?") == true){
$("#viewForm").attr("action", "deleteTest.do");
$("#viewForm").submit();
}
else{
return ;
}
});
//이전 클릭 시 testList로 이동
$("#btn_previous").click(function javascript_onclikc() {
$(location).attr('href', 'testList.do');
});
</script>
</html>
test.css
@charset "UTF-8";
.testlist {
width: 100%;
}
.list_table {
width: 100%;
}
.list_table thead th {
text-align: center;
border-top: 1px solid #e5e5e5;
border-bottom: 1px solid #e5e5e5;
padding: 8px 0;
background: #faf9fa;
}
.list_table tbody td {
text-align: center;
border-bottom: 1px solid #e5e5e5;
padding: 5px 0;
}
.paging {
margin-top: 30px;
text-align: center;
}
.btn_write, .btn_register, .btn_previous {
float: right;
margin-right: 10px;
}
.pagination1 {
margin-top: 60px;
margin-left: 750px;
}
'Spring Framework > 게시판 연습' 카테고리의 다른 글
6. ajax게시판 만들기(list,페이징,검색,selectbox)(JSON형태로 AJAX비동기 통신) (0) | 2020.10.19 |
---|---|
5. 게시판 만들기(파일 업로드 다운로드) (1) | 2020.09.25 |
4. 게시판 만들기 (페이징, 검색, 페이지 목록 갯수 변경하기) (0) | 2020.09.25 |
2. 게시판 만들기 maria DB 연결 (0) | 2020.09.22 |
1. 게시판 만들기 환경설정 egovframework(전자정부프레임워크) springframework(스프링프레임워크) (0) | 2020.09.21 |