jspblog : writeForm

This commit is contained in:
kim
2021-01-21 13:18:08 +09:00
parent 9f05d0b031
commit ed6da3790e
5 changed files with 92 additions and 6 deletions

View File

@@ -1,4 +1,33 @@
package com.example.jspblog.domain.board;
import com.example.jspblog.config.DB;
import com.example.jspblog.domain.board.dto.WriteReqDto;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class BoardDao {
public int write(WriteReqDto dto) {
String sql = "insert into board(userId, title, content, createDate) values(?, ?, ?, now())";
Connection conn = DB.getConnection();
PreparedStatement pstmt = null;
if (conn != null) {
try {
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, dto.getUserId());
pstmt.setString(2, dto.getTitle());
pstmt.setString(3, dto.getContent());
int result = pstmt.executeUpdate();
return result;
} catch (Exception e) {
e.printStackTrace();
} finally {
DB.close(conn, pstmt);
}
}
return -1;
}
}

View File

@@ -0,0 +1,11 @@
package com.example.jspblog.domain.board.dto;
import lombok.Data;
@Data
public class WriteReqDto {
private int userId;
private String title;
private String content;
}

View File

@@ -1,4 +1,17 @@
package com.example.jspblog.service;
import com.example.jspblog.domain.board.BoardDao;
import com.example.jspblog.domain.board.dto.WriteReqDto;
public class BoardService {
private final BoardDao boardDao;
public BoardService() {
boardDao = new BoardDao();
}
public int 글쓰기(WriteReqDto dto) {
return boardDao.write(dto);
}
}

View File

@@ -1,13 +1,20 @@
package com.example.jspblog.web;
import com.example.jspblog.domain.board.dto.WriteReqDto;
import com.example.jspblog.domain.user.User;
import com.example.jspblog.service.BoardService;
import com.example.jspblog.service.UserService;
import com.example.jspblog.util.Script;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet(name = "board")
@WebServlet("/board")
public class BoardController extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doProcess(request, response);
@@ -19,11 +26,36 @@ public class BoardController extends HttpServlet {
protected void doProcess(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String cmd = request.getParameter("cmd");
BoardService boardService = new BoardService();
HttpSession session = request.getSession();
User principal = (User) session.getAttribute("principal");
switch (cmd) {
case "writeForm": {
if (principal != null) {
response.sendRedirect("board/writeForm.jsp");
} else {
response.sendRedirect("user/loginForm.jsp");
}
break;
}
case "write": {
int userId = Integer.parseInt(request.getParameter("userId"));
String title = request.getParameter("title");
String content = request.getParameter("content");
if(cmd.equals("loginForm")) {
} else if (cmd.equals("login")) {
WriteReqDto dto = new WriteReqDto();
dto.setUserId(userId);
dto.setTitle(title);
dto.setContent(content);
int result = boardService.글쓰기(dto);
if (result == 1) {
response.sendRedirect("index.jsp");
} else {
Script.back(response, "글쓰기 실패");
}
break;
}
}
}
}

View File

@@ -3,7 +3,8 @@
<%@ include file="../layout/header.jsp" %>
<div class="container">
<form action="#" method="POST">
<form action="${pageContext.request.contextPath}/board?cmd=write" method="POST">
<input type="hidden" name="userId" value="${sessionScope.principal.id}">
<div class="form-group">
<label for="title">Title:</label>
@@ -11,7 +12,7 @@
</div>
<div class="form-group">
<label for="content">Content:</label>
<label for="summernote">Content:</label>
<textarea class="form-control" rows="5" id="summernote" name="content"></textarea>
</div>