마지막입니다. 글 삭제하기 기능입니다.

이것도 글 수정하는 거랑 원리가 비슷합니다. 글삭제버튼을 누르게 되면 비밀번호를 입력하라고 나오는데 입력해서 맞으면 삭제하는 식입니다.






글삭제를 누르면 deleteForm.do를 실행합니다. 이것은 deleteForm.jsp를 실행합니다.


<html:form action="delete" method="POST" focus="pwd">
<table border="1" width="250">
<tr bgcolor="#7eaee9">
<td>게시물의 비밀번호를 입력하세요.</td>
</tr>
<tr>
<td>
<html:password property="pwd"/>
<html:hidden property="id" value="<%= request.getParameter("id") %>"/>
<html:submit value="확인"/>
</td>
</tr>
<tr>
<td>
<html:messages id="msg" property="invalidPwd">
<bean:write name="msg"/>
</html:messages>
</td>
</tr>
</table>
</html:form>

pwd를 입력받고 id는 파라메터로 받은 것을 delete.do에 넘겨줍니다.
delete.do를 수행하는 action태그를 보도록 합시다.

struts-config.xml

<action path="/delete"
type="simpleboard.actions.deleteAction"
name="DynaForm"
scope="request"
validate="true"
input="/deleteForm.jsp">
</action>


deleteAction을 수행하며 비밀번호가 맞으면 자바스크립트를 통해 삭제가 성공했다는 메시지를 띄우고 틀리면 deleteForm.jsp를 다시 실행합니다.
deleteAction을 보도록 합시다.

deleteAction.java

public class deleteAction extends Action{
public ActionForward execute( ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception{

DynaActionForm deleteForm = (DynaActionForm)form;
BoardDAO dao = new BoardDAO();
ActionMessages errors = new ActionMessages();

int id = (Integer)deleteForm.get("id");
String pwd = (String)deleteForm.get("pwd");
if (dao.CheckPwd(id, pwd)) {
dao.Delete(id);
response.setContentType("text/html; charset=euc-kr");
PrintWriter out = response.getWriter();
out.println("<script language='javascript'>");
out.println("alert('글이 삭제되었습니다.');");
out.println("location.href = \"list.do\";");
out.println("</script>");
return null;
}
else {
errors.add("invalidPwd",new ActionMessage("error.pwd.invalid"));
saveErrors(request,errors);
return mapping.getInputForward();
}
}
}

id와 pwd를 받고, CheckPwd를 통해서 비밀번호를 체크후에 맞으면 dao의 Delete메소드를 수행한뒤 자바스크립트로 삭제되었다는 메시지를 띄운뒤 list.do로 이동합니다.
비밀번호가 틀리면 에러메시지를 추가해서 input으로 포워드합니다.

자 이제 끝났습니다.
혼자서 삽질한 느낌이 드는군요.
단 한분이라도 도움이 되었으면 좋겠습니다.

머드초보 이 작성.

당신의 의견을 작성해 주세요.

  1. Comment RSS : http://mudchobo.tomeii.com/tt/rss/comment/54
  2. 초보 2008/04/10 19:05  편집/삭제  댓글 작성  댓글 주소

    머드초보님홈피에서 소스코드의 표시효과에 관하여 질문드립니다.
    어덯게 하면 홈페지에 EDIT Tools처럼 번호달린 소스표시가 되는지요.

    그럼 잘 부탁드립니다.

  3. 초보 2008/04/11 16:49  편집/삭제  댓글 작성  댓글 주소

    답변 감사합니다. ^^

  4. 유리망치 2009/04/02 16:17  편집/삭제  댓글 작성  댓글 주소

    플렉스가 아직 먼지도 모르는 초보입니다.
    참고 많이 할게요
    퍼가여

    • 머드초보 2009/04/03 23:11  편집/삭제  댓글 주소

      반갑습니다^^
      저도 아직은 플렉스초보라 ^^
      근데 이 글은 Struts자료군요-_-;

[로그인][오픈아이디란?]


글 수정하는 부분이 가장 구현하기 힘들었던 것 같습니다-_-;

구조는 수정버튼을 클릭했을 때에 id를 리턴받습니다. 그다음에 비밀번호를 입력받는 페이지로 이동합니다. 그 비밀번호를 입력해서 그 해당글의 비밀번호와 일치한지를 확인합니다.
일치하게 되면 수정하는 폼으로 이동합니다. 그 후 수정해서 글수정을 클릭하면 modifyAction을 통해 글을 수정하는 식으로 구현했습니다.


우선 글수정을 클릭했을 때 modifyConfirm.do를 실행합니다. 이것은 단순히 modifyConfirm.jsp를 실행합니다.
modifyConfirm.jsp를 보도록 합시다.

modifyConfirm.jsp


<html:form action="modifyForm" method="POST" focus="pwd">
<table border="1" width="250">
<tr bgcolor="#7eaee9">
<td>게시물의 비밀번호를 입력하세요.</td>
</tr>
<tr>
<td>
<html:password property="pwd"/>
<html:hidden property="id" value="<%= request.getParameter("id") %>"/>
<html:submit value="확인"/>
</td>
</tr>
<tr>
<td>
<html:messages id="msg" property="invalidPwd">
<bean:write name="msg"/>
</html:messages>
</td>
</tr>
</table>

여기서 볼 것은 id값도 넘겨줘야하기 때문에 hidden으로 id값을 처리해서 넘겨줍니다.
저기서도 value값을 그냥 id로 쓰면 안되더라구요. 그래서 getParameter메소드를 통해 넘겨줬습니다.
html:messages태그는 비밀번호가 틀리면 보여지게 되어있습니다.
여기서 확인버튼을 클릭하게 되면 modifyForm.do를 실행하게 됩니다.
modifyForm의 액션을 살펴봅시다.

struts-config.xml

<action path="/modifyForm"
type="simpleboard.actions.modifyformAction"
name="DynaForm"
scope="request"
input="/modifyConfirm.jsp">
<forward name="success" path="/modifyForm.jsp"/>
</action>

modifyformAction을 수행하고 input으로 포워드하면 다시 modifyConfirm.jsp를 수행합니다.
성공으로 매핑하게 되면 modifyForm.jsp 즉 수정창으로 가는 겁니다.

modifyformAction클래스를 보도록 합시다.

modifyformAction.java

public class modifyformAction extends Action {

public ActionForward execute( ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception{

DynaActionForm modifyconfirmForm = (DynaActionForm)form;
ActionMessages errors = new ActionMessages();
Board board = new Board();
BoardDAO dao = new BoardDAO();
HttpSession session = request.getSession();

int id = (Integer)modifyconfirmForm.get("id");
String pwd = (String)modifyconfirmForm.get("pwd");

if (dao.CheckPwd(id, pwd)) {
board = dao.findBoard(id);
session.setAttribute("board", board);
return mapping.findForward("success");
}
else {
errors.add("invalidPwd",new ActionMessage("error.pwd.invalid"));
saveErrors(request,errors);
return mapping.getInputForward();
}
}
}

form에 있는 내용을 받아서 우선 dao의 CheckPwd를 이용해서 체크해서 비밀번호가 맞으면 현재보드내용을 찾아서 session에 저장 후 success로 포워드합니다.
비밀번호가 틀린경우에는 메시지를 추가해서 Input으로 포워드합니다.

성공했을 경우 modifyForm.jsp로 갑니다. 살펴봅시다.

modifyForm.jsp

<bean:define id="id" name="board" property="id" type="java.lang.Integer"/>
<bean:define id="name" name="board" property="name" type="java.lang.String"/>
<bean:define id="pwd" name="board" property="pwd" type="java.lang.String"/>
<bean:define id="title" name="board" property="title" type="java.lang.String"/>
<bean:define id="content" name="board" property="content" type="java.lang.String"/>

<html:form action="modify" method="POST" focus="name">
<html:hidden property="id" value="<%= id.toString() %>"/>
<table width="500" border="1">
<tr>
<td width="100" bgcolor="#7eaee9">등 록 자</td>
<td width="180" align="left">&nbsp;
<html:text property="name" size="10"
maxlength="10" value="<%= name %>"/>
</td>
<td width="100" bgcolor="#7eaee9">비밀번호</td>
<td width="120" align="left">&nbsp;
<html:password property="pwd" size="10"
maxlength="10" value="<%= pwd %>"/>
</td>
</tr>
<tr>
<td bgcolor="#7eaee9">제 목</td>
<td colspan="4" align="left">&nbsp;
<html:text property="title" size="30"
maxlength="30" value="<%= title %>"/>
</td>
</tr>
<tr>
<td bgcolor="#7eaee9">내 용</td>
<td colspan="4" align="left">&nbsp;
<html:textarea property="content"
cols="53" rows="10" value="<%= content %>"/>
</td>
</tr>
<tr>
<td colspan="4"><font color="red"><b>
<html:messages id="msg" property="requiredName">
<bean:write name="msg"/><br>
</html:messages>
<html:messages id="msg" property="requiredPwd">
<bean:write name="msg"/><br>
</html:messages>
<html:messages id="msg" property="requiredTitle">
<bean:write name="msg"/><br>
</html:messages>
<html:messages id="msg" property="requiredContent">
<bean:write name="msg"/><br>
</html:messages></b></font>
</td>
</tr>
<tr>
<td colspan="4"><html:submit value="내용수정"/></td>
</tr>
</table>
</html:form>

value에다가 값을 넣으려면 bean:define을 통해서 값을 정의해야합니다. 그 후에 jsp코드를 이용해서 value에 값을 넣어야지 값이 들어갑니다. 다른방법이 있을 것 같은데 이렇게 밖에 못하겠군요-_-;
form은 modify.do를 실행합니다. modifyAction을 실행합니다.
modifyAction은 writeAction과 거의 동일합니다. 그냥 글을 써서 등록하는 형식으로 하고 다른 점이 있다면 dao의 modify를 호출해서 update를 이용하고, insert는 insert sql문을 이용해서 합니다.

이렇게하면 수정하기가 끝이납니다.

오늘도 여기까지-_-;

머드초보 이 작성.

당신의 의견을 작성해 주세요.

[로그인][오픈아이디란?]


이번에는 글쓰기 부분을 설명해보겠습니다.

글쓰기를 실행할 때에는 writeForm.do를 실행하게 됩니다. writeForm.jsp로 포워드합니다.
<action path="/writeForm" forward="/writeForm.jsp"/>







writeForm.jsp를 보도록 합시다.
writeForm.jsp


<table width="500" border="1">
<tr>
<td width="100" bgcolor="#7eaee9">등 록 자</td>
<td width="180" align="left">&nbsp;
<html:text property="name" size="10" maxlength="10"/></td>
<td width="100" bgcolor="#7eaee9">비밀번호</td>
<td width="120" align="left">&nbsp;
<html:password property="pwd" size="10" maxlength="10"/></td>
</tr>
<tr>
<td bgcolor="#7eaee9">제 목</td>
<td colspan="4" align="left">&nbsp;
<html:text property="title" size="30" maxlength="30"/></td>
</tr>
<tr>
<td bgcolor="#7eaee9">내 용</td>
<td colspan="4" align="left">&nbsp;
<html:textarea property="content" cols="53" rows="10"/></td>
</tr>
<tr>
<td colspan="4"><font color="red"><b>
<html:messages id="msg" property="requiredName">
<bean:write name="msg"/><br>
</html:messages>
<html:messages id="msg" property="requiredPwd">
<bean:write name="msg"/><br>
</html:messages>
<html:messages id="msg" property="requiredTitle">
<bean:write name="msg"/><br>
</html:messages>
<html:messages id="msg" property="requiredContent">
<bean:write name="msg"/><br>
</html:messages></b></font>
</td>
</tr>
<tr>
<td colspan="4"><html:submit value="글쓰기"/></td>
</tr>
</table>

html:form은 그냥 html태그에 있는 것과 거의 비슷한 역할을 합니다.
마지막 부분에 html:messages는 메시지중에 property값과 같은 메세지가 있으면 출력하라는 뜻입니다.
form의 action은 write.do를 수행하게 됩니다.

<action path="/write"
type="simpleboard.actions.writeAction"
name="DynaForm"
scope="request"
validate="true"
input="/writeForm.jsp">
</action>

write.do를 수행하면 writeAction클래스를 수행하고 만약 mapping.getInputForward();을 리턴받게 되면 writeForm.jsp를 다시 수행하라는 뜻입니다. 여기서 저 메시지를 리턴받게 되는 경우는 이름, 제목, 내용, 비번을 쓰지 않은 경우가 됩니다.

writeAction을 보도록 합시다.

writeAction.java

public class writeAction extends Action {

public ActionForward execute( ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception{

DynaActionForm writeForm = (DynaActionForm)form;
ActionMessages errors = new ActionMessages();
Board board = new Board();
BoardDAO dao = new BoardDAO();
int errorCount = 0;

board.setName((String)writeForm.get("name"));
board.setPwd((String)writeForm.get("pwd"));
board.setTitle((String)writeForm.get("title"));
board.setContent((String)writeForm.get("content"));

if (board.getName().equals("")){
errors.add("requiredName",new ActionMessage("error.name.required"));
errorCount++;
}

if (board.getPwd().equals("")){
errors.add(
"requiredPwd",new ActionMessage("error.pwd.required"));
errorCount++;
}

if (board.getTitle().equals("")){
errors.add(
"requiredTitle",new ActionMessage("error.title.required"));
errorCount++;
}
if (board.getContent().equals("")){
errors.add(
"requiredContent",new ActionMessage("error.content.required"));
errorCount++;
}

if (errorCount > 0){
saveErrors(request,errors);
return mapping.getInputForward();
}
dao.Insert(board);

response.setContentType("text/html; charset=euc-kr");
PrintWriter out = response.getWriter();
out.println("<script language='javascript'>");
out.println("alert('글쓰기에 성공했습니다.');");
out.println("location.href = \"list.do\";");
out.println("</script>");

return null;
}
}

폼으로부터 값을 받고 값이 비었으면 에러메시지를 추가하여 에러메시지가 한개라도 있으면 다시 writeForm.jsp를 실행하도록 input으로 포워드합니다.
제대로 입력이 되었으면 dao의 Insert메소드를 실행해서 삽입후에 자바스크립트코드를 보여주기 위해서 out객체를 이용해서 확인창 하나띄운뒤 list.do로 가도록 합니다. 이 때 액션으로 이동하는것이 아니라 자바스크립트 코드로 이동하도록 했습니다.
out객체를 이용해서 자바스크립트코드를 적었는데 return을 forward값으로 리턴해버리면 자바스크립트가 그냥 씹혀버립니다-_-;(이유는 모르겠습니다-_-;)

오늘도 여기까지-_-;

머드초보 이 작성.

당신의 의견을 작성해 주세요.

  1. Comment RSS : http://mudchobo.tomeii.com/tt/rss/comment/52
  2. 김성운 2008/02/01 14:20  편집/삭제  댓글 작성  댓글 주소

    return을 forward값으로 리턴 하면 자바스크립트가 안먹히는 이유가요..

    out.flush(); <-- 이부분이 없어서 그런거 같아요..

    직접적으로 HTML에 자바스크립트를 쓰는 부분이 없어서.

    다른분들이 보시고 참고 하셨으면 하네욤 ㅋ

  3. 지멋대루 2008/02/12 18:26  편집/삭제  댓글 작성  댓글 주소

    와우!!!!!!!!!!!!!!!!!!

    엉뚱한키워드로 들어오게됬는데 제가 원하던게 딱!!!있군요~ 오호!!

    감사히 봤습니다^^

[로그인][오픈아이디란?]
« Prev : 1 : 2 : 3 : 4 : 5 : ... 6 : Next »