★ 혼란을 막기위해 Dao는 Dao 패키지에 Dto는 Dto 패키지에 생성한다 ★


게시판을 구현하기위해 회원가입때 받을 정보와 게시판에 나타낼 정보(sql 테이블 정보)를 토대로 각각의 Member Dto와 Board Dto 클래스를 생성하여 private로 변수를 선언해줌(직접 수정을 방지하기 위해 정보의 수정이나 입출력은 getter와 setter를 이용하기 위함)
[* select를 이용하여 테이블을 join하게 될 경우 추가로 필요한 필드가 있다면 해당 dto에 추가하고 getter와 setter, toString 메소드 또한 변경해준다.]
package dao;
import java.sql.SQLException;
import javax.servlet.ServletContext;
import dto.Member;
import util.JDBConnect;
public class MemberDao extends JDBConnect{
public MemberDao(ServletContext application) {
super(application);
}
public int register(Member m) {
int res = 0;
try {
String sql = "Insert into member (id, pass, name) values(?,?,?)";
psmt = con.prepareStatement(sql);
psmt.setString(1, m.getId());
psmt.setString(2, m.getPw());
psmt.setString(3, m.getName());
res = psmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return res;
}
public Member login(String id) {
Member mem = new Member();
try {
String sql = "Select * from member where id=?";
psmt=con.prepareStatement(sql);
psmt.setString(1, id);
rs = psmt.executeQuery();
if(rs.next()) {
mem.setId(rs.getString(1));
mem.setPw(rs.getString(2));
mem.setName(rs.getString(3));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return mem;
}
}
package dao;
import java.sql.Date;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Map;
import javax.servlet.ServletContext;
import dto.BoardDto;
import util.JDBConnect;
public class BoardDao extends JDBConnect{
public BoardDao(ServletContext application) {
super(application);
}
//selectCount - 게시물 개수(int 반환)
public int selectCount(Map<String, String> map) {
//1) 반환값 저장할 변수 선언
int totalCnt = 0;
try {
//2) 쿼리문 작성
String sql = "select count(*) from board ";
if(map.get("searchWord")!=null) {
sql+="where " + map.get("searchField") + " like '%" + map.get("searchWord") + "%' ";
}
//3) stmt 객체 생성
stmt = con.createStatement();
//4) 쿼리문 실행(psmt : 객체 생성시에 sql문을 매개값으로 넣어주고 실행시에 매개값을 주지 않았으나 stmt는 매개값없이 생성 후
// 실행시에 sql문을 매개값으로 넣어줌
rs = stmt.executeQuery(sql);
if(rs.next()) {
totalCnt = rs.getInt(1);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return totalCnt;
}
public ArrayList<BoardDto> selectList(Map<String, String> map){
//1) 반환값 저장할 변수 선언
ArrayList<BoardDto> blist = new ArrayList<BoardDto>();
try {
String sql = "select * from board ";
if(map.get("searchWord")!=null) {
sql += "where " + map.get("searchField") + " like '%" + map.get("searchWord") + "%' ";
}
sql += "order by num desc ";
sql += "limit "+map.get("offset")+" , "+map.get("pageSize");
stmt = con.createStatement();
rs = stmt.executeQuery(sql);
while(rs.next()) {
BoardDto dto = new BoardDto();
dto.setNum(rs.getInt(1));
dto.setTitle(rs.getString(2));
dto.setContent(rs.getString(3));
dto.setId(rs.getString(4));
dto.setPostDate(rs.getTimestamp(5));
dto.setViewCnt(rs.getInt(6));
blist.add(dto);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return blist;
}
public int post(BoardDto b) {
int res = 0;
try {
String sql = "insert into board (title, content, id) values (?, ?, ?)";
psmt = con.prepareStatement(sql);
psmt.setString(1, b.getTitle());
psmt.setString(2, b.getContent());
psmt.setString(3, b.getId());
res = psmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return res;
}
public BoardDto selectNum(int num) {
BoardDto dto = null;
try {
String sql = "select B.*, M.name from board B inner join member M "
+"on B.id = M.id where num = ?";
psmt = con.prepareStatement(sql);
psmt.setInt(1, num);
rs = psmt.executeQuery();
if(rs.next()) {
dto = new BoardDto();
dto.setNum(rs.getInt(1));
dto.setTitle(rs.getString(2));
dto.setContent(rs.getString(3));
dto.setId(rs.getString(4));
dto.setPostDate(rs.getTimestamp(5));
dto.setViewCnt(rs.getInt(6));
dto.setName(rs.getString(7));
};
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return dto;
}
public int updateViewCnt(int num) {
int res = 0;
try {
String sql = "update board set viewCnt = viewCnt+1 where num=?";
psmt = con.prepareStatement(sql);
psmt.setInt(1, num);
res = psmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return res;
}
public int edit(BoardDto b) {
int res = 0;
try {
String sql = "update board set title=?, content=? where num=?";
psmt=con.prepareStatement(sql);
psmt.setString(1, b.getTitle());
psmt.setString(2, b.getContent());
psmt.setInt(3, b.getNum());
res = psmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return res;
}
public int delete(int num, String id) {
int res = 0;
try {
String sql = "delete from board where id=? and num=?";
psmt = con.prepareStatement(sql);
psmt.setString(1, id);
psmt.setInt(2, num);
res = psmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return res;
}
}
위 코드에서 MemberDao에서는 psmt를 사용해보았고
BoardDao에서는 stmt를 사용해보았는데 나는 내 손에 psmt가 더 잘 익어서 psmt를 주로 사용할 것 같다.
Dao와 Dto를 잘 생성했다면 이를 토대로 각 게시판마다 데이터들을 핑퐁?하며 잘 넘겨주어 게시판의 흐름이 끊기지 않도록 한다.
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<header>
<h1>Study JSP</h1>
</header>
<%
String id = (String)session.getAttribute("id");
String name = (String)session.getAttribute("name");
if(session.getAttribute("id")!=null){
}
%>
<nav id="nav">
<div id="menu">
<a class="tab" href = "Main.jsp">홈으로</a>
<a class="tab" href = "List.jsp">게시판</a>
</div>
<% if(session.getAttribute("id")!=null){
%>
<div id="info">
<div id="welcome"><%= session.getAttribute("name")%> 님, 환영합니다!</div>
<div class="LoginAndOut"><a href="LogoutProcess.jsp">Log out</a></div>
<%} else{%>
<div class="LoginAndOut">
<a href="LoginForm.jsp">Log in</a>
<span class="navSpan"> / </span>
<a href="RegisterForm.jsp">Register</a>
</div>
</div>
<%}%>
</nav>
@charset "UTF-8";
*{
box-sizing: border-box;
}
header>h1{
background-color: white;
color : green;
padding : 20px;
margin : 15px auto;
text-align : left;
}
nav{
background-color: green;
color : white;
padding : 10px;
display : flex;
justify-content: space-between;
height : 60px;
}
#nav a{
text-decoration: none;
color : white;
}
#menu{
width : 160px;
display : flex;
justify-content: space-between;
}
.tab {
width : 80px;
height : 40px;
text-align : center;
line-height : 40px;
}
#nav>a:hover{
color : green;
background-color : white;
}
#info{
display : flex;
flex-direction : column;
height : 40px;
text-align : 40px;
}
.LoginAndOut{
display : flex;
justify-content: flex-end;
font-size : 13px;
}
.navSpan{margin : 0 15px;}
#welcome{
text-align : right;
font-size : 15px;
}
button:hover{
cursor:pointer;
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>홈화면</title>
<link rel="stylesheet" href="css/HeaderAndNav.css">
</head>
<body>
<%@ include file = "HeaderAndNav.jsp" %>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>회원가입</title>
<link rel="stylesheet" href="css/HeaderAndNav.css">
<style>
form{
border : 2px dashed green;
margin : 0 auto;
padding : 20px 0;
border-radius: 5px;
width : 300px;
height : 320px;
display : grid;
grid-template-rows : repeat(1fr, 5);
}
h2{
color : green;
text-align : center;
margin : 40px;}
.regSec{
width : 300px;
display : flex;
justify-content: center;
align-items: center;
}
input{
height : 35px;
font-size : 15px;
padding : 0 10px;
border-radius : 5px;
border : 1px solid gray;
}
button{
width : 80px;
height : 30px;
font-size : 15px;
margin : 10px;
}
</style>
</head>
<body>
<%@ include file = "HeaderAndNav.jsp" %>
<h2>회원가입</h2>
<form action="RegisterProcess.jsp" method="post">
<div class="regSec"><input type="text" name = "id" autofocus placeholder="아이디"></div>
<div class="regSec"><input type="password" name="pw" placeholder="비밀번호"></div>
<div class="regSec"><input type="password" name="pwch" placeholder="비밀번호 확인"></div>
<div class="regSec"><input type="text" name="name" placeholder="이름"></div>
<div class="regSec"><button>회원가입</button><button type="button" onclick="location.href='Main.jsp'">취소</button></div>
</form>
</body>
</html>
<%@page import="util.JSFunction"%>
<%@page import="dto.Member"%>
<%@page import="dao.MemberDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<% MemberDao dao = new MemberDao(application);
String id = request.getParameter("id");
String pw = request.getParameter("pw");
String name = request.getParameter("name");
Member mem = new Member(id, pw, name);
int res = dao.register(mem);
System.out.println(res);
String pwch = request.getParameter("pwch");
if(res==1&&pw!=null&&pw.equals(pwch)){
JSFunction.alertLocation("회원가입 완료", "LoginForm.jsp", out);
}else{
JSFunction.alertBack("회원가입 실패", out);
}
dao.close();
%>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>로그인</title>
<link rel="stylesheet" href="css/HeaderAndNav.css">
<style>
form{
border : 2px dashed green;
margin : 0 auto;
padding : 20px 0;
border-radius: 5px;
width : 300px;
height : 170px;
display : grid;
grid-template-rows : repeat(1fr, 3);
}
h2{
color : green;
text-align : center;
margin : 40px;
font-weight : bolder;}
.logSec{
width : 300px;
display : flex;
justify-content: center;
align-items: center;
}
input{
height : 35px;
font-size : 15px;
padding : 0 10px;
border-radius : 5px;
border : 1px solid gray;
}
button{
width : 80px;
height : 30px;
font-size : 15px;
margin : 10px;
}
#login{
font-size : 40px;
font-weight : bolder;
text-align : center;
color : green;
}
</style>
</head>
<body>
<%@ include file = "HeaderAndNav.jsp" %>
<p id="login">로그인</p>
<form action="LoginProcess.jsp" method="post">
<input type="hidden" name="back" value="<%=request.getParameter("back")%>">
<div class="logSec"><input type="text" name = "id" autofocus placeholder="아이디"></div>
<div class="logSec"><input type="password" name="pw" placeholder="비밀번호"></div>
<div class="logSec"><button>로그인</button></div>
</form>
</body>
</html>
<%@page import="util.JSFunction"%>
<%@page import="dto.Member"%>
<%@page import="dao.MemberDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<% MemberDao dao = new MemberDao(application);
String id = request.getParameter("id");
String pw = request.getParameter("pw");
String url = request.getParameter("back");
String back = "null".equals(url) || url==null || "".equals(url) ? "Main.jsp" : request.getParameter("back");
Member mem = dao.login(id);
if(mem.getId().equals(id)&&mem.getPw().equals(pw)){
response.sendRedirect(back);
session.setAttribute("id", id);
session.setAttribute("name", mem.getName());
}else{
JSFunction.alertBack("아이디 또는 패스워드가 존재하지 않습니다.", out);
}
dao.close();
%>
</body>
</html>
<%@page import="util.JSFunction"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
session.invalidate();
JSFunction.alertLocation("로그아웃하였습니다.", "Main.jsp", out);
%>
</body>
</script>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" href="css/HeaderAndNav.css">
<style>
*{
box-sizing: border-box;}
section {
width : 500px;
height: 600px;
margin : 0 auto;
}
#boardHeader{
text-align : center;
}
textarea{
font-size : 15px;
width: 500px;
height: 400px;
border: 1px solid green;
border-radius : 10px;
resize: none;
padding : 10px;
}
#title{
font-size : 15px;
width: 500px;
border: 1px solid green;
border-radius : 10px;
padding : 10px;
}
#btn{text-align : right;}
button{
margin : 3px;
background-color : white;
border : 1px solid green;
}
button:hover{
cursor:pointer;
}
</style>
</head>
<body>
<%@ include file = "HeaderAndNav.jsp" %>
<%@ include file = "IsLogin.jsp" %>
<section>
<form action="PostProcess.jsp" onsubmit="return formCheck(this);" method="post">
<h1 id="boardHeader">글 쓰기</h1>
<h3>제목</h3>
<input type="text" id="title" name="title">
<h3>내용</h3><textarea name="content"></textarea>
<div id=btn><button>작성</button><button type="reset">취소</button><button type="button" onclick="location.href='List.jsp'" >목록보기</button></div>
</form>
</section>
<script>
function formCheck(form){
console.log(form.title.value);
if(form.title.value.trim()==""){
alert("제목을 입력하세요.");
form.title.focus();
return false;
}
if(form.content.value.trim()==""){
alert("내용을 입력하세요.");
form.content.focuse();
return false;
}
return true;
}
</script>
</body>
</html>
<%@page import="util.JSFunction"%>
<%@page import="dto.BoardDto"%>
<%@page import="dao.BoardDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
BoardDao dao = new BoardDao(application);
String title = request.getParameter("title");
String content = request.getParameter("content");
String id = session.getAttribute("id")+"";
BoardDto dto = new BoardDto(title, content, id);
int res = dao.post(dto);
if(res==1){
JSFunction.alertLocation("정상적으로 등록되었습니다.", "List.jsp", out);
}else{
JSFunction.alertBack("잠시 후 다시 시도해주세요.", out);
}
dao.close();
%>
</body>
</html>
<%@page import="java.util.Date"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="util.PageHandler"%>
<%@page import="dto.BoardDto"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@page import="dao.BoardDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
section{
width : 1200px;
height : 700px;
margin : 0 auto;}
table{
border-collapse: collapse;
margin : 0 auto;
width : 1000px;
}
tr, th, td {
border : 1px solid lightgray;
padding : 10px;
text-align : center;
}
td{
font-size:15px;
}
th{
background-color : #F2F5F2;
font-size : 17px;
}
h1{
text-align : center;
margin-top : 40px;
}
#searchBar{
text-align : right;
margin : 20px 0;
padding-right:100px;
}
#searchField, #searchWord{
height : 30px;
}
#searchBar>button{
margin-left : 5px;
height : 30px;
}
#postBtn{
text-align : right;
margin : 10px;
padding-right : 100px;
}
#postBtn>a{
color : green;
font-size : 15px;
font-weight : bold;}
a{text-decoration: none; color : black;}
#pageNav{
text-align : center;
}
</style>
<link rel="stylesheet" href="css/HeaderAndNav.css">
</head>
<%
BoardDao dao = new BoardDao(application);
String searchField = request.getParameter("searchField");
String searchWord = request.getParameter("searchWord");
Map<String, String> map = new HashMap<String, String>();
if(searchWord!=null){
map.put("searchField", searchField);
map.put("searchWord", searchWord);
}
int totalCnt = dao.selectCount(map); // 총 게시물 수(검색결과까지 포함해서)
int pageSize = 10; // 1P 눌렀을때 보여줄 게시물 수
int naviSize = 10; // 페이지 네비게이션 크기 (1~10)
int pageNum = 1; // 처음 실행했을 때 List.jsp -> pageNum =1 로 초기화
String pageTmp = request.getParameter("pageNum");
// 쿼리스트링에 pageNum이 있으면
if(pageTmp!=null&& !"".equals(pageTmp)){
pageNum = Integer.parseInt(pageTmp);
}
PageHandler ph = new PageHandler(totalCnt, pageSize, naviSize, pageNum, searchField, searchWord);
int offset = (pageNum-1)*pageSize; //시작점
map.put("offset", offset+"");
map.put("pageSize", pageSize+"");
ArrayList<BoardDto> blist = dao.selectList(map);
dao.close();
%>
<body>
<%@ include file = "HeaderAndNav.jsp" %>
<%@ include file = "IsLogin.jsp" %>
<section>
<form>
<h1>회원제 게시판</h1>
<div id="searchBar">
<select name = "searchField" id = "searchField">
<option value="title" <%="title".equals(searchField)? "selected" : ""%>>제목</option>
<option value="content" <%="content".equals(searchField)? "selected" : ""%>>내용</option>
</select>
<input type="text" id= "searchWord" name="searchWord" value = "<%= searchWord!=null? searchWord : "" %>"><button>검색</button>
</div>
<table>
<tr>
<th width="10%">번호</th>
<th width="50%">제목</th>
<th width="10%">작성자</th>
<th width="10%">조회수</th>
<th width="20%">작성일</th>
</tr>
<%
if(blist.isEmpty()){
%>
<tr><td colspan = "5" align="center">등록된 게시물이 없습니다.</td>
<%} else{
for(BoardDto tmp : blist){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm");
Date today = new Date();
String todayDate = sdf.format(today);
String postDate = sdf.format(tmp.getPostDate());
if(todayDate.equals(postDate)){
postDate = sdf2.format(tmp.getPostDate());
}
%>
<tr>
<td><%=tmp.getNum() %></td>
<td><a href="BoardView.jsp?num=<%=tmp.getNum() %>&pageNum=<%=pageNum%>"><%=tmp.getTitle() %></a></td>
<td><%=tmp.getId() %></td>
<td><%=tmp.getViewCnt() %></td>
<td><%=postDate %></td>
</tr>
<% };
};%>
</table>
<div id="postBtn"><a href="PostForm.jsp">글쓰기</a></div>
<div id="pageNav" name="pageNav"><%=ph.pageStr(request.getRequestURI())%></div>
</form>
</section>
</body>
</html>
<%@page import="util.JSFunction"%>
<%@page import="dto.BoardDto"%>
<%@page import="dao.BoardDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
*{box-sizing: border-box;
margin : 0;
}
form {
width : 500px;
height: 600px;
margin : 0 auto;
}
#container{
border : 1px solid grey;
padding : 10px;
}
button{
margin : 3px;
background-color : white;
border : 1px solid green;
}
h2{text-align : center;
margin : 20px;
}
#viewInfo{
font-size : 12px;}
</style>
</head>
<link rel="stylesheet" href="css/HeaderAndNav.css">
<body>
<%@ include file = "HeaderAndNav.jsp" %>
<%
BoardDao dao = new BoardDao(application);
String tmp = request.getParameter("num");
if(tmp==null){
JSFunction.alertLocation("잘못된 접근입니다.", "Main.jsp", out);
return;
}
int num = Integer.parseInt(tmp);
int res = dao.updateViewCnt(num);
BoardDto dto = null;
if(res==1){
dto = dao.selectNum(num);
System.out.println(dto);
}
String pageNum = request.getParameter("pageNum");
pageNum = pageNum==null ? "1":pageNum;
dao.close();
%>
<h2>상세보기(View)</h2>
<form action="EditForm.jsp?num=<%= dto.getNum()%>">
<div id="container">
<input type="hidden" name = "num" value="<%=dto.getNum() %>">
<div><h3><%=dto.getTitle() %></h3></div>
<div id="viewInfo">작성자 : <%= dto.getName() %> / 작성일 : <%=dto.getPostDate() %> / 조회수 : <%=dto.getViewCnt() %></div>
<br>
<div><h4>내용</h4><%=dto.getContent().replace("\r\n","<br>") %></div>
</div>
<%
if(dto.getId().equals(session.getAttribute("id"))){
%> <button>수정</button><button type="button" onclick="deleteBoard()">삭제</button><button type="button" onclick="location.href='List.jsp?pageNum=<%=request.getParameter("pageNum")%>'">목록</button>
<% } else{%>
<button type="button" onclick="location.href='List.jsp?pageNum=<%=request.getParameter("pageNum")%>'">목록</button>
<% } %>
</form>
<script>
function deleteBoard(){
let check = confirm("정말로 삭제하시겠습니까?");
if(check){
location.href="DeleteProcess.jsp?num=<%=dto.getNum()%>";
}
}
</script>
</body>
</html>
<%@page import="dto.BoardDto"%>
<%@page import="dao.BoardDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" href="css/HeaderAndNav.css">
<style>
*{
box-sizing: border-box;}
section {
width : 500px;
height: 600px;
margin : 0 auto;
}
#boardHeader{
text-align : center;
}
textarea{
font-size : 15px;
width: 500px;
height: 400px;
border: 1px solid green;
border-radius : 10px;
resize: none;
padding : 10px;
}
#title{
font-size : 15px;
width: 500px;
border: 1px solid green;
border-radius : 10px;
padding : 10px;
}
#btn{text-align : right;}
button{
margin : 3px;
background-color : white;
border : 1px solid green;
}
</style>
</head>
<body>
<%@ include file = "HeaderAndNav.jsp" %>
<%@ include file = "IsLogin.jsp" %>
<%
int num = Integer.parseInt(request.getParameter("num"));
BoardDao dao = new BoardDao(application);
BoardDto dto = dao.selectNum(num);
%>
<section>
<form action="EditProcess.jsp?num=<%=dto.getNum() %>" onsubmit="return formCheck(this);" method="post">
<h1 id="boardHeader">글 쓰기</h1>
<h3>제목</h3>
<input type="text" id="title" name="title" value="<%=dto.getTitle()%>">
<h3>내용</h3><textarea name="content"><%=dto.getContent()%></textarea>
<div id=btn><button>수정</button><button type="button" onclick="location.href='BoardView.jsp?num=<%=request.getParameter("num") %>'">취소</button><button>목록보기</button></div>
</form>
</section>
<script>
function formCheck(form){
console.log(form.title.value);
if(form.title.value.trim()==""){
alert("제목을 입력하세요.");
form.title.focus();
return false;
}
if(form.content.value.trim()==""){
alert("내용을 입력하세요.");
form.content.focuse();
return false;
}
return true;
}
</script>
</body>
</html>
<%@page import="util.JSFunction"%>
<%@page import="dto.BoardDto"%>
<%@page import="dao.BoardDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
BoardDao dao = new BoardDao(application);
String title = request.getParameter("title");
String content = request.getParameter("content");
String id = (String)session.getAttribute("id");
String tmp = request.getParameter("num");
int num = Integer.parseInt(tmp);
BoardDto board = new BoardDto(num, title, content, id);
int res = dao.edit(board);
if(res==1) {
JSFunction.alertLocation("수정완료", "BoardView.jsp?num="+num, out);
} else{
JSFunction.alertBack("잠시 후 다시 시도해주세요.", out);
}
%>
</body>
</html>
<%@page import="util.JSFunction"%>
<%@page import="dao.BoardDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String tmp = request.getParameter("num");
String id = (String)session.getAttribute("id");
int num = Integer.parseInt(tmp);
BoardDao dao = new BoardDao(application);
int res = dao.delete(num, id);
if(res==1){
JSFunction.alertLocation("정상적으로 삭제되었습니다.", "List.jsp", out);
}else{
JSFunction.alertBack("잠시 후 다시 시도해주세요.", out);
}
%>
</body>
</html>
<%@page import="util.JSFunction"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<% if(session.getAttribute("id")==null){
String back = request.getRequestURI();
JSFunction.alertLocation("로그인 후 이용해주세요.", "LoginForm.jsp?back="+back, out);
return;
}
%>
</body>
</html>