자바/스프링

[Spring]게시판 작성 boardWriter

SimpleU 2020. 3. 14. 18:10

1 ) 게시판 작성 Controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//게시판 작성하기
    @RequestMapping(value = "/boardWrite")
    public String boardWrite(HttpServletRequest request, HttpServletResponse response) throws Exception {
        
        return "board/boardWrite";
    }
 
    
    @RequestMapping(value = "/insertBoard")
    @ResponseBody
    public BoardDto insertBoard(HttpServletRequest request, HttpServletResponse response, BoardForm boardForm) throws Exception {
 
        BoardDto boardDto = boardService.insertBoard(boardForm);
 
        return boardDto;
    }
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 

@RequestMapping - 요청 URL을 어떤 메소드가 처리할 지 여부를 결정

@ReponseBody - 자바 객체를 HTTP 응답 몸체로 전송

                     - 자바 객체를 HTTP 요청의 body 내용으로 매핑하는 역할

 

2 ) 게시판 작성 Dao

1
2
3
4
    public int insertBoard(BoardForm boardForm) throws Exception {
        return sqlSession.insert(NAMESPACE + ".insertBoard", boardForm);
    }
    
 

 

3 ) 게시판 작성 Dto

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    int board_seq; // 게시글 번호
    int board_re_ref; // 게시글 그룹 번호
    int board_re_lev; // 게시글 글의 깊이
    int board_re_seq; // 게시글 글의 순서
    String board_writer; // 작성자
    String board_subject; // 제목
    String board_content; // 내용
    int board_hits; // 조회수
    String del_yn; // 삭제유무
    String ins_user_id; // Insert 사용자 아이디
    String ins_date; // Insert 날짜
    String upd_user_id; // Update 사용자 아이디
    String upd_date; // Update 날짜
    String result;
 
 

 

4 ) 게시판 작성 Service

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
    /* 게시판 게시글 작성 */
    public BoardDto insertBoard(BoardForm boardForm) throws Exception {
 
        BoardDto boardDto = new BoardDto();
 
        int insertCnt = 0;
 
        int boardReRef = boardDao.getBoardReRef(boardForm);
        boardForm.setBoard_re_ref(boardReRef);
 
        insertCnt = boardDao.insertBoard(boardForm);
 
        List<BoardFileForm> boardFileList = getBoardFileInfo(boardForm);
        for (BoardFileForm boardFileForm : boardFileList) {
            boardDao.insertBoardFile(boardFileForm);
        }
 
        if (insertCnt > 0) {
            boardDto.setResult("SUCCESS");
        } else {
            boardDto.setResult("FAIL");
        }
 
        return boardDto;
    }
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 

 

5 ) 게시판 작성 mapper

select last_insert_id() -  last_insert_id 함수는 테이블의 마지막 auto_increment 값을 리턴

 

6 ) 게시판 작성 view boardWrite

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>게시판 작성</title>
 
<!-- 공통 CSS -->
<link rel="stylesheet" type="text/css" href="/css/common/common.css"/>
 
<!-- 공통 JavaScript -->
<script type="text/javascript" src="/js/common/jquery.js"></script>
<script type="text/javascript" src="/js/common/jquery.form.js"></script>
<script type="text/javascript">
    
    $(document).ready(function(){        
        
    });
        
    /** 게시판 - 목록 페이지 이동 */
    function goBoardList(){                
        location.href = "/board/boardList";
    }
    
    /** 게시판 - 작성  */
    function insertBoard(){
        //board의 제목과 내용을 불러옴
        var boardSubject = $("#board_subject").val();
        var boardContent = $("#board_content").val();
            
        if (boardSubject == ""){            
            alert("제목을 입력해주세요.");
            $("#board_subject").focus();
            return;
        }
        
        if (boardContent == ""){            
            alert("내용을 입력해주세요.");
            $("#board_content").focus();
            return;
        }
            
        var yn = confirm("게시글을 등록하시겠습니까?");        
        if(yn){
                
            var filesChk = $("input[name='files[0]']").val();
            if(filesChk == ""){
                $("input[name='files[0]']").remove();
            }
            
            $("#boardForm").ajaxForm({
            
                url        : "/board/insertBoard",
                enctype    : "multipart/form-data",
                cache   : false,
                async   : true,
                type    : "POST",                         
                success : function(obj) {
                    insertBoardCallback(obj);                
                },           
                error     : function(xhr, status, error) {}
                
            }).submit();             
        }
    }
    
    /** 게시판 - 작성 콜백 함수 */
    function insertBoardCallback(obj){
    
        if(obj != null){        
            
            var result = obj.result;
            
            if(result == "SUCCESS"){                
                alert("게시글 등록을 성공하였습니다.");                
                goBoardList();                 
            } else {                
                alert("게시글 등록을 실패하였습니다.");    
                return;
            }
        }
    }
    
</script>
</head>
<body>
<div id="wrap">
    <div id="container">
        <div class="inner">        
            <h2>게시글 작성</h2>
            <form id="boardForm" name="boardForm" action="/board/insertBoard" enctype="multipart/form-data" method="post" onsubmit="return false;">
                <table width="100%" class="table02">
                <caption><strong><span class="t_red">*</span> 표시는 필수입력 항목입니다.</strong></caption>
                    <colgroup>
                        <col width="20%">
                        <col width="*">
                    </colgroup>
                    <tbody id="tbody">
                        <tr>
                            <th>제목<span class="t_red">*</span></th>
                            <td><input id="board_subject" name="board_subject" value="" class="tbox01"/></td>
                        </tr>
                        <tr>
                            <th>작성자<span class="t_red">*</span></th>
                            <td><input id="board_writer" name="board_writer" value="" class="tbox01"/></td>
                        </tr>
                        <tr>
                            <th>내용<span class="t_red">*</span></th>
                            <td><textarea id="board_content" name="board_content" cols="10" rows="5" class="textarea01"></textarea></td>
                        </tr>
                        <tr>
                            <th scope="row">첨부파일</th>
                            <td><input type="file" id="files[0]" name="files[0]" value=""></td>
                        </tr>
                    </tbody>
                </table>
            </form>
            <div class="btn_right mt15">
                <button type="button" class="btn black mr5" onclick="javascript:goBoardList();">목록으로</button>
                <button type="button" class="btn black" onclick="javascript:insertBoard();">등록하기</button>
            </div>
        </div>
    </div>
</div>
</body>
</html>
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 

 

7 ) 게시판 작성 결과 화면