티스토리 뷰
1. 게시판 응답하기
1) Controller
@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";
}
//7.답변응답 부분
@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";
}
2) Command(reply)
package com.javalec.spring_pjt_board_command;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.ui.Model;
import com.javalec.spring_pjt_board_dao.BDao;
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);
}
}
3) Command(reply_view)
package com.javalec.spring_pjt_board_command;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.ui.Model;
import com.javalec.spring_pjt_board_dao.BDao;
import com.javalec.spring_pjt_board_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 dto = dao.reply_view(bId);
model.addAttribute("reply_view", dto);
}
}
4) DAO
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
Connection connection = null;
PreparedStatement preparedStatement = null;
replyShape(bGroup, bStep);
try {
connection = dataSource.getConnection();
//String query = "insert into mvc_board (bId, bName, bTitle, bContent, bHit, bGroup, bStep, bIndent) values (?,?,?,?,?,?,?,?)";
String query = "insert into mvc_board (bId, bName, bTitle, bContent, bGroup, bStep, bIndent) values ("+(cnt_res++)+", ?, ?, ?, ?, ?, ?)";
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
}finally {
try {
if(preparedStatement != null) {
preparedStatement.close();
}
if(connection != null) {
connection.close();
}
} catch (Exception e2) {
// TODO: handle exception
}
}
}
private void replyShape( String strGroup, String strStep) {
// TODO Auto-generated method stub
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = dataSource.getConnection();
String query = "update mvc_board set bStep = bStep + 1 where bGroup = ? and bStep > ?";
preparedStatement = connection.prepareStatement(query);
preparedStatement.setInt(1, Integer.parseInt(strGroup));
preparedStatement.setInt(2, Integer.parseInt(strStep));
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();
}
}
}
5) DTO
import java.sql.Timestamp;
// Database의 데이터->객체로 바꿔주는 부분
public class BDto {
int bId;
String bName;
String bTitle;
String bContent;
Timestamp bDate;
int bHit;
int bGroup;
int bStep;
int bIndent;
// 파라미터 없는 Structure
public BDto() {
// TODO Auto-generated constructor stub
}
// 파라미터 있는 Structrue
public BDto(int bId,String bName,String bTitle,String bContent,Timestamp bDate,int bHit,int bGroup,
int bStep,int bIndent) {
// TODO Auto-generated constructor stub
this.bId = bId;
this.bName = bName;
this.bTitle = bTitle;
this.bDate = bDate;
this.bContent = bContent;
this.bHit = bHit;
this.bGroup = bGroup;
this.bStep = bStep;
this.bIndent = bIndent;
}
public int getbId() {
return bId;
}
public void setbId(int bId) {
this.bId = bId;
}
public String getbName() {
return bName;
}
public void setbName(String bName) {
this.bName = bName;
}
public String getbTitle() {
return bTitle;
}
public void setbTitle(String bTitle) {
this.bTitle = bTitle;
}
public String getbContent() {
return bContent;
}
public void setbContent(String bContent) {
this.bContent = bContent;
}
public Timestamp getbDate() {
return bDate;
}
public void setbDate(Timestamp bDate) {
this.bDate = bDate;
}
public int getbHit() {
return bHit;
}
public void setbHit(int bHit) {
this.bHit = bHit;
}
public int getbGroup() {
return bGroup;
}
public void setbGroup(int bGroup) {
this.bGroup = bGroup;
}
public int getbStep() {
return bStep;
}
public void setbStep(int bStep) {
this.bStep = bStep;
}
public int getbIndent() {
return bIndent;
}
public void setbIndent(int bIndent) {
this.bIndent = bIndent;
}
}
'Spring' 카테고리의 다른 글
[Spring] JDBC 활용하기 (0) | 2018.03.22 |
---|---|
[Spring] MVC - 게시판 만들기 6(게시판 글 삭제하기) (0) | 2018.03.15 |
[Spring] MVC - 게시판 만들기 5(게시판 글 내용 수정하기) (0) | 2018.03.15 |
[Spring] MVC - 게시판 만들기 4(글 내용 보기) (0) | 2018.03.15 |
[Spring] mysql DateTime Type Error (0) | 2018.03.14 |
- Total
- Today
- Yesterday
- 코드엔진
- 학교
- 스프링
- Android
- node
- node.js
- Spring
- Controller
- 안드로이드
- C언어
- TensorFlow
- MVC
- 개발하는 관광이
- 감자개발자
- 머신러닝
- 백준알고리즘
- C langauge
- 텐서플로우
- 프로그래밍
- 알고리즘
- 노드
- BFS
- 리버싱
- db
- Algorigm
- 초보자를 위한 C언어 300제
- programming
- 백준
- 감자코딩
- 복습
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |