본문 바로가기

spring

Jstl 게시판 만들기

sql>tbl_board생성

create table tbl_board(
	bno int not null auto_increment, /* 등록번호 */
    title varchar(200) not null, /* 제목 */
    content text , 
    regdate datetime not null default now(), /* 등록일 현재 시간 자동*/
    viewcnt int default 0, /* 조회수  0*/
    primary key(bno)
);

 

BoardVO(class)

package com.example.domain;

import java.util.Date;

public class BoardVO {
	private int bno;
	private String title;
	private String content;
	private Date regdate;
	private String viewcnt;
	
	
	public int getBno() {
		return bno;
	}
	public void setBno(int bno) {
		this.bno = bno;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public Date getRegdate() {
		return regdate;
	}
	public void setRegdate(Date regdate) {
		this.regdate = regdate;
	}
	public String getViewcnt() {
		return viewcnt;
	}
	public void setViewcnt(String viewcnt) {
		this.viewcnt = viewcnt;
	}
	@Override
	public String toString() {
		return "BoardVO [bno=" + bno + ", title=" + title + ", content=" + content + ", regdate=" + regdate
				+ ", viewcnt=" + viewcnt + "]";
	}
	
	
	
	
}


BoardDAO(interface)

package com.example.persistence;

import java.util.List;

import com.example.domain.BoardVO;
import com.example.domain.Criteria;
import com.example.domain.SearchCriteria;

public interface BoardDAO {
	public List<BoardVO> list(Criteria cri) throws Exception;
	
	public int total() throws Exception;
	
	public List<BoardVO> slist(SearchCriteria cri) throws Exception;
	
	public int stotal(SearchCriteria cri) throws Exception;
	
	public void insert(BoardVO vo) throws Exception;
	
	public BoardVO read(int bno) throws Exception;
	
	public void delete(int bno) throws Exception;
	
	public void update(BoardVO vo)throws Exception;
}


BoardMapper(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="BoardMapper">
	<select id="list" resultType="com.example.domain.BoardVO">
		select * from tbl_board 
		order by bno desc
		limit #{pageStart}, #{perPageNum}
	</select>
	<select id="total" resultType="int">
		select count(*) from tbl_board
	</select>
	
	<select id="slist" resultType="com.example.domain.BoardVO">
		select * from tbl_board
		<if test="searchType=='title'">
			where title like concat('%',#{keyword},'%')
		</if>
		<if test="searchType=='content'">
			where content like concat('%',#{keyword},'%')
		</if>
		order by bno desc
		limit #{pageStart}, #{perPageNum}
	</select>
	
	
	<select id="stotal" resultType="int">
		select count(*) from tbl_board 
		<if test="searchType=='title'">
			where title like concat('%',#{keyword},'%')
		</if>
		<if test="searchType=='content'">
			where content like concat('%',#{keyword},'%')
		</if>
	</select>
	
	<insert id="insert">
		insert into tbl_board(title,content) 
		values(#{title},#{content})
	</insert>
	
	<select id="read" resultType="com.example.domain.BoardVO">
		select * from tbl_board 
		where bno=#{bno}
	</select>
	
	
	<delete id="delete">
		delete from tbl_board where bno=#{bno}
	</delete>
	
	<update id="update">
		update tbl_board set title=#{title},content=#{content}
		where bno=#{bno}
	</update>
</mapper>


DBTest2(class)

package com.example.web;
import javax.inject.Inject;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.example.domain.BoardVO;
import com.example.domain.Criteria;
import com.example.domain.SearchCriteria;
import com.example.domain.UserVO;
import com.example.persistence.BoardDAO;
import com.example.persistence.UserDAO;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"file:src/main/webapp/WEB-INF/spring/**/*.xml"})

public class DBTest2 {
	@Inject
	UserDAO dao;
	
	@Test
	public void list() throws Exception{
		SearchCriteria cri=new SearchCriteria();
		
		
		cri.setPerPageNum(10);
		cri.setSearchType("uname");
		cri.setKeyword("선미");
		
		dao.list(cri);
	}
	
	@Test
	public void insert()throws Exception{
		UserVO vo=new UserVO();
		vo.setUid("user101");
		vo.setUname("김수현");
		vo.setUpw("pass");
		dao.insert(vo);
	}
	
	@Test
	public void update()throws Exception{
		UserVO vo=new UserVO();
		vo.setUid("user101");
		vo.setUname("김수현");
		vo.setUpw("pass");
		dao.update(vo);
	}
	
	
	
	@Test
	public void delete() throws Exception{
		dao.delete("user02");
	}
	/*
	@Test
	public void slist() throws Exception{
		SearchCriteria cri=new SearchCriteria();
		
		cri.setPage(2);
		cri.setPerPageNum(10);
		cri.setSearchType("content");
		cri.setKeyword("내용");
		
		dao.slist(cri);
	}
	*/
}


BoardDAOImpl(class)

package com.example.persistence;

import java.util.List;

import javax.inject.Inject;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;

import com.example.domain.BoardVO;
import com.example.domain.Criteria;
import com.example.domain.SearchCriteria;

@Repository
public class BoardDAOImpl implements BoardDAO{
	@Inject
	SqlSession session;
	private static final String namespace="BoardMapper";
	
	
	
	@Override
	public List<BoardVO> list(Criteria cri) throws Exception {
		// TODO Auto-generated method stub
		return session.selectList(namespace + ".list",cri);
	}



	@Override
	public void insert(BoardVO vo) throws Exception {
		// TODO Auto-generated method stub
		session.insert(namespace+".insert",vo);
	}



	@Override
	public void delete(int bno) throws Exception {
		// TODO Auto-generated method stub
		session.delete(namespace+".delete",bno);
	}



	@Override
	public void update(BoardVO vo) throws Exception {
		// TODO Auto-generated method stub
		session.update(namespace+".update",vo);
	}



	@Override
	public int total() throws Exception {
		// TODO Auto-generated method stub
		return session.selectOne(namespace+".total");
	}



	@Override
	public List<BoardVO> slist(SearchCriteria cri) throws Exception {
		// TODO Auto-generated method stub
		return session.selectList(namespace+".slist",cri);
	}



	@Override
	public int stotal(SearchCriteria cri) throws Exception {
		// TODO Auto-generated method stub
		return session.selectOne(namespace + ".stotal",cri);
	}



	@Override
	public BoardVO read(int bno) throws Exception {
		// TODO Auto-generated method stub
		return session.selectOne(namespace+".read",bno);
	}



	
}


BoardController(class)

package com.example.web;

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 org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.example.domain.BoardVO;
import com.example.domain.PageMaker;
import com.example.domain.SearchCriteria;
import com.example.persistence.BoardDAO;


//기본루트
@RequestMapping("board")
@Controller
public class BoardController {
	@Inject
	BoardDAO dao;
	
	@RequestMapping(value="list", method=RequestMethod.GET)
	public String list(Model model,SearchCriteria cri) throws Exception{
		cri.setPerPageNum(10);
		//cri.setPage(2);
		
		
		PageMaker pm=new PageMaker();
		pm.setCri(cri);
		//cri에 몇개씩 출력하는지 받아서 pm에서 페이지를 구할 수 있음
		pm.setTotalCount(dao.stotal(cri));
		
		
		
		
		model.addAttribute("list",dao.slist(cri));
		model.addAttribute("pm",pm);
		
		//JSp 주소
		return "board/list";
	}
	
	//insert화면 이동-get
	@RequestMapping("insert")
	public String insertGet(){
		return "board/insert";
	}
	
	//insert 실제 데이터 이동 post
	@RequestMapping(value="insert",method=RequestMethod.POST)
	public String insertPost(BoardVO vo) throws Exception{
		
		dao.insert(vo);
		return "redirect:list";
	}
	
	//insert화면 이동-get
	@RequestMapping("read")
	public String read(Model model,int bno,SearchCriteria cri) throws Exception{
		
		//한개니까 vo에 받는다.
		
		model.addAttribute("vo",dao.read(bno));
		model.addAttribute("cri",cri);
		return "board/read";
	}
	@RequestMapping(value="update",method=RequestMethod.POST)
	public String update(BoardVO vo, SearchCriteria cri,RedirectAttributes rttr) throws Exception{
		
		dao.update(vo);
		
		rttr.addAttribute("keyword",cri.getKeyword());
		rttr.addAttribute("searchType",cri.getSearchType());
		rttr.addAttribute("page",cri.getPage());
		rttr.addAttribute("bno",vo.getBno());
		return "redirect:list";
	}
	//redirect로 이동할때는 model사용 불가,객체로 못넣어주고 필드 각각 넣어줘야됨
	
	@RequestMapping(value="delete",method=RequestMethod.POST)
	public String delete(int bno) throws Exception{
		dao.delete(bno);
		return "redirect:list";
	}
	
	@RequestMapping("reply")
	public String reply(){
		return "board/reply";
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}



board>insert(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">
	<title>Insert title here</title>
	<link href="${pageContext.request.contextPath}/resources/main.css" rel="stylesheet">
</head>
<body>
	<h1 style="margin-bottom:20px; margin-top:10px;">[글쓰기 화면]</h1>
	
	<!-- 입력하면 DB에 저장할 것임으로 메소드를 POST로 -->
	<form name="frm" action="insert" method="post">
		<table border=1 width=500>
			<tr>
				<td>제목:</td>
				<td><input type="text" name="title" size="50"></td>
			</tr>
			<tr>
				<td>내용:</td>
				<td><textarea rows="5" cols="52" name="content"></textarea></td>
			</tr>
		</table>
		<input type="submit" value="SAVE">
		<input type="reset" value="CANCEL">
		<input type="button" value="MAIN" onClick="location.href='list'">
	</form>
</body>
</html>


board>list(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="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!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>게시판목록</title>
	<link href="${pageContext.request.contextPath}/resources/main.css" rel="stylesheet">
	<script src="http://code.jquery.com/jquery-3.1.1.min.js"></script>
</head>
<body>
	<h1 style="margin-bottom:20px;margin-top:20px;">[게시판 목록]</h1>
	<div style="width:688px; border:2px dotted;padding:5px; margin:0px auto">
	<form name="frm" action="list" method="get" style="margin:5px;">
			<select name="searchType">
				<option value="title" <c:out value="${pm.cri.searchType=='title'?'selected':''}"/>>제목
				<option value="content" <c:out value="${pm.cri.searchType=='content'?'selected':''}"/>>내용</option>
			</select>
			
			<input type="text" name="keyword" value="${pm.cri.keyword}">
			<input type="submit" value="SEARCH">
			<input type="hidden" name="page" value="${pm.cri.page}">
			검색결과:${pm.totalCount}건
			<input type="text" name="bno">
	</form>
	</div>
		<div style="width:688px; border:2px dotted;padding:3px; margin:0px auto">
			<button onClick="location.href='insert'">INSERT</button>
		</div>	
	
	<table border=1 width=700>
	<tr class="title">
		<td width=100>번호</td>
		<td width=400>제목</td>
		<td width=100>작성일</td>
		<td width=100>조회수</td>
	</tr>
	<c:forEach items="${list}" var="vo">
	<tr class="row">
		<td width=100>${vo.bno}</td>
		<td width=300><a href="#" onClick="funread('${vo.bno}')">${vo.title}</a></td>
		<td width=200><fmt:formatDate value="${vo.regdate}" pattern="yyyy-MM-dd kk:mm:ss"/></td>
		<td width=100>${vo.viewcnt}</td>
	</tr>
	</c:forEach>
		<tr class="title">
		<td width=100>번호</td>
		<td width=400>제목</td>
		<td width=100>작성일</td>
		<td width=100>조회수</td>
	</tr>
	</table>
	<div id="pagination">
	<!-- JStl -->
		<c:if test="${pm.prev}">
			<a href="list?page=${pm.startPage-1}">◀</a>
		</c:if>
		<c:forEach begin="${pm.startPage}" end="${pm.endPage}" var="i">
			<c:if test="${i==pm.cri.page}">
				[<a href="${i}" class="active">${i}</a>]
			</c:if>
			<c:if test="${i!=pm.cri.page}">
				[<a href="${i}">${i}</a>]
			</c:if>
		</c:forEach>
		<c:if test="${pm.next}">
			<a href="list?page=${pm.endPage+1}">▶</a>
		</c:if>
	</div>
</body>
<script>

	$("#pagination").on("click","a",function(event){
		event.preventDefault();
		
		var page=$(this).attr("href");
		$(frm.page).val(page);
		frm.action="list"
		frm.submit();
	});

	
	//submit은 form안에 값을 자동으로 데이터 가져감
	function funread(bno){
		frm.bno.value=bno;
		frm.method="get";
		frm.action="read";
		frm.submit();
	}
		
</script>
</html>

main.css(css)

@CHARSET "UTF-8";

* {
	margin: 0px;
	padding: 0px;
}

table, body {
	margin: 0px auto;
	text-align: center;
}

a {
	text-decoration: none;
	color: black;
}

.active{
	color:violet;
}

.row:hover {
	background: black;
	color: white;
}
.title{
	background:black;
	color:white;
}
.tbl-user{
	margin:0px auto;
}
.tbl-title{
	color:yellow;
	background:blue;
}
.tbl-row:hover{
	background:yellow;
}
.tbl-row:nth-child(2n+1){
	color:blue;
	font-family:border;
}

'spring' 카테고리의 다른 글

맛집목록 만들기  (0) 2019.11.26
Transaction 처리  (0) 2019.11.26
게시판에 댓글만들기  (0) 2019.11.22
사용자 게시판 만들기  (0) 2019.11.22
네이버 API와 CURD작업 연결  (0) 2019.11.22