티스토리 뷰

1. 게시판 글 내용 수정하기

계속 진행해왔던 부분이라 따로 많은 설명은 하지 않고 넘어 가겠습니다.

1) Controller


// 5. Modify부분 처리

@RequestMapping(method = RequestMethod.POST,value = "/modify")

public String modify(HttpServletRequest request,Model model) {

System.out.println("modify()");

model.addAttribute("request",request);

command = new BModifyCommand();

command.execute(model);

return "redirect:list";

}


2) Command

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 BModifyCommand 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");

BDao dao = new BDao();

dao.modify(bId,bName,bTitle,bContent);


}


}

3) DAO


public void modify(String bId, String bName, String bTitle, String bContent) {

Connection connection = null;

PreparedStatement preparedStatement =null;

try {

connection = dataSource.getConnection();

// 쿼리문 순서대로 setString 순서 결정 된다.

String query ="update mvc_board set bName = ?,bTitle = ?, bContent = ? where bId = ?";

preparedStatement = connection.prepareStatement(query);

preparedStatement.setString(1,bName);

preparedStatement.setString(2,bTitle);

preparedStatement.setString(3,bContent);

preparedStatement.setInt(4,Integer.parseInt(bId));

//반환형이int형 이므로 

int rn = preparedStatement.executeUpdate();

}catch(Exception e) {

}finally {

try {

if(preparedStatement != null) {

preparedStatement.close();

}

if(connection != null) {

connection.close();

}

}catch(Exception e) {

e.printStackTrace();

}

}

4) 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/11   »
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
글 보관함