반응형
https://knowhoon.tistory.com/108
TestController, TestService, TestDAO, TestDAO 생성(/com/knowhoon/web/)
TestController.java
package com.knowhoon.web;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class TestController {
@Autowired
private TestService testService;
@Autowired
@RequestMapping(value = "/board", method = RequestMethod.GET) //리스트
public ModelAndView board(HttpServletRequest request) {
ModelAndView mv = new ModelAndView("board");
int sb_cate = 1 ;
if(request.getParameter("sb_cate") != null){
sb_cate = Integer.parseInt(request.getParameter("sb_cate"));
if(sb_cate > 3) {
sb_cate = 1;
}
}
mv.addObject("boardList", testService.boardList(sb_cate));
mv.addObject("category", testService.getCategory(sb_cate));
mv.addObject("sb_cate", sb_cate);
return mv;
}
//@RequestMapping(value = "/write", method = {RequestMethod.GET, RequestMethod.POST})
//@RequestMapping(value ="/write") 메소드 생략시 GET, POST 둘다 받는다.
@GetMapping("/write")
public String write() {
return "write";
}
//HttpServletRequest request
//@Requestparam("title) String title
//DTO방식 두가지 다 시연하기
@PostMapping("/write")
public String write2(TestDTO testDTO) {
testDTO.setSm_id("son"); //세션 만든 후 변경 예정
testService.write(testDTO);
return "redirect:/board?sb_cate=" + testDTO.getSb_cate();//board 메소드 다시 실행
}
@GetMapping("/detail")
public ModelAndView detail(@RequestParam("sb_no") Integer sb_no) {
ModelAndView mv = new ModelAndView("detail");
mv.addObject("detail", testService.boardDetail(sb_no));
return mv;
}
@GetMapping("/boardDelete")
public String boardDelete(@RequestParam("sb_no") Integer sb_no) { //@RquestParam타입 파라미터
int result = testService.delete(sb_no);
return "redirect:/board";
}
@GetMapping("/boardUpdate")
public ModelAndView boardUpdate(HttpServletRequest request) { //servlet 타입 파라미터
ModelAndView mv = new ModelAndView("boardUpdate");
int sb_no = Integer.parseInt(request.getParameter("sb_no"));
mv.addObject("update",testService.boardDetail(sb_no));
return mv;
}
@PostMapping("/boardUpdate")
public String boardUpdate2(TestDTO testDTO) { //DTO타입 파라미터 (view단에서 넘어오는 name이 같으면 자동 저장)
testService.update(testDTO);
return "redirect:/detail?sb_no=" + testDTO.getSb_no();
}
}
TestService.java
package com.knowhoon.web;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class TestService {
@Autowired
private TestDAO testDAO;
public List<TestDTO> boardList(int sb_cate){
return testDAO.boardList(sb_cate);
}
public void write(TestDTO testDTO) {
testDAO.write(testDTO);
}
public TestDTO boardDetail(Integer sb_no) {
return testDAO.boardDetail(sb_no);
}
public int delete(Integer sb_no) {
return testDAO.delete(sb_no);
}
public int update(TestDTO testDTO) {
return testDAO.update(testDTO);
}
public String getCategory(int sb_cate) {
return testDAO.getCategory(sb_cate);
}
}
TestDTO.java
package com.knowhoon.web;
public class TestDTO {
private int sb_no, sb_count, sb_cate, sb_like, sb_dislike, sm_no;
private String sb_title, sb_content, sb_date, sb_date2, sb_modifydate, sb_file, sb_voteid, sm_name, sm_id, sca_category;
public int getSb_no() {
return sb_no;
}
public void setSb_no(int sb_no) {
this.sb_no = sb_no;
}
public int getSb_count() {
return sb_count;
}
public void setSb_count(int sb_count) {
this.sb_count = sb_count;
}
public int getSb_cate() {
return sb_cate;
}
public void setSb_cate(int sb_cate) {
this.sb_cate = sb_cate;
}
public int getSb_like() {
return sb_like;
}
public void setSb_like(int sb_like) {
this.sb_like = sb_like;
}
public int getSb_dislike() {
return sb_dislike;
}
public void setSb_dislike(int sb_dislike) {
this.sb_dislike = sb_dislike;
}
public int getSm_no() {
return sm_no;
}
public void setSm_no(int sm_no) {
this.sm_no = sm_no;
}
public String getSb_title() {
return sb_title;
}
public void setSb_title(String sb_title) {
this.sb_title = sb_title;
}
public String getSb_content() {
return sb_content;
}
public void setSb_content(String sb_content) {
this.sb_content = sb_content;
}
public String getSb_date() {
return sb_date;
}
public void setSb_date(String sb_date) {
this.sb_date = sb_date;
}
public String getSb_date2() {
return sb_date2;
}
public void setSb_date2(String sb_date2) {
this.sb_date2 = sb_date2;
}
public String getSb_modifydate() {
return sb_modifydate;
}
public void setSb_modifydate(String sb_modifydate) {
this.sb_modifydate = sb_modifydate;
}
public String getSb_file() {
return sb_file;
}
public void setSb_file(String sb_file) {
this.sb_file = sb_file;
}
public String getSb_voteid() {
return sb_voteid;
}
public void setSb_voteid(String sb_voteid) {
this.sb_voteid = sb_voteid;
}
public String getSm_name() {
return sm_name;
}
public void setSm_name(String sm_name) {
this.sm_name = sm_name;
}
public String getSm_id() {
return sm_id;
}
public void setSm_id(String sm_id) {
this.sm_id = sm_id;
}
public String getSca_category() {
return sca_category;
}
public void setSca_category(String sca_category) {
this.sca_category = sca_category;
}
}
TestDAO.java
package com.knowhoon.web;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository
public class TestDAO {
@Autowired
private SqlSession sqlSession;
public List<TestDTO> boardList(int sb_cate) { //리스트 출력
return sqlSession.selectList("test.boardList", sb_cate);
}
public void write(TestDTO testDTO) { //글쓰기
sqlSession.insert("test.write", testDTO);
}
public TestDTO boardDetail(Integer sb_no) { //게시물 출력
return sqlSession.selectOne("test.boardDetail", sb_no);
}
public int delete(Integer sb_no) { //삭제
return sqlSession.update("test.boardDelete", sb_no);
}
public int update(TestDTO testDTO) { //수정
return sqlSession.update("test.boardUpdate", testDTO);
}
public String getCategory(int sb_cate) { //카테고리 받아오기
return sqlSession.selectOne("test.getCategory", sb_cate);
}
}
testMapper.xml 수정(DAO와 연결)
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="test">
<select id="boardList" resultType="TestDTO" parameterType="integer">
SELECT sb_no, sb_cate, sb_title, sb_date, sb_count, sm_id, sm_name FROM sboardview WHERE sb_cate=#{sb_cate}
</select>
<insert id="write" parameterType="TestDTO">
INSERT INTO sboard (sb_title, sb_content, sm_no, sb_cate) VALUES (#{sb_title}, #{sb_content}, (SELECT sm_no FROM smember WHERE sm_id=#{sm_id}), ${sb_cate})
</insert>
<!-- mybatisConfig -->
<select id="boardDetail" parameterType="integer" resultType="com.knowhoon.web.TestDTO">
SELECT sb_no, sb_title, sb_content, sb_date2, sb_count, sm_id, sm_name FROM sboardview WHERE sb_no=#{sb_no}
</select>
<update id="boardDelete" parameterType="Integer">
UPDATE sboard SET sb_del=1 WHERE sb_no=#{sb_no}
</update>
<update id="boardUpdate" parameterType="TestDTO">
UPDATE sboard SET sb_title=#{sb_title}, sb_content=#{sb_content} WHERE sb_no=#{sb_no}
</update>
<select id="getCategory" parameterType="integer" resultType="String">
SELECT sca_category FROM scategory WHERE sca_no=#{sc_cate}
</select>
</mapper>
view단 jsp 파일
board.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>board</title>
<style type="text/css">
body {
line-height: 2em;
font-family: "맑은 고딕";
}
ul, li {
list-style: none;
text-align: center;
padding: 0;
margin: 0;
}
#mainWrapper {
width: 800px;
margin: 0 auto; /*가운데 정렬*/
}
#mainWrapper>ul>li:first-child {
text-align: center;
font-size: 14pt;
height: 40px;
vertical-align: middle;
line-height: 30px;
}
#ulTable {
margin-top: 10px;
}
#ulTable>li:first-child>ul>li {
background-color: #c9c9c9;
font-weight: bold;
text-align: center;
}
#ulTable>li>ul {
clear: both;
padding: 0px auto;
position: relative;
min-width: 40px;
}
#ulTable>li>ul>li {
float: left;
font-size: 10pt;
border-bottom: 1px solid silver;
vertical-align: baseline;
}
#ulTable>li>ul>li:first-child {
width: 10%;
} /*No 열 크기*/
#ulTable>li>ul>li:first-child+li {
width: 45%;
} /*제목 열 크기*/
#ulTable>li>ul>li:first-child+li+li {
width: 20%;
} /*작성일 열 크기*/
#ulTable>li>ul>li:first-child+li+li+li {
width: 15%;
} /*작성자 열 크기*/
#ulTable>li>ul>li:first-child+li+li+li+li {
width: 10%;
} /*조회수 열 크기*/
#divPaging {
clear: both;
margin: 0 auto;
width: 220px;
height: 50px;
}
#divPaging>div {
float: left;
width: 30px;
margin: 0 auto;
text-align: center;
}
#liSearchOption {
clear: both;
}
#liSearchOption>div {
margin: 0 auto;
margin-top: 30px;
width: auto;
height: 100px;
}
.left {
text-align: left;
}
</style>
</head>
<body>
<a href="./board?sb_cate=1">자유게시판</a> |
<a href="./board?sb_cate=2">공지사항</a> |
<a href="./board?sb_cate=3">문의게시판</a>
<div id="mainWrapper">
<ul>
<!-- 게시판 제목 -->
<li>${category }</li>
<!-- 게시판 목록 -->
<li>Table
<ul id="ulTable">
<c:choose>
<c:when test="${fn:length(boardList) gt 0 }">
<li>
<ul>
<li>No</li>
<li>제목</li>
<li>작성일</li>
<li>작성자</li>
<li>조회수</li>
</ul>
</li>
<!-- 게시물이 출력될 영역 -->
<c:forEach items="${boardList }" var="list">
<li>
<ul>
<li>${list.sb_no }</li>
<li class="left"><a href="./detail?sb_no=${list.sb_no }">${list.sb_title }</a></li>
<li>${list.sb_date }</li>
<li>${list.sm_id }(${list.sm_name })</li>
<li>${list.sb_count }</li>
</ul>
</li>
</c:forEach>
<!-- 게시판 페이징 영역 -->
</c:when>
<c:otherwise>
출력할 글이 없습니다.
</c:otherwise>
</c:choose>
<li><a href="./write?sb_cate=${sb_cate }">글쓰기</a>
<div id="divPaging">
<div>◀</div>
<div>
<b>1</b>
</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>▶</div>
</div></li>
<!-- 검색 폼 영역 -->
<li id='liSearchOption'>
<div>
<select id='selSearchOption'>
<option value='A'>제목+내용</option>
<option value='T'>제목</option>
<option value='C'>내용</option>
</select> <input id='txtKeyWord' /> <input type='button' value='검색' />
</div>
</li>
</ul>
</li>
</ul>
</div>
</body>
</html>
출력화면
detail.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>detail</title>
</head>
<body>
<h1>Detail</h1>
${detail.sb_no }<br>
${detail.sb_title }<button onclick="location.href='./boardDelete?sb_no=${detail.sb_no }'">삭제</button>
<button onclick="location.href='./boardUpdate?sb_no=${detail.sb_no }'">수정</button><br>
${detail.sb_content }<br>
${detail.sb_date2 }<br>
${detail.sb_count }<br>
${detail.sm_id }<br>
<a href="./board">게시판으로</a><br>
<a href="./write">글쓰기</a>
</body>
</html>
출력화면
write.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>글쓰기</title>
</head>
<body>
<form action="write" method="post">
<input type="text" name="sb_title">
<textarea rows="" cols="" name="sb_content"></textarea>
<button>글쓰기</button>
<input type="hidden" name="sb_cate" value="${param.sb_cate }">
</form>
</body>
</html>
출력화면
boardUpdate.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>boardUpdate</title>
</head>
<body>
<h1>boardUpdate</h1>
<body>
<form action="boardUpdate" method="post">
<input type="text" name="sb_title" value="${update.sb_title }">
<textarea rows="" cols="" name="sb_content">${update.sb_content }</textarea>
<input type="hidden" name="sb_no" value="${update.sb_no }">
<button>글수정하기</button>
</form>
</body>
</html>
출력화면
반응형
'프로그래밍 > Java_Spring 게시판 만들기' 카테고리의 다른 글
Spring 게시판 만들기 (6) - 페이징 처리(전자정부프레임워크 페이징) (0) | 2021.09.03 |
---|---|
Spring 게시판 만들기 (5) - 관리자 기능 구현 (0) | 2021.09.03 |
Spring 게시판 만들기 (4) - 회원가입 기능 구현 (0) | 2021.09.03 |
Spring 게시판 만들기 (3) - Login 기능 구현 (0) | 2021.08.30 |
Spring 게시판 만들기 (1) - 개발 환경 세팅 (1) | 2021.08.26 |