📂 views
header 파일을 따로 빼서 list.jsp에 include를 한다.
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<link href="resources/css/board.css" rel="stylesheet" type="text/css">
📂 views
<%@ include file="header.jsp" %> -> include로 디자인 적용
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="header.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>게시판</h1>
<table>
<tr>
<td>번호</td><td>이름</td><td>제목</td><td>날짜</td><td>히트</td>
</tr>
<c:forEach items="${boardList}" var="mvc_board">
<tr>
<td>${mvc_board.bId}</td>
<td>${mvc_board.bName}</td>
<td>
<c:forEach begin="1" end="${mvc_board.bIndent}">-</c:forEach>
<a href="content_view?bId=${mvc_board.bId}">${mvc_board.bTitle}</a>
</td>
<td>${mvc_board.bDate}</td>
<td>${mvc_board.bHit}</td>
</tr>
</c:forEach>
<tr>
<td colspan="5"><a href="write_view">글작성</a></td>
</tr>
</table>
</body>
</html>
package com.oracle.oMVCBoard.controller;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.oracle.oMVCBoard.command.BCommand;
import com.oracle.oMVCBoard.command.BListCommand;
@Controller
public class BController {
private static final Logger logger = LoggerFactory.getLogger(BController.class);
BCommand command = null;
@RequestMapping("list")
public String list(Model model) {
logger.info("list start..");
command = new BListCommand();
command.execute(model);
return "list";
}
@RequestMapping("/content_view")
public String context_view(HttpServletRequest request, Model model) {
System.out.println("content_view()");
model.addAttribute("request", request);
command = new BContentCommand();
command.execute(model);
return "content_view";
}
}
- 강점 : 표준화, 로직 구성 편함, 관계맺기 편함
- 약점 : 개발자 입장에서는 불편하지만 소프트웨어 운영에는 편리함
asMap() -> model을 map으로 전환
- request 설명
// BController 이름 실제객체 model.addAttribute("request", request); // BContentCommand object-request String-request HttpServletRequest request = map.get("request"); ```
package com.oracle.oMVCBoard.command;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.ui.Model;
import com.oracle.oMVCBoard.dao.BDao;
import com.oracle.oMVCBoard.dto.BDto;
public class BContentCommand implements BCommand {
@Override
public void execute(Model model) {
// model를 Map으로 전환
Map<String, Object> map = model.asMap();
HttpServletRequest request = (HttpServletRequest) map.get("request");
String bId = request.getParameter("bId");
BDao bDao = new BDao();
BDto board = bDao.contentView(bId);
model.addAttribute("mvc_board", board);
}
}
📂 views
<%@ 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>
<form action="modify" method="post">
<input type="hidden" name="bId" value="${mvc_board.bId}">
<table border="1">
<tr>
<td>번호</td><td>${mvc_board.bId}</td>
</tr>
<tr>
<td>히트</td><td>${mvc_board.bHit}</td>
</tr>
<tr>
<td>이름</td><td><input type="text" name="bName" value="${mvc_board.bName}"></td>
</tr>
<tr>
<td>제목</td>
<td><input type="text" name="bTitle" value="${mvc_board.bTitle}"></td>
</tr>
<tr>
<td>내용</td>
<td><textarea rows="10" name="bContent">${mvc_board.bContent}</textarea></td>
</tr>
<tr>
<td>
<input type="submit" value="수정">
<a href="list">목록보기</a>
<a href="delete?bId=${mvc_board.bId}">삭제</a>
<a hidden="reply_view?bId=${mvc_board.bId}">답변</a>
</td>
</tr>
</table>
</form>
</body>
</html>
pstmt.setInt(1, Integer.parseInt(strId)); -> String을 int로 전환
public BDto contentView(String strId) {
BDto dto = null;
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
System.out.println("BDao contentView start...");
try {
conn = dataSource.getConnection();
String sql = "select * from mvc_board where bId = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, Integer.parseInt(strId));
rs = pstmt.executeQuery();
if(rs.next()) {
int bId = rs.getInt("bId");
String bName = rs.getString("bName");
String bTitle = rs.getString("bTitle");
String bContent = rs.getString("bContent");
Timestamp bDate = rs.getTimestamp("bDate");
int bHit = rs.getInt("bHit");
int bGroup = rs.getInt("bGroup");
int bStep = rs.getInt("bStep");
int bIndent = rs.getInt("bIndent");
dto = new BDto(bId, bName, bTitle, bContent, bDate, bHit, bGroup, bStep, bIndent);
}
} catch (Exception e) {
System.out.println("contentView dataSource-->"+e.getMessage());
} finally {
try {
if( rs != null) rs.close();
if( pstmt!= null ) pstmt.close();
if( conn != null ) conn.close();
} catch (Exception e2) {
System.out.println(e2.getMessage());
}
}
return dto;
}
public BDto contentView(String strId) {
upHit(strId);
Dao 안에서만 쓰기 위해 private으로 걸어준다.
int rn = pstmt.executeUpdate();->return 값 준비
private void upHit(String bId) {
Connection conn = null;
PreparedStatement pstmt = null;
System.out.println("Dao upHit start...");
try {
conn = dataSource.getConnection();
String sql = "update mvc_board set bHit = bHit + 1 where bId=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, bId);
pstmt.executeUpdate();
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
try {
if( pstmt!= null ) pstmt.close();
if( conn != null ) conn.close();
} catch (Exception e2) {
System.out.println(e2.getMessage());
}
}
}
⛔ 변경 전
👍 변경 후
@RequestMapping(value = "/modify", method = RequestMethod.POST)
public String modify(HttpServletRequest request, Model model) {
logger.info("modity start...");
model.addAttribute("request", request);
command = new BModifyCommand();
command.execute(model);
return "redirect:list";
}
package com.oracle.oMVCBoard.command;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.ui.Model;
import com.oracle.oMVCBoard.dao.BDao;
public class BModifyCommand implements BCommand {
@Override
public void execute(Model model) {
// 1. model을 Map 선언
Map<String, Object> map = model.asMap();
HttpServletRequest request = (HttpServletRequest) map.get("request");
// 2. parameter로 bId, bName, bTitle, bContent
String bId = request.getParameter("bId");
String bName = request.getParameter("bName");
String bTitle = request.getParameter("bTitle");
String bContent = request.getParameter("bContent");
BDao dao = new BDao();
dao.modify(bId, bName, bTitle, bContent);
}
}
public void modify(String bId, String bName, String bTitle, String bContent) {
Connection conn = null;
PreparedStatement pstmt = null;
System.out.println("BDao modify start...");
try {
conn = dataSource.getConnection();
String sql = "update mvc_board set bName=?, bTitle=?, bContent=? where bId=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, bName);
pstmt.setString(2, bTitle);
pstmt.setString(3, bContent);
pstmt.setString(4, bId);
pstmt.executeUpdate();
} catch (Exception e) {
System.out.println("modify dataSource-->"+e.getMessage());
} finally {
try {
if( pstmt!= null ) pstmt.close();
if( conn != null ) conn.close();
} catch (Exception e2) {
System.out.println(e2.getMessage());
}
}
}
아직 어떻게 할지 모르겠음
public BDto(int bId, String bName, String bTitle, String bContent) {
this.bId = bId;
this.bName = bName;
this.bTitle = bTitle;
this.bContent = bContent;
}
@RequestMapping("/delete")
public String delete(HttpServletRequest request, Model model) {
System.out.println("delete()");
model.addAttribute("request", request);
command = new BDeleteCommand();
command.execute(model);
return "redirect:list";
}
package com.oracle.oMVCBoard.command;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.ui.Model;
import com.oracle.oMVCBoard.dao.BDao;
public class BDeleteCommand implements BCommand {
@Override
public void execute(Model model) {
Map<String, Object> map = model.asMap();
HttpServletRequest request = (HttpServletRequest) map.get("request");
String bId = request.getParameter("bId");
BDao dao = new BDao();
dao.delete(bId);
}
}
public void delete(String bId) {
Connection conn = null;
PreparedStatement pstmt = null;
System.out.println("BDao delete start...");
try {
conn = dataSource.getConnection();
String sql = "delete from mvc_board where bId=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, bId);
pstmt.executeUpdate();
} catch (Exception e) {
System.out.println("delete dataSource-->"+e.getMessage());
} finally {
try {
if( pstmt!= null ) pstmt.close();
if( conn != null ) conn.close();
} catch (Exception e2) {
System.out.println(e2.getMessage());
}
}
}
@RequestMapping("/write_view")
public String write_view(Model model) {
System.out.println("write_view start...");
return "write_view";
}
@PostMapping(value = "/write")
public String write(HttpServletRequest request, Model model) {
logger.info("write start...");
model.addAttribute("request", request);
command = new BWriteCommand();
command.execute(model);
return "redirect:list";
}
📂 views
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="header.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="write" method="post">
<table width="500" border="1">
<tr>
<td>이름</td>
<td><input type="text" name="bName" size="50"></td>
</tr>
<tr>
<td>제목</td>
<td><input type="text" name="bTitle" size="50"></td>
</tr>
<tr>
<td>내용</td>
<td><textarea rows="10" name="bContent"></textarea> </td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="입력">
<a href="list">목록보기</a></td>
</tr>
</table>
</form>
</body>
</html>
package com.oracle.oMVCBoard.command;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.ui.Model;
import com.oracle.oMVCBoard.dao.BDao;
public class BWriteCommand implements BCommand {
@Override
public void execute(Model model) {
Map<String, Object> map = model.asMap();
HttpServletRequest request = (HttpServletRequest) map.get("request");
String bName = request.getParameter("bName");
String bTitle = request.getParameter("bTitle");
String bContent = request.getParameter("bContent");
BDao dao = new BDao();
dao.write(bName, bTitle, bContent);
}
}
시퀀스는 mvc_board_seq.nextval로 다음걸 찾아가고, mvc_board_seq.currval 현재 번호를 불러온다.
public void write(String bName, String bTitle, String bContent) {
Connection conn = null;
PreparedStatement pstmt = null;
System.out.println("BDao write start...");
try {
conn = dataSource.getConnection();
String sql = "insert into mvc_board(bId, bName, bTitle, bContent, bDate, bHit, bGroup, bStep, bIndent) "
+ "values(mvc_board_seq.nextval,?,?,?,sysdate,0,mvc_board_seq.currval,0,0)";
System.out.println("BDao write sql->"+sql);
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, bName);
pstmt.setString(2, bTitle);
pstmt.setString(3, bContent);
int rn = pstmt.executeUpdate();
} catch (Exception e) {
System.out.println("write dataSource-->"+e.getMessage());
} finally {
try {
if( pstmt!= null ) pstmt.close();
if( conn != null ) conn.close();
} catch (Exception e2) {
System.out.println(e2.getMessage());
}
}
}
@RequestMapping("/reply_view")
public String reply_view(HttpServletRequest request, Model model) {
System.out.println("reply_view start...");
model.addAttribute("request", request);
command = new BReplyViewCommand();
command.execute(model);
return "reply_view";
}
package com.oracle.oMVCBoard.command;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.ui.Model;
import com.oracle.oMVCBoard.dao.BDao;
import com.oracle.oMVCBoard.dto.BDto;
public class BReplyViewCommand implements BCommand {
@Override
public void execute(Model model) {
Map<String, Object> map = model.asMap();
HttpServletRequest request = (HttpServletRequest) map.get("request");
String bId = request.getParameter("bId");
BDao dao = new BDao();
BDto board = dao.reply_view(bId);
model.addAttribute("reply_view", board);
}
}
public BDto reply_view(String str) {
BDto dto = null;
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
System.out.println("BDao reply_view start...");
try {
conn = dataSource.getConnection();
String sql = "select * from mvc_board where bId=?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, Integer.parseInt(str));
rs = pstmt.executeQuery();
if(rs.next()) {
int bId = rs.getInt("bId");
String bName = rs.getString("bName");
String bTitle = rs.getString("bTitle");
String bContent = rs.getString("bContent");
Timestamp bDate = rs.getTimestamp("bDate");
int bHit = rs.getInt("bHit");
int bGroup = rs.getInt("bGroup");
int bStep = rs.getInt("bStep");
int bIndent = rs.getInt("bIndent");
dto = new BDto(bId, bName, bTitle, bContent, bDate, bHit, bGroup, bStep, bIndent);
}
} catch (Exception e) {
System.out.println("BDao reply_view"+e.getMessage());
} finally {
try {
if( rs != null) rs.close();
if( pstmt!= null ) pstmt.close();
if( conn != null ) conn.close();
} catch (Exception e2) {
System.out.println(e2.getMessage());
}
}
return dto;
}
📂 views
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="header.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="reply" method="post">
<input type="hidden" name="bId" value="${reply_view.bId}">
<input type="hidden" name="bGroup" value="${reply_view.bGroup}">
<input type="hidden" name="bStep" value="${reply_view.bStep}">
<input type="hidden" name="bIndent" value="${reply_view.bIndent}">
<table width="500" border="1">
<tr>
<td>번호</td><td>${reply_view.bId}</td>
</tr>
<tr>
<td>히트</td><td>${reply_view.bHit}</td>
</tr>
<tr>
<td>이름</td><td><input type="text" name="bName" value="${reply_view.bName}"></td>
</tr>
<tr>
<td>제목</td><td><input type="text" name="bTitle" value="답변"+"${reply_view.bTitle}"></td>
</tr>
<tr>
<td>내용</td>
<td><textarea rows="10" name="bContent">${reply_view.bContent}</textarea></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="답변저장">
<a href="list">목록</a></td>
</tr>
</table>
</form>
</body>
</html>
@RequestMapping(value = "/reply", method = RequestMethod.POST)
public String reply(HttpServletRequest request, Model model) {
System.out.println("reply()");
model.addAttribute("request", request);
command = new BReplyCommand();
command.execute(model);
return "redirect:list";
}
package com.oracle.oMVCBoard.command;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.ui.Model;
import com.oracle.oMVCBoard.dao.BDao;
public class BReplyCommand implements BCommand {
@Override
public void execute(Model model) {
Map<String, Object> map = model.asMap();
HttpServletRequest request = (HttpServletRequest) map.get("request");
String bId = request.getParameter("bId");
String bName = request.getParameter("bName");
String bTitle = request.getParameter("bTitle");
String bContent = request.getParameter("bContent");
int bGroup = Integer.parseInt(request.getParameter("bGroup"));
int bStep = Integer.parseInt(request.getParameter("bStep"));
int bIndent = Integer.parseInt(request.getParameter("bIndent"));
BDao dao = new BDao();
dao.reply(bId, bName, bTitle, bContent, bGroup, bStep, bIndent);
}
}
public void reply(String bId, String bName, String bTitle, String bContent, int bGroup, int bStep, int bIndent) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = dataSource.getConnection();
String sql = "insert into mvc_board(bId, bName, bTitle, bContent, bDate, bHit, bGroup, bStep, bIndent) "
+ "values(mvc_board_seq.nextval,?,?,?,sysdate,0,?,?,?)";
String sql2 = "update mvc_board set bStep = bStep +1 where bGroup=? and bStep > ?";
if( Integer.parseInt(bId) != 0) {
pstmt = conn.prepareStatement(sql2);
pstmt.setInt(1, bGroup);
pstmt.setInt(2, bStep);
pstmt.executeUpdate();
pstmt.close();
bStep = bStep +1;
bIndent = bIndent +1;
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, bName);
pstmt.setString(2, bTitle);
pstmt.setString(3, bContent);
pstmt.setInt(4, bGroup);
pstmt.setInt(5, bStep);
pstmt.setInt(6, bIndent);
pstmt.executeUpdate();
}
} catch (Exception e) {
System.out.println("BDao reply"+e.getMessage());
} finally {
try {
if( pstmt!= null ) pstmt.close();
if( conn != null ) conn.close();
} catch (Exception e2) {
System.out.println(e2.getMessage());
}
}
}
jsp를 가기 위한 RequestMapping