spring 게시판 구현 게시글 작성, 상세 글 보기

박예린·2023년 2월 26일

게시판 게시글 작성, 상세 글 보기

게시판 작성

view

bbswrite.jsp

<%@page import="mul.cam.a.dto.MemberDto"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	MemberDto login = (MemberDto)session.getAttribute("login");		
%>    
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>bbs write</title>

<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css">
<script src="https://cdn.jsdelivr.net/npm/jquery@3.6.3/dist/jquery.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js"></script>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>

<style type="text/css">
th{
	background-color: #007bff;
	color: white;
	text-align: center;
	vertical-align: middle;
}
</style>

</head>
<body>

<h1>글쓰기</h1>

<div id="app" class="container">

<form action="bbswriteAf.do" id="frm" method="post">

<table class="table table-sm">
<col width="100px"><col width="500px">

<tr>
	<th>아이디</th>
	<td>
		<%-- <input type="text" name="id" size="50px" value="<%=login.getId() %>" readonly="readonly"> --%>
		
		<%=login.getId() %>
		<input type="hidden" name="id" value="<%=login.getId() %>">
	</td>
</tr>
<tr>
	<th>제목</th>
	<td>
		<input type="text" id="title" name="title" size="50px" class="form-control form-control-lg" placeholder="제목기입">
	</td>
</tr>
<tr>	
	<td colspan="2">
		<textarea rows="14" id="content" name="content" class="form-control" placeholder="내용기입"></textarea>
	</td>
</tr>
<tr>
	<td colspan="2" align="right" style="padding-top: 20px">
		<!-- <input type="submit" value="글쓰기"> -->
		<button type="button" class="btn btn-primary">글작성 완료</button>
	</td>
</tr>

</table>
</form>
</div>

<script type="text/javascript">
$(document).ready(function() {
	
	$("button").click(function() {
		
		if($("#title").val().trim() == "" ){
			alert("제목을 기입해 주십시오");
			return;
		}else if($("#content").val().trim() == "" ){
			alert("내용을 기입해 주십시오");
			return;
		}else{
			$("#frm").submit();
		}		
	});	
});
</script>
</body>
</html>

Controller

BbsController.java

@GetMapping(value = "bbswrite.do")
	public String bbswrite() {
		return "bbswrite";
	}
    
/* 글 작성 후 완료를 누르면 메세지 호출이 아닌 bbslist로 바로 넘어감 */
@PostMapping(value = "bbswriteAf.do")
public String bbswriteAf(Model model, BbsDto dto) {
	boolean isS = service.writeBbs(dto);
	String bbswrite = "";		
	if(isS) {
		bbswrite = "BBS_ADD_OK";
	}else {
		bbswrite = "BBS_ADD_NG";
	}
	model.addAttribute("bbswrite", bbswrite);
		
	// return "message";
	return "redirect:/bbslist.do";	// controller에서  controller로 이동시 == sendRedirect대신
	// return "forward:/bbslist.do";	// controller에서  controller로 이동시 == forward대신
	}

mapper

Bbs.xml

<!-- 게시글 작성 -->
<insert id="writeBbs" parameterType="mul.cam.a.dto.BbsDto">
	insert into bbs(id, ref, step, depth, title, content, wdate, del, readcount)
	values(#{id}, (select ifnull(max(ref), 0)+1 from bbs b), 0, 0, 
			#{title}, #{content}, now(), 0, 0)	
</insert>

DAO/Service

BbsDao.java

int writeBbs(BbsDto dto);

BbsDaoImpl.java

@Repository
public class BbsDaoImpl implements BbsDao {

	@Autowired
	SqlSession session;
	
	String ns = "Bbs.";
    
@Override
	public int writeBbs(BbsDto dto) {		
		return session.insert(ns + "writeBbs", dto);
	}
 }

BbsService.java

public interface BbsService {

boolean writeBbs(BbsDto dto);
}

BbsServiceImpl.java

@Service
public class BbsServiceImpl implements BbsService {
@Override
	public boolean writeBbs(BbsDto dto) {
		int n = dao.writeBbs(dto);
		return n>0?true:false;
	}
}

상세 글 보기

view

bbsdetail.jsp

<%@page import="mul.cam.a.dto.MemberDto"%>
<%@page import="mul.cam.a.dto.BbsDto"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
  
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Insert title here</title>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css">
<script src="https://cdn.jsdelivr.net/npm/jquery@3.6.3/dist/jquery.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js"></script>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script>

<style type="text/css">
th{
	background-color: #007bff;
	color: white;
}
pre{
	white-space: pre-wrap;
	word-break:break-all;
	overflow: auto;
}
</style>

</head>
<body>

<%	
	BbsDto dto = (BbsDto)request.getAttribute("bbsdto");
%> 

<h1>상세 글보기</h1>

<hr>

<div id="app" class="container">

<table class="table table-striped table-sm">
<colgroup>
	<col style="width: 150px"/>
	<col style="width: 500px"/>
</colgroup>

<tr>
	<th>작성자</th>
	<td><%=dto.getId() %></td>
</tr>

<tr>
	<th>작성일</th>
	<td><%=dto.getWdate() %></td>
</tr>
<tr>
	<th>조회수</th>
	<td><%=dto.getReadcount() %></td>
</tr>
<tr>	
	<td colspan="2" style="font-size: 22px;font-weight: bold;"><%=dto.getTitle() %></td>
</tr>
<tr>	
	<td colspan="2" style="background-color: white;">
		<pre style="font-size: 20px;font-family: 고딕, arial;background-color: white"><%=dto.getContent() %></pre>
	</td>
</tr>
</table>

<br>
<button type="button" class="btn btn-primary" onclick="location.href='answer.do?seq=<%=dto.getSeq() %>'">답글</button>

<button type="button" class="btn btn-primary" onclick="location.href='bbslist.do'">글목록</button>

<!-- 로그인 한 아이디와 글 작성한 아이디가 같다면 수정과 삭제를 보여줌 -->
<%
MemberDto login = (MemberDto)session.getAttribute("login");
if(dto.getId().equals(login.getId())){
	%>
	<button type="button" class="btn btn-primary" onclick="updateBbs(<%=dto.getSeq() %>)">수정</button>
	
	<button type="button" class="btn btn-primary" onclick="deleteBbs(<%=dto.getSeq() %>)">삭제</button>
	<%
}
%>
</div>
</body>
</html>

Controller

BbsController.java

@GetMapping(value = "bbsdetail.do")
	public String bbsdetail(Model model, int seq) {
		BbsDto dto = service.getBbs(seq);
		model.addAttribute("bbsdto", dto);
		
		return "bbsdetail";
	}

mapper

Bbs.xml

<!-- 상세 글보기 -->
<select id="getBbs" parameterType="Integer" resultType="mul.cam.a.dto.BbsDto">
	select seq, id, ref, step, depth, title, content, wdate, del, readcount
	from bbs
	where seq=#{seq}
</select>

DAO/Service

BbsDao.java

public interface BbsDao {
BbsDto getBbs(int seq);
}

BbsDaoImpl.java

@Repository
public class BbsDaoImpl implements BbsDao {
@Override
	public BbsDto getBbs(int seq) {		
		return session.selectOne(ns + "getBbs", seq);
	}
}

BbsService.java

public interface BbsService {
BbsDto getBbs(int seq);
}

BbsServiceImpl.java

@Service
public class BbsServiceImpl implements BbsService {
@Override
	public BbsDto getBbs(int seq) {		
		return dao.getBbs(seq);
	}
}
profile
개발자를 꿈꾸는 귀여운 나

0개의 댓글