기초공사 (html,css,javascript)
Dao Dto 적용된 모든jsp코드 본문
//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"%>
<%@ page import="java.sql.*" %>
<%@ page import = "java.util.*" %><!-- ArrayList를 사용하기 위한 것. -->
<%@ page import = "com.companyboard.db.*" %>
<!-- 한글오류 해결 따로 빼주어야 함 -->
<% request.setCharacterEncoding("utf-8"); %>
<%
ArrayList<BoardDto> dtoList = new BoardDao().selectList();
%>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/import.css">
<title>LIST</title>
</head>
<body>
<div class="wrapper" style="padding: 20px;">
<!-- 페이지 타이틀 -->
<div class="tit-wrap">
<h2 class="tit">뉴스&미디어</h2>
</div>
<!--// 페이지 타이틀 -->
<!-- 셀렉트/검색창 -->
<div class="slt-serch">
<div class="slt-wrap slt-type01 frm-item">
<select name="#" id="#" class="slt">
<option value="#" selected>전체</option>
<option value="#">전체</option>
<option value="#">전체</option>
</select>
</div>
<div class="inp-btn frm-item">
<div class="inp-wrap inp-type01">
<input type="text" placeholder="내용을 입력하세요" class="inp">
</div>
<button type="button" class="btn btn-type01">
<span class="btn-txt">검색</span>
</button>
</div>
</div>
<!-- 셀렉트/검색창 -->
<div class="content">
<!-- 게시글 목록 -->
<div class="tbl-wrap tbl-type01">
<table class="tbl">
<caption>뉴스&미디어 게시글 목록</caption>
<colgroup>
<col class="col1" style="width: 15%;"></col>
<col class="col2" style="width: 70%;"></col>
<col class="col3" style="width: 15%;"></col>
<col class="col3" style="width: 15%;"></col>
<col class="col3" style="width: 15%;"></col>
</colgroup>
<thead>
<tr>
<th scope="col">번호</th>
<th scope="col">제목</th>
<th scope="col">작성자</th>
<th scope="col">등록일</th>
<th scope="col">조회수</th>
</tr>
</thead>
<!-- 리스트의 모든DTO 객체의 내용을 화면에 출력-->
<%
for (BoardDto dto : dtoList){
%>
<tr>
<td><%=dto.getId()%></td>
<td>
<a href="view.jsp?key=<%=dto.getId()%>">
<%=dto.getBtitle()%>
</a>
</td>
<td><%=dto.getReguser()%></td>
<td><%=dto.getRegdate()%></td>
<td><%=dto.getHits()%></td>
</tr>
<%
}
%>
</table>
</div>
<!--글쓰기 버튼 -->
<br>
<div class="write-container">
<input type="button" value="글쓰기" class="btn btn-type01 btn-txt" onclick="location.href='write.jsp'">
</div>
<!--// 게시글 목록 -->
<!-- 페이지 네비 -->
<div class="page-nav-wrap">
<div class="page-nav">
<button type="button" title="처음으로 가기" class="btn-page first"></button>
<button type="button" title="이전으로 가기" class="btn-page pre"></button>
<ul class="num-list">
<li class="btn-page on">
<a href="#" class="btn-txt">1</a>
</li>
<li class="btn-page">
<a href="#" class="btn-txt">2</a>
</li>
<li class="btn-page">
<a href="#" class="btn-txt">3</a>
</li>
<li class="btn-page page-last"><!-- 마지막 목록페이지: .btn-page.page-last -->
<a href="#" class="btn-txt" title="마지막 목록페이지">5</a><!-- 마지막 목록페이지: title="마지막 목록페이지" -->
</li>
</ul>
<button type="button" title="다음으로 가기" class="btn-page next"></button>
<button type="button" title="마지막으로 가기" class="btn-page last"></button>
</div>
</div>
<!--// 페이지 네비 -->
</div>
</div>
</body>
</html>
//view.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page import="java.sql.*" %>
<%@ page import="com.companyboard.db.*" %>
<!-- 한글오류 해결 따로 빼주어야 함 -->
<% request.setCharacterEncoding("utf-8"); %>
<%
out.println(request.getParameter("key"));
System.out.println("한글테스트:");
//지정된 글 번호 얻기,보여줄 글의 번호--지정된 글번호의 글을 DB에서 읽음
int id = Integer.parseInt(request.getParameter("key"));
BoardDto dto = new BoardDao().selectOne(id, true);
//글제목과 내용이 웹페이지에 올바르게 출력되도록
//공백과 줄넘김 처리
dto.setBtitle(dto.getBtitle().replace(" ", " "));
dto.setContent(dto.getContent().replace(" ", " ").replace("\n", "<br>"));
%>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/import.css">
<title>글쓰기양식</title>
</head>
<body>
<div class="wrapper" style="padding: 20px;">
<!-- 페이지 타이틀 -->
<div class="tit-wrap">
<h2 class="tit">뉴스&미디어</h2>
</div>
<!--// 페이지 타이틀 -->
<form method="post" accept-charset = "UTF-8" encType = "multipart/form-data" class="content" action="insert.jsp">
<p class="guide-txt txt-r">
<span class="colr-FF3000">*</span> 표시는 필수 입력 사항입니다.</p>
<table class="bg-grey write-cont">
<!-- 작성자 -->
<tr class="frm-inp">
<th class="lbel-wrap">
<div class="lbel-cont">
<label for="#" class="lbel">작성자</label>
</div>
</th>
<td class="inp-wrap inp-type01">
<!--게시글 데이터를 읽어서 DTO에 넣고... -->
<input type="text" name="reguser" placeholder="이름을 입력해 주세요." class="inp"
value="<%=dto.getReguser()%>">
</td>
</tr>
<!-- 제목 -->
<tr class="frm-inp">
<th class="lbel-wrap">
<div class="lbel-cont">
<label for="#" class="lbel">제목</label>
</div>
</th>
<td class="inp-wrap inp-type01">
<input type="text" name="btitle" placeholder="제목을 입력해 주세요." class="inp"
value="<%=dto.getBtitle()%>">
</td>
</tr>
<!-- 작성일시 -->
<tr class="frm-inp">
<th class="lbel-wrap">
<div class="lbel-cont">
<label for="#" class="lbel">작성일시</label>
</div>
</th>
<td class="inp-wrap inp-type01">
<input type="text" name="regdate" class="inp"
value="<%=dto.getRegdate()%>">
</td>
</tr>
<!--조회수 -->
<tr class="frm-inp">
<th class="lbel-wrap">
<div class="lbel-cont">
<label for="#" class="lbel">조회수</label>
</div>
</th>
<td class="inp-wrap inp-type01">
<input type="text" name="hits" class="inp"
value="<%=dto.getHits()%>">
</td>
</tr>
<!-- 내용 -->
<tr class="frm-inp">
<th class="lbel-wrap">
<div class="lbel-cont">
<label for="#" class="lbel">내용</label>
</div>
</th>
<td class="txtarea-wrap txtarea-type01">
<textarea id="content" name="content" placeholder="내용을 입력해 주세요." class="txtarea">
<%=dto.getContent()%>
</textarea>
</td>
</tr>
<tr class="frm-inp">
<th class="lbel-wrap">
<div class="lbel-cont">
<label for="#" class="lbel">첨부파일</label>
</div>
</th>
<td class="inp-wrap inp-type01 board-flex">
<div class="inp-wrap inp-type01">
<input type="text" placeholder="10MB 이하" class="inp" readonly>
</div>
<label for="file_inp" class="btn btn-type01 btn-file">
<input type="file" id="file_inp">
<span class="btn-txt">파일 첨부</span>
</label>
<!-- <input type="file" name="fileName"> -->
</td>
</tr>
</table>
<div class="btn-wrap">
<input type="button"class="btn btn-type01 white long btn-txt" value="목록보기" onclick="location.href='list.jsp'">
<input type="button" class="btn btn-type01 btn-txt long" value="수정" onclick="location.href='write.jsp?key=<%=id%>'">
<input type="button" class="btn btn-type01 btn-txt long" value="삭제" onclick="location.href='delete.jsp?del=<%=id%>'">
</div>
</form>
</div>
</body>
</html>
//write.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page import="java.sql.*" %>
<%@ page import="com.companyboard.db.*" %>
<!-- 한글오류 해결 따로 빼주어야 함 -->
<% request.setCharacterEncoding("utf-8"); %>
<%
//글번호 값 얻기.주어지지 않았으면 0으로 설정
/* String tmp = request.getParameter("id"); */
String tmp = request.getParameter("key");
int id = (tmp != null && tmp.length() > 0) ? Integer.parseInt(tmp)
:0;
//새글쓰기 모드로 가정하고 시작
BoardDto dto = new BoardDto();
String action = "insert.jsp";
//글 번호가 주어졌으면, 글 수정 모드
if(id > 0){
dto = new BoardDao().selectOne(id, false);
// 글 수정 모드일때는 저장버튼을 누르면 update 실행 update.jsp에서 파라미터이름이 key로 되어있으므로 바로밑의 update.jsp?key가 맞다.
action = "update.jsp?idkey=" + id;
}
%>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/import.css">
<title>글쓰기양식</title>
</head>
<body>
<div class="wrapper" style="padding: 20px;">
<!-- 페이지 타이틀 -->
<div class="tit-wrap">
<h2 class="tit">뉴스&미디어</h2>
</div>
<!--// 페이지 타이틀 -->
<!-- <form method="post" encType = "multipart/form-data" class="content" action="<%=action%>"> -->
<form method="post" accept-charset = "UTF-8" action="<%=action%>">
<p class="guide-txt txt-r">
<span class="colr-FF3000">*</span> 표시는 필수 입력 사항입니다.</p>
<table class="bg-grey write-cont">
<tr class="frm-inp">
<th class="lbel-wrap">
<div class="lbel-cont">
<label for="#" class="lbel">작성자</label>
<!-- <span class="required">필수 입력 사항</span> -->
</div>
</th>
<td class="inp-wrap inp-type01">
<input type="text" name="reguser" placeholder="이름을 입력해 주세요." class="inp"
value="<%=dto.getReguser()%>">
</td>
</tr>
<tr class="frm-inp">
<th class="lbel-wrap">
<div class="lbel-cont">
<label for="#" class="label">제목</label>
</div>
</th>
<td class="inp-wrap inp-type01">
<input type="text" name="btitle" placeholder="제목을 입력해 주세요." class="inp"
value="<%=dto.getBtitle()%>">
</td>
</tr>
<tr class="frm-inp">
<th class="lbel-wrap">
<div class="lbel-cont">
<label for="#" class="lbel">내용</label>
<span class="required">필수 입력 사항</span>
</div>
</th>
<td class="txtarea-wrap txtarea-type01">
<textarea id="content" name="content" placeholder="내용을 입력해 주세요." class="txtarea">
<%=dto.getContent()%>
</textarea>
</td>
</tr>
<tr class="frm-inp">
<th class="lbel-wrap">
<div class="lbel-cont">
<label for="#" class="lbel">첨부파일</label>
</div>
</th>
<td class="inp-wrap inp-type01 board-flex">
<div class="inp-wrap inp-type01">
<input type="text" placeholder="10MB 이하" class="inp" readonly>
</div>
<label for="file_inp" class="btn btn-type01 btn-file">
<input type="file" id="file_inp">
<span class="btn-txt">파일 첨부</span>
</label>
<input type="file" name="fileName">
</td>
</tr>
</table>
<div class="btn-wrap">
<input type="submit" class="btn btn-type01 btn-txt long" value="저장">
<input type="button"class="btn btn-type01 white long btn-txt" value="취소" onclick="history.back()">
</div>
</form>
</div>
</body>
</html>
//update.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.time.*" %>
<%@ page import="com.companyboard.db.*" %>
<!-- 한글오류 해결 따로 빼주어야 함 -->
<% request.setCharacterEncoding("utf-8"); %>
<%
out.println(request.getParameter("idkey"));
// 수정페이지에서 전달된 idkey
int id = Integer.parseInt(request.getParameter("idkey"));
String reguser = request.getParameter("reguser");
String btitle = request.getParameter("btitle");
String content = request.getParameter("content");
//빈칸이 하나라도 있으면 오류 출력하고 중료
if(reguser == null || reguser.length() == 0 ||
btitle == null || btitle.length() == 0 ||
content == null || content.length() == 0) {
%>
<script>
alert(reguser + '모든 항목이 빈칸 없이 입력되어야 합니다.');
history.back();
</script>
<%
return;
}
// 글 데이터를 DTO에 저장
BoardDto dto = new BoardDto();
dto.setId (id);
dto.setReguser (reguser);
dto.setBtitle (btitle);
dto.setContent(content);
//입력된 내용으로 글 내용 업데이트
new BoardDao().updateOne(dto);
//글보기 화면으로 돌아감
response.sendRedirect("list.jsp?key=" + id);
%>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/import.css">
<title>글쓰기양식</title>
</head>
<body>
<div class="wrapper" style="padding: 20px;">
<!-- 페이지 타이틀 -->
<div class="tit-wrap">
<h2 class="tit">뉴스&미디어</h2>
</div>
<!--// 페이지 타이틀 -->
<form method="post" encType = "multipart/form-data" class="content" action="insert.jsp">
<p class="guide-txt txt-r">
<span class="colr-FF3000">*</span> 표시는 필수 입력 사항입니다.</p>
<table class="bg-grey write-cont">
<tr class="frm-inp">
<th class="lbel-wrap">
<div class="lbel-cont">
<label for="#" class="lbel">작성자</label>
<!-- <span class="required">필수 입력 사항</span> -->
</div>
</th>
<td class="inp-wrap inp-type01">
<input type="text" name="reguser" placeholder="이름을 입력해 주세요." class="inp"
value="<%=reguser%>">
</td>
</tr>
<tr class="frm-inp">
<th class="lbel-wrap">
<div class="lbel-cont">
<label for="#" class="lbel">제목</label>
</div>
</th>
<td class="inp-wrap inp-type01">
<input type="text" placeholder="제목을 입력해 주세요." class="inp" value="<%=btitle%>">
</td>
</tr>
<tr class="frm-inp">
<th class="lbel-wrap">
<div class="lbel-cont">
<label for="#" class="lbel">내용</label>
<span class="required">필수 입력 사항</span>
</div>
</th>
<td class="txtarea-wrap txtarea-type01">
<textarea name="#" id="#" placeholder="내용을 입력해 주세요." class="txtarea">
<%=content%>
</textarea>
</td>
</tr>
<tr class="frm-inp">
<th class="lbel-wrap">
<div class="lbel-cont">
<label for="#" class="lbel">첨부파일</label>
</div>
</th>
<td class="inp-wrap inp-type01 board-flex">
<div class="inp-wrap inp-type01">
<input type="text" placeholder="10MB 이하" class="inp" readonly>
</div>
<label for="file_inp" class="btn btn-type01 btn-file">
<input type="file" id="file_inp">
<span class="btn-txt">파일 첨부</span>
</label>
<!-- <input type="file" name="fileName"> -->
</td>
</tr>
</table>
<div class="btn-wrap">
<input type="submit" class="btn btn-type01 btn-txt long" value="저장" onclick="location.href='list.jsp'">
<input type="button"class="btn btn-type01 white long btn-txt" value="취소" onclick="history.back()">
</div>
</form>
</div>
</body>
</html>
//delete.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.time.*" %>
<%@ page import="com.companyboard.db.*" %>
<!-- 한글오류 해결 따로 빼주어야 함 -->
<% request.setCharacterEncoding("utf-8"); %>
<%
out.println(request.getParameter("id"));
System.out.println("삭제테스트");
//지정한 글 번호 얻기--> 지정된 글 번호의 레코드를 DB에서 삭제
int id = Integer.parseInt(request.getParameter("del"));
new BoardDao().deleteOne(id); //DAO 객체를 생성한 뒤, 이 객체의 deleteOne메서드를 호출
//양식에 입력되었던 값 읽기 작성자, 제목, 내용
/* String ID = request.getParameter("ID"); */
/* String reguser = request.getParameter("reguser");
String btitle = request.getParameter("btitle");
String content = request.getParameter("content");
out.println("reguser:"+reguser+"<br>");
out.println("btitle:"+btitle+"<br>");
out.println("content:"+content+"<br>"); */
//입력된 내용으로 새 글 레코드 추가
Class.forName("oracle.jdbc.driver.OracleDriver");//드라이버불러오기
try(
Connection conn = DriverManager.getConnection(
"jdbc:oracle:thin:@192.168.0.54:1525:CDSWT", "PVOICE_TEMP", "temp1234");
Statement stmt = conn.createStatement();
){
//현재시간 얻기
String curTime = LocalDate.now() + "" +
LocalTime.now().toString().substring(0,8);
//쿼리실행
/* stmt.executeUpdate(String.format(
"delete t_board" +
"(btitle, content, reguser, regdate, hits)" +
"values('%s', '%s', '%s', '%s', 0)",
btitle, content, reguser, curTime)); */
stmt.executeUpdate("delete from t_board where id=" + id);
} catch(Exception e){
e.printStackTrace();
}
//목록보기 화면으로 돌아감
response.sendRedirect("list.jsp");
%>
//DAO
package com.companyboard.db;
import java.sql.*;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.ArrayList;
public class BoardDao {
private Connection getConnection() throws Exception{
Class.forName("oracle.jdbc.driver.OracleDriver");//JDBC드라이버불러오기
Connection conn = DriverManager.getConnection( //DB접속
"jdbc:oracle:thin:@192.168.0.54:1525:CDSWT", "PVOICE_TEMP", "temp1234");
return conn;
}
//삭제에 관한 메서드 생성
public void deleteOne(int id){
try(
Connection conn = getConnection();
Statement stmt = conn.createStatement();
){
stmt.executeUpdate("delete from t_board where id=" + id);
} catch(Exception e){
e.printStackTrace();
}
}
//view.jsp
public BoardDto selectOne(int id, boolean incHits) {
BoardDto dto = new BoardDto();
try(
Connection conn = getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(
"select * from t_board where id=" + id);
){
if (rs.next()) {
//글데이터를 DTO에 저장
dto.setId (rs.getInt("id"));
dto.setReguser(rs.getString("reguser"));
dto.setBtitle(rs.getString("btitle"));
dto.setContent(rs.getString("content"));
dto.setRegdate(rs.getString("regdate"));
dto.setHits(rs.getInt("hits"));
//이글의 조회수를 증가시켜야 하는 경우
// (글 보기 화면을 위해 읽을 때)이면 조회수 1 증가
if(incHits) {
stmt.executeUpdate(
"update t_board set hits=hits+1 where id=" + id);
}
}
} catch(Exception e) {
e.printStackTrace();
}
return dto;
}
//insert.jsp
private String getCurrentTime() {
return LocalDate.now() + " " +
LocalTime.now().toString().substring(0,8);
}
public void insertOne(BoardDto dto) {
//입력된 내용으로 새 글 레코드 추가
try(
Connection conn = DriverManager.getConnection(
"jdbc:oracle:thin:@192.168.0.54:1525:CDSWT", "PVOICE_TEMP", "temp1234");
Statement stmt = conn.createStatement();
){
//쿼리실행
stmt.executeUpdate(String.format(
"insert into t_board" +
"(btitle, content, reguser, regdate, hits)" +
"values('%s', '%s', '%s', '%s', 0)",
dto.getBtitle(), dto.getContent(), dto.getReguser(), getCurrentTime()));
} catch(Exception e){
e.printStackTrace();
}
}
//update.jsp
public void updateOne(BoardDto dto) {
try(
Connection conn = DriverManager.getConnection(
"jdbc:oracle:thin:@192.168.0.54:1525:CDSWT", "PVOICE_TEMP", "temp1234");
Statement stmt = conn.createStatement();
){
//쿼리실행
stmt.executeUpdate(String.format(
"update t_board set reguser='%s', btitle='%s', " +
"content='%s', regdate='%s' where id=%d",
dto.getReguser(), dto.getBtitle(), dto.getContent(), getCurrentTime(), dto.getId()));
} catch(Exception e){
e.printStackTrace();
}
}
//list.jsp
public ArrayList<BoardDto> selectList(){
//데이터를 DTO에 넣은 뒤에는, ArrayList에 추가해야한다.
ArrayList<BoardDto> dtoList = new ArrayList<BoardDto>();
try(
Connection conn = getConnection();
Statement stmt = conn.createStatement();
//쿼리실행
ResultSet rs = stmt.executeQuery(
"select * from t_board order by ID desc" );
){
//게시글 레코드가 남아있는 동안 반복하며 화면에 출력 아래로 업그레이드.
//while반복문에서 각각의 반복을 수행할 때마다,새로 만들어진 DTO를 ArrayList에 추가한다.
while (rs.next()){
BoardDto dto = new BoardDto();
dto.setId (rs.getInt ("id"));
dto.setReguser (rs.getString("reguser"));
dto.setBtitle (rs.getString("Btitle"));
dto.setContent(rs.getString("content"));
dto.setRegdate(rs.getString("regdate"));
dto.setHits(rs.getInt("hits"));
dtoList.add(dto);
}
} catch(Exception e){
e.printStackTrace();//프로그램 다운 안되고,에러문출력
}
return dtoList;
}
}
//DTO
package com.companyboard.db;
public class BoardDto {
private int id = 0;
private String reguser = "";
private String btitle = "";
private String content = "";
private String regdate = "";
private int hits = 0;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getReguser() {
return reguser;
}
public void setReguser(String reguser) {
this.reguser = reguser;
}
public String getBtitle() {
return btitle;
}
public void setBtitle(String btitle) {
this.btitle = btitle;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getRegdate() {
return regdate;
}
public void setRegdate(String regdate) {
this.regdate = regdate;
}
public int getHits() {
return hits;
}
public void setHits(int hits) {
this.hits = hits;
}
}
'게시판' 카테고리의 다른 글
JSTL 적용한 list.jsp코드 (0) | 2024.01.18 |
---|---|
EL을 사용한 list.jsp코드 (0) | 2024.01.18 |
모든jsp에 dao적용 (0) | 2024.01.18 |
오류 - LocalTime /해결 (0) | 2024.01.17 |
JDBC Connection객체 / Class.forname (0) | 2024.01.17 |