티스토리 뷰

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


package com.javalec.spring_pjt_board_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;

}



}





공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/09   »
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
29 30
글 보관함