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

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







writeForm.jsp를 보도록 합시다.
writeForm.jsp
[code]
<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>
[/code]
html:form은 그냥 html태그에 있는 것과 거의 비슷한 역할을 합니다.
마지막 부분에 html:messages는 메시지중에 property값과 같은 메세지가 있으면 출력하라는 뜻입니다.
form의 action은 write.do를 수행하게 됩니다.
[code]
<action path="/write"
    type="simpleboard.actions.writeAction"
    name="DynaForm"
    scope="request"
    validate="true"
    input="/writeForm.jsp">
</action>
[/code]
write.do를 수행하면 writeAction클래스를 수행하고 만약 mapping.getInputForward();을 리턴받게 되면 writeForm.jsp를 다시 수행하라는 뜻입니다. 여기서 저 메시지를 리턴받게 되는 경우는 이름, 제목, 내용, 비번을 쓰지 않은 경우가 됩니다.

writeAction을 보도록 합시다.

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

오늘도 여기까지-_-;

 
Posted by 머드초보
,