-
전자정부프레임워크기반 게시판 만들기 (4) 글 상세보기,삽입,수정,삭제DEV/Spring 2020. 12. 30. 10:24
글 상세보기, 삽입, 수정, 삭제하기
testMapper.xml
123456789101112131415161718192021222324252627282930313233343536<?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.ivory.service.TestMapper"><!-- 게시글 목록 조회 --><select id="selectTest" resultType="egovframework.example.ivory.vo.TestVo">SELECT * FROM testORDER BY testId DESC</select><!-- 게시글 상세보기 --><select id="selectDetail" parameterType="Integer" resultType="egovframework.example.ivory.vo.TestVo">SELECT * FROM testWHERE testId = #{testId}</select><!-- 게시글 삽입 --><insert id="insertTest" parameterType="egovframework.example.ivory.vo.TestVo">INSERT INTO test(testTitle, testContent, testName, testDate)VALUES(#{testTitle},#{testContent},'ivory',now())</insert><!-- 게시글 수정 --><update id="updateTest" parameterType="egovframework.example.ivory.vo.TestVo">UPDATE test SETtestTitle = #{testTitle}, testContent = #{testContent}WHERE testId = #{testId}</update><!-- 게시글 삭제 --><delete id="deleteTest" parameterType="Integer">DELETE FROM testWHERE testId = #{testId}</delete></mapper>cs testController.java
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071package egovframework.example.ivory.controller;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.ivory.service.TestService;import egovframework.example.ivory.vo.TestVo;@Controllerpublic class TestController {@Autowiredprivate TestService testService;//글목록페이지@RequestMapping(value="/testList.do")public String testListDo(TestVo testVo, Model model) throws Exception{model.addAttribute("list", testService.selectTest(testVo));return "test/testList";}//글 상세페이지@RequestMapping(value="testDetail.do")public String viewForm(Model model, HttpServletRequest request) throws Exception{int testId = Integer.parseInt(request.getParameter("testId"));TestVo testVo = testService.selectDetail(testId);model.addAttribute("vo", testVo);return "test/testDetail";}//글작성페이지@RequestMapping(value="/testRegister.do")public String testRegister(){return "test/testRegister";}//글쓰기@RequestMapping(value="/insertTest.do")public String write(@ModelAttribute("testVo") TestVo testVo) throws Exception {testService.insertTest(testVo);return "redirect:testList.do";}//글수정@RequestMapping(value="/updateTest.do")public String updateTest(@ModelAttribute("testVo") TestVo testVo) throws Exception {testService.updateTest(testVo);return "redirect:testDetail.do?testId="+testVo.getTestId();}//글삭제@RequestMapping(value="/deleteTest.do")public String deleteTest(HttpServletRequest request) throws Exception {int testId = Integer.parseInt(request.getParameter("testId"));testService.deleteTest(testId);return "redirect:testList.do";}}cs testService.java
1234567891011121314151617181920package egovframework.example.ivory.service;import java.util.List;import egovframework.example.ivory.vo.TestVo;public interface TestService {public List<TestVo> selectTest(TestVo testVo) throws Exception;public void insertTest(TestVo testVo) throws Exception;public TestVo selectDetail(int testId) throws Exception;public void updateTest(TestVo testVo) throws Exception;public void deleteTest(int testId) throws Exception;}cs testServiceImpl.java
1234567891011121314151617181920212223242526272829303132333435363738394041424344package egovframework.example.ivory.service.impl;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import egovframework.example.ivory.dao.TestDao;import egovframework.example.ivory.service.TestService;import egovframework.example.ivory.vo.TestVo;@Servicepublic class TestServiceImpl implements TestService{@Autowiredprivate TestDao testDao;@Overridepublic List<TestVo> selectTest(TestVo testVo) throws Exception {return testDao.selectTest(testVo);}@Overridepublic void insertTest(TestVo testVo) throws Exception {testDao.insertTest(testVo);}@Overridepublic TestVo selectDetail(int testId) throws Exception {return testDao.selectDetail(testId);}@Overridepublic void updateTest(TestVo testVo) throws Exception {testDao.updateTest(testVo);}@Overridepublic void deleteTest(int testId) throws Exception {testDao.deleteTest(testId);}}cs testDao.java
1234567891011121314151617181920package egovframework.example.ivory.dao;import java.util.List;import egovframework.example.ivory.vo.TestVo;public interface TestDao {public List<TestVo> selectTest(TestVo testVo) throws Exception;public void insertTest(TestVo testVo) throws Exception;public TestVo selectDetail(int testId)throws Exception;public void updateTest(TestVo testVo) throws Exception;public void deleteTest(int testId) throws Exception;}cs testDaoImpl.java
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950package egovframework.example.ivory.dao.impl;import java.util.List;import org.apache.ibatis.session.SqlSession;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Repository;import egovframework.example.ivory.dao.TestDao;import egovframework.example.ivory.service.TestMapper;import egovframework.example.ivory.vo.TestVo;@Repositorypublic class TestDaoImpl implements TestDao {@Autowiredprivate SqlSession sqlSession;@Overridepublic List<TestVo> selectTest(TestVo testVo) throws Exception {TestMapper mapper = sqlSession.getMapper(TestMapper.class);return mapper.selectTest(testVo);}@Overridepublic void insertTest(TestVo testVo) throws Exception {TestMapper mapper = sqlSession.getMapper(TestMapper.class);mapper.insertTest(testVo);}@Overridepublic TestVo selectDetail(int testId) throws Exception {TestMapper mapper = sqlSession.getMapper(TestMapper.class);return mapper.selectDetail(testId);}@Overridepublic void updateTest(TestVo testVo) throws Exception {TestMapper mapper = sqlSession.getMapper(TestMapper.class);mapper.updateTest(testVo);}@Overridepublic void deleteTest(int testId) throws Exception {TestMapper mapper = sqlSession.getMapper(TestMapper.class);mapper.deleteTest(testId);}}cs testMapper.java
12345678910111213141516171819202122package egovframework.example.ivory.service;import java.util.List;import egovframework.example.ivory.vo.TestVo;//Mapper namespace 와 ID를 연결할 Interface 를 두어서 interface를 호출하는 방법.//Mybatis 매핑XML에 기재된 SQL을 호출하기 위한 인터페이스이다.//SQL id는 인터페이스에 정의된 메서드명과 동일하게 작성한다public interface TestMapper {public List<TestVo> selectTest(TestVo testVo) throws Exception;public void insertTest(TestVo testVo) throws Exception;public TestVo selectDetail(int testId) throws Exception;public void updateTest(TestVo testVo) throws Exception;public void deleteTest(int testId) throws Exception;}cs testList.jsp
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071<%@ 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 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"><title>Board List</title><!-- Bootstrap CSS --><link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous"><script src="https://code.jquery.com/jquery-1.12.4.js"></script><style type="text/css">a{text-decoration: auto;}</style></head><body><br/><h1 class="text-center">Board List</h1><br/><br/><div class="container"><table class="table table-hover table-striped text-center" style="border:1px solid;"><colgroup><col width="10%" /><col width="50%" /><col width="20%" /><col width="20%" /></colgroup><thead><tr><th>번호</th><th>제목</th><th>작성자</th><th>등록일자</th></tr></thead><tbody><c:forEach items="${list }" var="result"><tr><td>${result.testId}</td><td><a href="testDetail.do?testId=${result.testId}">${result.testTitle}</a></td><td>${result.testName}</td><td>${result.testDate}</td></tr></c:forEach></tbody></table><hr/><div><ul class="pagination justify-content-center"><li><a href="#" style="margin-right:5px;" class="text-secondary">◀</a></li><li><a href="#" style="margin-right:5px;" class="text-secondary">1</a></li><li><a href="#" style="margin-right:5px;" class="text-secondary">2</a></li><li><a href="#" style="margin-right:5px;" class="text-secondary">3</a></li><li><a href="#" style="margin-right:5px;" class="text-secondary">4</a></li><li><a href="#" style="margin-right:5px;" class="text-secondary">5</a></li><li><a href="#" style="margin-right:5px;" class="text-secondary">▶</a></li></ul></div><a class="btn btn-outline-info" style="float:right" href="testRegister.do">글쓰기</a></div><br><script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/js/bootstrap.bundle.min.js" integrity="sha384-ygbV9kiqUc6oa4msXn9868pTtWMgiQaeYH7/t7LECLbyPA2x65Kgf80OJFdroafW" crossorigin="anonymous"></script><script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.5.4/dist/umd/popper.min.js" integrity="sha384-q2kxQ16AaE6UbzuKqyBE9/u/KzioAlnx2maXQHiDX9d4/zp8Ok3f+M7DPm+Ib6IU" crossorigin="anonymous"></script><script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/js/bootstrap.min.js" integrity="sha384-pQQkAEnwaBkjpqZ8RU1fF1AKtTcHJwFl3pblpTlHXybJjHpMYo79HY3hIi4NKxyj" crossorigin="anonymous"></script></body></html>cs testDetail.jsp
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091<%@ 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"><title>Board Detail</title><!-- 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 --><scriptsrc="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"crossorigin="anonymous"></script><script src="https://code.jquery.com/jquery-1.12.4.js"></script></head><body><br /><h1 class="text-center">Board Detail</h1><br /><br /><div class="container"><form action="updateTest.do" id="viewForm" method="post"encType="multiplart/form-data"><table class="table table-bordered"><tbody><tr><th>글번호</th><td><input name="testId" type="text" value="${vo.testId}"class="form-control" readonly /></td></tr><tr><th>제목</th><td><input type="text" value="${vo.testTitle}"name="testTitle" class="form-control" /></td></tr><tr><th>내용</th><td><textarea name="testContent" class="form-control"style="height: 200px;">${vo.testContent}</textarea></td></tr><tr><td colspan="2" style="text-align: right;"><button id="btn_previous" type="button" class="btn_previous">이전</button><button id="btn_modify" type="button" class="btn_register">수정</button><button id="btn_delete" type="button" class="btn_delete">삭제</button></td></tr></tbody></table></form></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) {var testId = ${vo.testId};if (confirm("정말 삭제하시겠습니까 ?") == true) {$("#viewForm").attr("action", "deleteTest.do?testId="+testId);$("#viewForm").submit();} else {return;}});//이전 클릭 시 testList로 이동$("#btn_previous").click(function previous() {$(location).attr('href', 'testList.do');});</script></html>cs testRegister.jsp
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071<%@ 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"><title>Board Write</title><!-- 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 --><scriptsrc="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"crossorigin="anonymous"></script><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><br/><h1 class="text-center">Board Write</h1><br/><br/><div class="container"><form id="form_test" action="insertTest.do" method="post"encType="multiplart/form-data"><table class="table table-bordered"><tbody><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></tbody></table></form></div></body><script type="text/javascript">//글쓰기$(document).on('click', '#btn_register', function(e) {$("#form_test").submit();});//이전 클릭 시 testList로 이동$("#btn_previous").click(function previous() {$(location).attr('href', 'testList.do');});</script></html>cs 게시판목록 (testList.jsp)
글 상세보기 및 수정,삭제 (testDetail.jsp)
글 쓰기 (testRegister.jsp)
'DEV > Spring' 카테고리의 다른 글
전자정부프레임워크기반 게시판 만들기 (6) 파일 업로드,다운로드 (2) 2020.12.31 전자정부프레임워크기반 게시판 만들기 (5) 페이징,검색 (2) 2020.12.30 전자정부프레임워크기반 게시판 만들기 (3) MariaDB연동, 게시판 리스트 출력 (18) 2020.12.29 전자정부프레임워크기반 게시판 만들기 (2) JSP 연결 (0) 2020.12.29 전자정부프레임워크기반 게시판 만들기 (1) 시작 (1) 2020.12.28