--------------------------------------------/WEB-INF/CommandPro.properties 추가
/mvc2bbs/updateForm.do=my.action.UpdateFormAction
------------------------------------------------------------UpdateFormAction.java
package my.action;
import java.sql.ResultSet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import my.board.BoardDBBean;
import my.board.BoardDataBean;
public class UpdateFormAction implements CommandAction {
@Override
public String requestPro(HttpServletRequest request,
HttpServletResponse response) throws Throwable
{
BoardDataBean article = null;
int num=Integer.parseInt(request.getParameter("num"));
String pageNum=request.getParameter("pageNum");
BoardDBBean dbPro= BoardDBBean.getInstance();
article = dbPro.updateGetArticle(num);
request.setAttribute("pageNum", new Integer(pageNum));
request.setAttribute("article", article);
return "updateForm.jsp"; //해당뷰
}
}//End
------------------------------------------------------------BoardDBBean.java
public BoardDataBean updateGetArticle(int num) throws Exception
{
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
BoardDataBean article=null;
try {
conn=getConnection();
String sql=" Select * From board Where num=? ";
pstmt=conn.prepareStatement(sql);
pstmt.setInt(1, num);
rs = pstmt.executeQuery();
if(rs.next()){
article = new BoardDataBean();
article.setNum(rs.getInt("num"));
article.setWriter(rs.getString("writer"));
article.setEmail(rs.getString("email"));
article.setSubject(rs.getString("subject"));
article.setPasswd(rs.getString("passwd"));
article.setReg_date(rs.getTimestamp("reg_date"));
article.setReadcount(rs.getInt("readcount"));
article.setRef(rs.getInt("ref"));
article.setRe_step(rs.getInt("re_step"));
article.setRe_level(rs.getInt("re_level"));
article.setContent(rs.getString("content"));
article.setIp(rs.getString("ip"));
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(rs!=null) try { rs.close(); } catch(SQLException e){}
if(pstmt!=null) try { pstmt.close(); } catch(SQLException e){}
if(conn!=null) try { conn.close(); } catch(SQLException e){}
}
return article;
}//updateGetArticle
------------------------------------------------------------updateForm.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@include file="/view/color.jspf" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>게시판</title>
</head>
<body bgcolor="${bodyback_c }">
<center><b>글 수정</b></center>
<br/>
<form method="post"
name="writeform"
action="/mvc2bbs/updatePro.do?pageNum=${pageNum}"
onSubmit="return writeSave()" >
<table width="400" border="1" cellspacing="0" cellpadding="0" align="center">
<tr>
<td width="70" bgcolor="${value_c }" align="center">이름</td>
<td align="left" width="330" >
<input type="text" size="10" maxlength="10" name ="writer" value="${article.writer }">
<input type="hidden" name ="num" value="${article.num }">
</td>
</tr>
<tr>
<td width="70" bgcolor="${value_c }" align="center">제목</td>
<td align="left" width="330" >
<input type="text" size="40" maxlength="50" name ="subject" value="${article.subject}">
</td>
</tr>
<tr>
<td width="70" bgcolor="${value_c } " align="center">이메일</td>
<td align="left" width="330" >
<input type="text" size="40" maxlength="30" name ="email" value="${article.email}">
</td>
</tr>
<tr>
<td width="70" bgcolor="${value_c }" align="center">내용</td>
<td align="left" width="330" >
<textarea name ="content" rows="13" cols="40" >${article.content }</textarea>
</td>
</tr>
<tr>
<td width="70" bgcolor="${value_c } " align="center">비밀번호</td>
<td align="left" width="330" >
<input type="password" size="8" maxlength="12" name ="passwd" >
</td>
</tr>
<tr>
<td colspan=2 bgcolor="${value_c }" align="center">
<input type="submit" value="글수정">
<input type="reset" value="다시작성">
<input type="button" value="목록"
onClick="document.location.href='/mvc2bbs/list.do?pageNum=${pageNum }'">
</td>
</tr>
</table>
</form>
</body>
</html>
--------------------------------------------/WEB-INF/CommandPro.properties 추가
/mvc2bbs/updatePro.do=my.action.UpdateProAction
------------------------------------------------------------UpdateProAction.java
package my.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import my.board.BoardDBBean;
import my.board.BoardDataBean;
public class UpdateProAction implements CommandAction {
@Override
public String requestPro(HttpServletRequest request, HttpServletResponse response)
throws Throwable
{
request.setCharacterEncoding("euc-kr");
String pageNum = request.getParameter("pageNum");
BoardDataBean article=new BoardDataBean();
article.setNum(Integer.parseInt(request.getParameter("num")));
article.setWriter(request.getParameter("writer"));
article.setEmail(request.getParameter("email"));
article.setSubject(request.getParameter("subject"));
article.setContent(request.getParameter("content"));
article.setPasswd(request.getParameter("passwd"));
BoardDBBean dbPro=BoardDBBean.getInstance();
int check = dbPro.updateArticle(article);
request.setAttribute("pageNum", new Integer(pageNum));
request.setAttribute("check", new Integer(check));
return "updatePro.jsp";
}
}//End
------------------------------------------------------------BoardDBBean.java추가
public int updateArticle(BoardDataBean article) throws Exception
{
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
String dbpasswd="";
String sql="";
int x=-1;
try{
conn=getConnection();
pstmt=conn.prepareStatement("select passwd from board where num=?");
pstmt.setInt(1, article.getNum());
rs = pstmt.executeQuery();
if(rs.next()){
dbpasswd = rs.getString("passwd");
if(dbpasswd.equals(article.getPasswd())) {
sql=" Update board Set writer=?, email=?, subject=?, passwd=? ";
sql+= " ,content=? Where num=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, article.getWriter());
pstmt.setString(2, article.getEmail());
pstmt.setString(3, article.getSubject());
pstmt.setString(4, article.getPasswd());
pstmt.setString(5, article.getContent());
pstmt.setInt(6, article.getNum());
pstmt.executeUpdate();
x=1;
}else{
x=0;
}
}//if
}catch(Exception e){
e.printStackTrace();
}finally{
if(rs!=null) try { rs.close(); } catch(SQLException e){}
if(pstmt!=null) try { pstmt.close(); } catch(SQLException e){}
if(conn!=null) try { conn.close(); } catch(SQLException e){}
}
return x;
}//updateArticle
------------------------------------------------------------updateProjsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:if test="${check==1 }">
<meta http-equiv="Refresh" content="0;url=/mvc2bbs/list.do?pageNum=${pageNum}" >
</c:if>
<c:if test="${check==0 }">
비밀번호가 다릅니다.
<br/>
<a href="javascript:history.go(-1)">[글 수정 폼으로 돌아가기]</a>
</c:if>
'..열심히 공부하세.. > MVC' 카테고리의 다른 글
[13] Struts1 기본문법 (0) | 2012.07.03 |
---|---|
[12] MVC2패턴 게시판 - ⑥ 삭제 (0) | 2012.06.30 |
[10] MVC2패턴 게시판 - ④ 상세보기 (0) | 2012.06.28 |
[09] MVC2패턴 게시판 - ③ 목록 (0) | 2012.06.28 |
[08] 데이터베이스 커넥션 풀 - ② (0) | 2012.06.26 |