본문 바로가기
매일코딩/Spring

23.스프링프로젝트 - board write

by 인생여희 2016. 11. 17.
반응형

logincheck page include 



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>    
    
<c:if test="${sessionScope.id==null }">
 
    <script>
    
        alert("로그인 하신 후 사용하시기 바랍니다.");
        location.href="${path}/member/login";
    </script>
 
 
</c:if>    
    
    
cs


1. write page


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
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%-- <%@ page session="false"%> --%>
<c:set var="path" value="${pageContext.request.contextPath}"></c:set> 
 
 
 
<!-- 세션값 체크가 필요한 곳에 삽입 -->
 
 
    <%@ include file="../include/sessionCheck.jsp"%>
 
<script type="text/javascript" src="${path}/ckeditor/ckeditor.js">
</script>    
    
<jsp:include page="boardHeader.jsp" flush="false"/>
 
 
                    <h1 id="product"><div>COMMUNITY</div></h1>
          <article>
              <h2>글쓰기</h2>
 
    
    <!-- 개발 경로가 아닌 실제 경로 -->
<%--  <%= application.getRealPath("/upload") %> --%>
    
 
<!-- for fileuplaod enctype= -->
    <form name="form1" method="post" enctype="mulstipart/form-data" action="${path}/board/write.do">
        <table border="1" width="650px" align="center">
        
            <tr>
                <td align="center">제목</td>
                <td><input name="subject" size="80"></td>
            </tr>
                <td align="center">내용</td>
                <td><textarea rows="5" cols="80" name="content" id="content"></textarea>
                
                
                <script>
                //textarea의 id값과 일치해야함//textarea의 id값과 일치해야함
                CKEDITOR.replace('content',{
                    filebrowserUploadUrl: '${pageContext.request.contextPath}/imageUpload.do'
                    
                    });
                </script>
                
                
                
                </td>
 
            <tr>
                <td align="center">첨부파일</td>
                <td><input type="file" name="file1"></td>
 
            </tr>
 
            <tr>
                <td colspan="2" align="center">
                    <input type="submit" value="작성하기">
                </td>
            </tr>
        </table>
    </form>
 
</article>
 
    <jsp:include page="boardSidebar.jsp" flush="false"/>
 
          <jsp:include page="boardFooter.jsp" flush="false"/>
cs




2.controller



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
   @RequestMapping("board/write.do")
    public String write(@ModelAttribute BoardDTO dto, HttpSession session, Model model) {
      
 
        if (dto.getSubject() == null) {
            return "board/write";
        } else {
           
           
           //if there is value , fileupload + insert table
            String filename = "";
            long filesize = 0;
            MultipartFile file1 = dto.getFile1();
 
            if (!file1.isEmpty()) {  //if there is value
 
                filename = file1.getOriginalFilename();
                filesize = file1.getSize(); 
              
              
                // can make web-inf under 
                
                String uploadDir = "d:\\upload\\";
 
                try {
 
                    new File(uploadDir).mkdir();  // make directory
                   
                    file1.transferTo(new File(uploadDir + filename));
 
                } catch (Exception e) {
 
                    e.printStackTrace();
 
                }
 
            }
           
 
            String id = (String) session.getAttribute("id");
            dto.setUserid(id);
 
            
            dto.setFilename(filename);
            dto.setFilesize(filesize);
 
            boardService.boardInsert(dto);
 
            
            return "redirect:/board/board_list.do";
 
        }
 
    }
 
cs



service

1
2
3
4
5
6
7
    // 湲��벐湲�+ �뙆�씪�뾽濡쒕뱶
    @Override
    public void boardInsert(BoardDTO dto) {
        boardDAO.boardInsert(dto);
 
    }
 
cs

 

dao


1
2
3
4
5
6
7
8
9
10
11
12
 @Override
    public void boardInsert(BoardDTO dto) {
 
        try {
            sqlSession.insert("boardInsert", dto);
        } catch (Exception e) {
 
            e.printStackTrace();
 
        }
 
    }
cs



mapper


1
2
3
4
5
6
7
8
9
10
11
12
    <insert id="replyInsert">
 
        insert into board(idx, userid, subject,content, ref,
        depth, reorder)
        values(
        (select nvl(max(idx)+1,1) from
        board),#{userid},
        #{subject},
        #{content},#{ref}, #{depth}, #{reorder}
 
        )
    </insert>
cs




servlet-context.xml

1
2
3
4
5
6
    <!-- 파일 업로드를 위한 bean 등록 -->
    <beans:bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 파일 업로드 최대 용량(바이트) -->
        <beans:property name="maxUploadSize" value="10485760"></beans:property>
    </beans:bean>
cs



pom.xml


1
2
3
4
5
6
7
8
9
        <!-- 파일업로드를 위한 라이브러리 추가 -->
 
        <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.2</version>
        </dependency>
 
cs


반응형

댓글