본문 바로가기

MVC1/게시판

게시판 list/ 글쓰기

loginAf.jsp에서 넘어옴

 

-------------------------------------->

글쓰기 링크 클릭

 

 

 

 

bbslist.jsp

<%@page import="dto.BbsDto"%>
<%@page import="java.util.List"%>
<%@page import="dao.BbsDao"%>
<%@page import="dto.MemberDto"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	Object ologin = session.getAttribute("login");
	MemberDto mem = null;
	
	if(ologin == null){//session이 날라갔을 때(시간경과 등)
		%>
		<script type="text/javascript">
		alert("로그인 해 주십시오");
		location.href = "login.jsp";
		</script>		
		<%
	}
	mem = (MemberDto)ologin;
	BbsDao dao = BbsDao.getInstance();
	List<BbsDto> list = dao.getBbsList();
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<h4 align="right" style="background-color: #f0f0f0">
	환영합니다 <%=mem.getId() %>님
</h4>
<h1>게시판</h1>
<div align="center">
<table border="1">
<col width="70"><col width="600"><col width="150">
<tr>
	<th>번호</th><th>제목</th><th>작성자</th>
</tr>
<%
	if(list == null || list.size()==0){//list가 없거나 글이 없을 때
		%>
		<tr>
			<td colspan="3">작성된 글이 없습니다</td>
		</tr>
		<%
	}else{
		
		for(int i = 0; i < list.size(); i++){
			BbsDto bbs = list.get(i);
		%>
		<tr>
			<th><%=i+1 %></th>
			<td>
				<a href="bbsdetail.jsp?seq=<%=bbs.getSeq() %>">
					<%=bbs.getTitle() %>
				</a>
			</td>
			<td align="center">
				<%=bbs.getId() %>
			</td>
		</tr>
		<%
		}
	}
%>
</table>
</div>
<a href="bbswrite.jsp">글쓰기</a>
</body>
</html>

 

 

 

 

 

bbswrite.jsp

<%@page import="dto.BbsDto"%>
<%@page import="java.util.List"%>
<%@page import="dao.BbsDao"%>
<%@page import="dto.MemberDto"%>
<%@page import="dao.MemberDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%
Object ologin = session.getAttribute("login");
MemberDto mem = null;
mem = (MemberDto)ologin;
BbsDao dao = BbsDao.getInstance();
List<BbsDto> list = dao.getBbsList();
%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Login하기</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="http://lab.alexcican.com/set_cookies/cookie.js" type="text/javascript" ></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
<style>
	body {
		color: #fff;
		background: white;
	}
	.form-control {
		min-height: 41px;
		background: #f2f2f2;
		box-shadow: none !important;
		border: transparent;
		text-align: center;
	}
	.form-area {
		min-height: 700px;
		min-width: 830px;
		background: #f2f2f2;
		box-shadow: none !important;
		border: transparent;
		text-align: center;
	}
	.form-control:focus {
		background: #e2e2e2;
	}
	.form-control, .btn {        
        border-radius: 2px;
    }
	.write-form {
		width: 900px;
		height: 700px;
		margin: 30px auto;
		border-radius: 100px;
		/* opacity: 0.95; */
	}
	.write-form h2 {
        margin: 10px 0 25px;
    }
    .write-form form {
		color: #7a7a7a;
		border-radius: 3px;
    	margin-bottom: 15px;
        background: #fff;
        box-shadow: 0px 2px 2px rgba(0, 0, 0, 0.5);
        padding: 30px;
    }
    .write-form .btn {        
        font-size: 16px;
        font-weight: bold;
		background: black;
		border: none;
        outline: none !important;
    }
	.write-form .btn:hover, .write-form .btn:focus .form-area {
		background: #2389cd;
	}
</style>
</head>
<body>
<div class="write-form">
    <form id="frm" method="post">
        <h2 class="text-center">게시글 작성</h2>   
        
        <div class="form-group has-error">
       		ID:<p class="form-control" id="id" name="id" ><%=mem.getId() %>
        </div>
		<div class="form-group">
                     제목 : <input type="text" class="form-control" id="title" name="title" placeholder="글 제목을 입력해주세요" required="required">
        </div>   
          
        <div class="form-group">
         	내용:<textarea class="form-area" id="area" name="area" cols="100" rows="40" placeholder="내용을 입력해주세요" required="required"></textarea>
        </div> 
        <div class="form-group">
            <button type="button" id="back" class="btn btn-primary btn-lg btn-block" value="">목록으로 돌아가기</button>
            <button type="button" id="front" class="btn btn-primary btn-lg btn-block" value="">작성 완료</button>
            
        </div>
    </form>
</div>
<script type="text/javascript">
$(document).ready(function() {
	$("#back").click(function() {
		location.href = "./bbslist.jsp";
	});
	
	$("#front").click(function() {
		$("#frm").attr("action", "bbswriteAf.jsp").submit();
	});
});
</script>
</body>
</html>

 

 

 

 

bbswriteAf.jsp

<%@page import="java.util.List"%>
<%@page import="dto.MemberDto"%>
<%@page import="dto.BbsDto"%>
<%@page import="dao.BbsDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	request.setCharacterEncoding("UTF-8");

	Object ologin = session.getAttribute("login");
	MemberDto mem = null;
	mem = (MemberDto)ologin;
	BbsDao dao = BbsDao.getInstance();
	List<BbsDto> list = dao.getBbsList();
	
	String id = mem.getId();
	String title = request.getParameter("title");
	String area = request.getParameter("area");
	
	System.out.println(id + title + area);
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>bbswriteAf</title>
</head>
<body>
<%
	boolean result = dao.writeBbs(new BbsDto(id, title, area));
	
	if(result){
%>
	<script type="text/javascript">
		alert("글이 추가되었습니다");
		location.href = "bbslist.jsp";
	</script>
<%
	}else{
%>
	<script type="text/javascript">
		alert("추가되지 않았습니다");
		location.href = "bbswrite.jsp";
	</script>
<%
	}
%>
</body>
</html>

 

 

 

 

 

 

BbsDao.java

package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import db.DBClose;
import db.DBConnection;
import dto.BbsDto;

public class BbsDao {
	private static BbsDao dao = new BbsDao();
	
	public BbsDao() {
		// TODO Auto-generated constructor stub
		DBConnection.initConnection();
	}
	public static BbsDao getInstance() {
		return dao;
	}
	
	//(글 목록) LIST 가져오기
	public List<BbsDto> getBbsList() {
		
		String sql = " SELECT SEQ, ID, REF, STEP, DEPTH,"
					+ " TITLE, CONTENT, WDATE,"
					+ " DEL, READCOUNT "
					+ " FROM BBS "
					+ " ORDER BY REF DESC, STEP ASC ";
		
		Connection conn = null;
		PreparedStatement psmt = null;
		ResultSet rs = null;
		
		List<BbsDto> list = new ArrayList<BbsDto>();
		
		try {
			conn = DBConnection.getConnection();
			System.out.println("1/6 getBbsList success");

			psmt = conn.prepareStatement(sql);
			System.out.println("2/6 getBbsList success");

			rs = psmt.executeQuery();
			System.out.println("3/6 getBbsList success");

			while(rs.next()) {
				int i = 1;
				BbsDto dto = new BbsDto(rs.getInt(i++), 
										rs.getString(i++), 
										rs.getInt(i++), 
										rs.getInt(i++), 
										rs.getInt(i++), 
										rs.getString(i++), 
										rs.getString(i++), 
										rs.getString(i++), 
										rs.getInt(i++), 
										rs.getInt(i++));
				list.add(dto);
			}
			System.out.println("4/6 getBbsList success");

		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			DBClose.close(psmt, conn, rs);
		}
		return list;
	}

	
	//글 추가 하기
	public boolean writeBbs(BbsDto dto) {
		//첫글 게시 할 때 REF의 최고값이 0이므로 +1, SEQ와 값이 같아짐
		String sql = " INSERT INTO BBS "
					+ " (SEQ, ID, REF, STEP, DEPTH, "
					+ " TITLE, CONTENT, WDATE, "
					+ " DEL, READCOUNT) "
					+ " VALUES( SEQ_BBS.NEXTVAL, ?, "
							+ " (SELECT NVL(MAX(REF), 0)+1 FROM BBS), 0, 0, "
							+ " ?, ?, SYSDATE, "
							+ " 0, 0) ";
		
		Connection conn = null;
		PreparedStatement psmt = null;
		
		int count = 0;
		
		try {
			conn = DBConnection.getConnection();
			System.out.println("1/6 writeBbs success");
			
			psmt = conn.prepareStatement(sql);
			psmt.setString(1, dto.getId());
			psmt.setString(2, dto.getTitle());
			psmt.setString(3, dto.getContent());
			System.out.println("2/6 writeBbs success");

			count = psmt.executeUpdate();
			System.out.println("3/6 writeBbs success");

			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			DBClose.close(psmt, conn, null);
		}
		return count>0?true:false;
	}
}

 

 

 

 

 

 

BbsDto.java

package dto;
import java.io.Serializable;
/*
CREATE TABLE BBS(
	SEQ NUMBER(8) PRIMARY KEY,
	ID VARCHAR2(50) NOT NULL,
	
	REF NUMBER(8) NOT NULL,
	STEP NUMBER(8) NOT NULL,
	DEPTH NUMBER(8) NOT NULL,
	
	TITLE VARCHAR2(200) NOT NULL,
	CONTENT VARCHAR2(4000) NOT NULL,
	WDATE DATE NOT NULL,
	
	DEL NUMBER(1) NOT NULL,
	READCOUNT NUMBER(8) NOT NULL
);
 */

//Bulletin Board System : 게시판
public class BbsDto implements Serializable {
	private int seq;		//sequence
	private String id;		//작성자
	
	private int ref;		//그룹 번호
	private int step;		//행(row)번호
	private int depth;		//깊이
	
	private String title;	//제목
	private String content;	//내용
	private String wdate; 	//작성일
	
	private int del;		//삭제
	private int readcount;	//조회수
	
	public BbsDto() {
		// TODO Auto-generated constructor stub
	}
	public BbsDto(int seq, String id, int ref, int step, int depth, String title, String content, String wdate, int del,
			int readcount) {
		super();
		this.seq = seq;
		this.id = id;
		this.ref = ref;
		this.step = step;
		this.depth = depth;
		this.title = title;
		this.content = content;
		this.wdate = wdate;
		this.del = del;
		this.readcount = readcount;
	}
	public BbsDto(String id, String title, String content) {
		super();
		this.id = id;
		this.title = title;
		this.content = content;
	}
	public int getSeq() {
		return seq;
	}
	public void setSeq(int seq) {
		this.seq = seq;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public int getRef() {
		return ref;
	}
	public void setRef(int ref) {
		this.ref = ref;
	}
	public int getStep() {
		return step;
	}
	public void setStep(int step) {
		this.step = step;
	}
	public int getDepth() {
		return depth;
	}
	public void setDepth(int depth) {
		this.depth = depth;
	}
	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 String getWdate() {
		return wdate;
	}
	public void setWdate(String wdate) {
		this.wdate = wdate;
	}
	public int getDel() {
		return del;
	}
	public void setDel(int del) {
		this.del = del;
	}
	public int getReadcount() {
		return readcount;
	}
	public void setReadcount(int readcount) {
		this.readcount = readcount;
	}
	@Override
	public String toString() {
		return "BbsDto [seq=" + seq + ", id=" + id + ", ref=" + ref + ", step=" + step + ", depth=" + depth + ", title="
				+ title + ", content=" + content + ", wdate=" + wdate + ", del=" + del + ", readcount=" + readcount
				+ "]";
	}
}

 

'MVC1 > 게시판' 카테고리의 다른 글

게시판// 페이징 처리  (0) 2020.07.30
게시판// 업데이트  (0) 2020.07.30
게시판// select search 검색  (0) 2020.07.30
게시판// 글삭제, 댓글추가, 글 detail  (0) 2020.07.29