스프링 20강 - 스프링 MVC 계층형 게시판 (5/5) with 오라클
November 16, 2018
20-1 글 답변 페이지 만들기
- reply_view : 답변을 할 수 있는 Form이 있는 공간
- reply : 실제 답변이 DB와 연동되어 처리되는 공간
BController 작성
@RequestMapping("/reply_view")
public String reply_view(HttpServletRequest request, Model model){
System.out.println("reply_view()");
model.addAttribute("request", request);
command = new BReplyViewCommand();
command.execute(model);
return "reply_view";
}
@RequestMapping("/reply")
public String reply(HttpServletRequest request, Model model) {
System.out.println("reply()");
model.addAttribute("request", request);
command = new BReplyCommand();
command.execute(model);
return "redirect:list";
}
BReplyViewCommand 작성
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 dto = dao.reply_view(bId);
model.addAttribute("reply_view", dto);
}
}
BReplyCommand 작성
public class BReplyCommand implements BCommand {
@Override
public void execute(Model model) {
// TODO Auto-generated method stub
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");
String bGroup = request.getParameter("bGroup");
String bStep = request.getParameter("bStep");
String bIndent = request.getParameter("bIndent");
BDao dao = new BDao();
dao.reply(bId, bName, bTitle, bContent, bGroup, bStep, bIndent);
}
}
BDao 작성
public class BDao {
// 생략
public BDto reply_view(String str) {
// TODO Auto-generated method stub
BDto dto = null;
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connection = dataSource.getConnection();
String query = "select * from mvc_board where bId = ?";
preparedStatement = connection.prepareStatement(query);
preparedStatement.setInt(1, Integer.parseInt(str));
resultSet = preparedStatement.executeQuery();
if(resultSet.next()) {
int bId = resultSet.getInt("bId");
String bName = resultSet.getString("bName");
String bTitle = resultSet.getString("bTitle");
String bContent = resultSet.getString("bContent");
Timestamp bDate = resultSet.getTimestamp("bDate");
int bHit = resultSet.getInt("bHit");
int bGroup = resultSet.getInt("bGroup");
int bStep = resultSet.getInt("bStep");
int bIndent = resultSet.getInt("bIndent");
dto = new BDto(bId, bName, bTitle, bContent, bDate, bHit, bGroup, bStep, bIndent);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
try {
if(preparedStatement != null) preparedStatement.close();
if(connection != null) connection.close();
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
}
return dto;
}
public void reply(String bId, String bName, String bTitle, String bContent, String bGroup, String bStep, String bIndent) {
// TODO Auto-generated method stub
replyShape(bGroup, bStep);
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = dataSource.getConnection();
String query = "insert into mvc_board (bId, bName, bTitle, bContent, bGroup, bStep, bIndent) values (mvc_board_seq.nextval, ?, ?, ?, ?, ?, ?)";
preparedStatement = connection.prepareStatement(query);
preparedStatement.setString(1, bName);
preparedStatement.setString(2, bTitle);
preparedStatement.setString(3, bContent);
preparedStatement.setInt(4, Integer.parseInt(bGroup));
preparedStatement.setInt(5, Integer.parseInt(bStep) + 1);
preparedStatement.setInt(6, Integer.parseInt(bIndent) + 1);
int rn = preparedStatement.executeUpdate();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
try {
if(preparedStatement != null) preparedStatement.close();
if(connection != null) connection.close();
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
}
}
}
참고자료
- 스프링과정20강 블스(김명호 강사)