TIL 2022-11-02 게시판

JYR00·2022년 11월 16일
0

TIL

목록 보기
35/60

상세페이지 글 수정 삭제
boardWrite.jsp,boardDetail, boardUpdate_process, boardDelete_process.jsp

<%--
  Created by IntelliJ IDEA.
  User: admin
  Date: 2022-10-28
  Time: 오후 3:17
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
    <title>게시판 글쓰기</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/js/bootstrap.bundle.min.js"></script>

  <script>
    window.addEventListener('DOMContentLoaded', function (){
      //DOMContentLoaded 순수 자바 스크립트. 다 실행되었을 때 사용됨
      const btnBack = document.querySelector('#btn-back');
      btnBack.addEventListener('click', function () {
        history.back();
      });
    });
  </script>
</head>
<body>

<header class="container mt-3">
  <div class="p-5 mb-4 bg-light rounded-3">
    <div class="container-fluid py-4">
      <h1 class="text-center">게시물 리스트 페이지</h1>
    </div>
  </div>
</header>

<main class="container mt-5">
  <div class="row">
    <div class="col-sm-6 mx-auto">
      <form action="boardWrite_process.jsp" method="post" class="border rounded-3 p-4">

          <div class="form-floating my-3">
            <input type="text" class="form-control" id="title" name="title" placeholder="제목을 입력하세요">
            <label for="title" class="form-label">Title</label>
        </div>

        <div class="form-floating my-3">
          <input type="text" class="form-control" id="user-id" name="userId" placeholder="ID를 입력하세요">
          <label for="user-id" class="form-label">ID</label>
        </div>
        <div class="form-floating my-3">
          <textarea class="form-control" id="contents" name="contents" rows="10" placeholder="내용을 입력하세요"></textarea>
          <label for="contents" class="form-label">Contents...</label>
        </div>
          <div class="d-grid gap-2">
            <button class="btn btn-primary" type="submit">글쓰기</button>
            <button class="btn btn-secondary" type="button" id="btn-back">돌아가기</button>
          </div>
      </form>
    </div>
  </div>
</main>

<footer class="container-fluid mt-5 p-5 border-top">
  <p class="lead text-muted text-center">made by bitc java 505 </p>
</footer>
</body>
</html>

boardDetail.jsp

<%--
  Created by IntelliJ IDEA.
  User: admin
  Date: 2022-11-02
  Time: 오전 9:20
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<%@ page import="java.sql.*"%>
<!DOCTYPE html>
<html>
<head>
    <title>상세 글 읽기</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/js/bootstrap.bundle.min.js"></script>
  <script>
    window.addEventListener('DOMContentLoaded', function (){
      const btnList = document.querySelector('#btn-list');
      btnList.addEventListener('click', function (){
        history.back();
      });
    });
  </script>
</head>
<body>
<%--<h1>글읽기 페이지</h1>--%>
<%@ include file="dbconn.jsp"%>
<%
    int seq = Integer.parseInt( request.getParameter("seq"));

  String title = "";
  String contents = "";
  String userId = "";
  String createDate ="";
  String updateDate = "";
  int boardCnt = 0;
%>


<%
  PreparedStatement pstmt = null;
  ResultSet rs = null;

  try {
    String sql = "SELECT seq, title, contents, user_id, create_date, update_date, cnt FROM board ";
    sql += "WHERE seq = ? ";
    pstmt = conn.prepareStatement(sql);
    pstmt.setInt(1, seq);
    rs = pstmt.executeQuery();


    if(rs.next()) {
    title = rs.getString("title");
    contents = rs.getString("contents");
    userId = rs.getString("user_id");
    createDate = rs.getString("create_date");
    updateDate = rs.getString("update_date");
    int cnt = rs.getInt("cnt");

//    out.print(title + "<br>");
//    out.print(contents + "<br>");
//    out.print(userId + "<br>");
//    out.print(createDate + "<br>");
//    out.print(updateDate + "<br>");
//    out.print(cnt);

    }
  }

  catch (SQLException e) {
    out.println(e.getMessage());
  }
  finally {
    if (rs != null) {
      rs.close();
    }
    if (pstmt != null) {
      pstmt.close();
    }
    if (conn != null) {
      conn.close();
    }
  }
  %>

  <header class="container mt-3">
    <div class="p-5 mb-4 bg-light rounded-3">
      <div class="container-fluid py-4">
        <h1 class="text-center">상세 글 읽기 페이지</h1>
      </div>
    </div>
  </header>

  <main class="container mt-5">
    <div class="row">
        <form action="boardUpdate_process.jsp" method="post">
        <div class="col-sm-6 mx-auto">
<%--      <form action="boardWrite_process.jsp" method="post" class="border rounded-3 p-4">--%>

        <div class="form-floating my-3">
          <input type="text" class="form-control" id="seq" name="seq" placeholder="글 번호를 입력하세요" readonly
          value="<%=seq%>">
          <label for="seq" class="form-label">Seq</label>
        </div>
        <div class="form-floating my-3">
          <input type="text" class="form-control" id="title" name="title" placeholder="제목을 입력하세요"
                 value="<%=title%>">
          <label for="title" class="form-label">Title</label>
        </div>
        <div class="form-floating my-3">
          <input type="text" class="form-control" id="user-id" name="userId" placeholder="ID를 입력하세요" disabled
                 value="<%=userId%>">
<%--          disable과 readonly는 둘 다 수정 불가하지만 disable은 바탕색 바뀜--%>
          <label for="user-id" class="form-label">ID</label>
        </div>
        <div class="form-floating my-3">
          <input type="text" input class="form-control" id="create-date" name="createDate" rows="10" placeholder="날짜를 입력하세요" readonly
                 value="<%=createDate%>">
          <label for="create-date" class="form-label">Date</label>
        </div>
        <div class="form-floating my-3">
           <input type="text" class="form-control" id="board-cnt" name="boardCnt" rows="10" placeholder="조회수를 입력하세요" readonly
                value="<%=boardCnt%>">
            <label for="board-cnt" class="form-label">Count</label>
        </div>
        <div class="form-floating my-3">
            <input type="text" class="form-control" id="contents" name="contents" rows="10" placeholder="내용을 입력하세요"
                    value="<%=contents%>">
          <label for="contents" class="contents">Contents...</label>
      </div>

        <div class="my-3 row">
            <div class="col-sm">
                <button class="btn btn-secondary" type="button" id="btn-list">목록으로</button>
            </div>
            <div class="col-sm d-flex justify-content-end">

<%--                    <input type="hidden" name="seq" value="<%=seq%>">--%>
<%--                    <input type="hidden" name="title" value="<%=title%>">--%>
<%--                    <input type="hidden" name="contents" value="<%=contents%>">--%>
<%--                    이거를 안했더니 안됨. 주석처리됨.--%>
                <button class="btn btn-warning me-2" type="submit">수정</button>
                </form>
<%--              margin end2--%>
                <form action="boardDelete_process.jsp" method="get">
                <input type="hidden" name="seq" value="<%=seq%>">
                <button class="btn btn-danger" type="submit" id="btn-back">삭제</button>
<%--                  get방식이니까 ?seq = <%=seq%>--%>
                </form>
            </div>
        </div>
      </div>
    </div>
  </main>

</body>

</html>

boardUpdate_process.jsp

<%--
  Created by IntelliJ IDEA.
  User: admin
  Date: 2022-11-02
  Time: 오전 10:48
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<%@ page import="java.sql.*" %>
<%@ include file="dbconn.jsp"%>

<%
  request.setCharacterEncoding("utf-8");
  int seq = Integer.parseInt(request.getParameter("seq"));
  String title = request.getParameter("title"); //데이터받아옴
  String contents = request.getParameter("contents");

  String sql = "UPDATE board SET title = ?, contents =? ";
  sql += "WHERE seq = ? ";

  PreparedStatement pstmt = null;

  try {
    pstmt = conn.prepareStatement(sql); //객체생성
    pstmt.setString(1, title);
    pstmt.setString(2, contents);
    pstmt.setInt(3, seq);
    pstmt.executeUpdate();
  }
  catch (SQLException e) {
    out.print(e.getMessage());
  }
  finally {
    if(pstmt != null){pstmt.close();}
    if(conn != null) {conn.close();}
  }
  response.sendRedirect("boardList.jsp");
%>

boardDelete_process.jsp

<%--
  Created by IntelliJ IDEA.
  User: admin
  Date: 2022-11-02
  Time: 오전 10:36
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<%@ page import="java.sql.*" %>

<%@ include file="dbconn.jsp"%>
<%
    int seq = Integer.parseInt(request.getParameter("seq"));

    String sql = "UPDATE board SET deleted_yn = 'Y' " ;
    sql += "WHERE seq = ? " ;

    PreparedStatement pstmt = null;

    try {
        pstmt = conn.prepareStatement(sql);
        pstmt.setInt(1, seq);
        pstmt.executeUpdate();
    }
    catch (SQLException e) {
        out.print(e.getMessage());
    }
    finally {
        if(pstmt != null){pstmt.close();}
        if(conn != null) {conn.close();}
    }

    response.sendRedirect("boardList.jsp");
//    다 삭제하고 나면 이 페이지로 간다.
%>

gson 설치

// https://mvnrepository.com/artifact/com.google.code.gson/gson
implementation 'com.google.code.gson:gson:2.10'

cal, cal_process, cal_process2

form, ajax, gson

0개의 댓글