게시판 만들기 (게시글 쓰기)
Back-End/Spring 2019. 7. 2. 18:18-게시글 쓰기 구현-
1. list.jsp 중 일부
글쓰기 링크 버튼과 스크립트 태그를 추가
1 2 3 4 5 6 7 8 9 10 11 | <script> $(function(){ //아이디가 btnWrite인 버튼을 누르게 되면 write.do 컨트롤러로 맵핑 $("#btnWrite").click(function(){ location.href="${path}/board/write.do"; }); }); </script> ========================================================================= <button type="button" id="btnWrite">글쓰기</button> | cs |
2. BoardController.java 중 일부
list.jsp 페이지에서 맵핑된 write.do가 맵핑된 메소드를 만듦
1 2 3 4 5 6 | @RequestMapping("write.do") public String write() { // 글쓰기 폼 페이지로 이동 return "board/write"; //write.jsp 페이지로 이동 } // write.jsp에서 입력한 내용들이 BoardDTO에 저장됨 | cs |
3. write.jsp
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 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <%@ include file="../include/header.jsp" %> <script src="${path}/include/js/common.js"></script> <!-- ckeditor의 라이브러리 --> <script src="${path}/ckeditor/ckeditor.js"></script> <script> $(function(){ //id가 btnSave인 버튼을 클릭하면 실행되는 구문. //post 방식으로 자료를 insert.do (컨트롤러)로 보낸다. $("#btnSave").click(function(){ document.form1.submit(); }); }); </script> </head> <body> <%@ include file="../include/menu.jsp" %> <h2>글쓰기</h2> <form id="form1" name="form1" method="post" action="${path}/board/insert.do"> <div>제목 <input name="title" id="title" size="80" placeholder="제목을 입력하세요"> <!-- 제목 입력 --> </div> <div style="width:800px;"> 내용 <textarea id="content" name="content" rows="3" cols="80" placeholder="내용을 입력하세요"></textarea><!-- 내용 입력 --> </div> <div> 첨부파일을 등록하세요 <div class="fileDrop"></div> <div id="uploadedList"></div> </div> <div style="width:700px; text-align:center;"> <button type="button" id="btnSave">확인</button> </div> </form> </body> </html> | cs |
write.jsp 에서 입력한 내용들이 dto를 거쳐서 BoardController.java 페이지로 이동함
4. BoardController.java 중 일부
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | // write.jsp에서 입력한 내용들이 BoardDTO에 저장됨 @RequestMapping("insert.do") public String insert(@ModelAttribute BoardDTO dto , HttpSession session) //세션은 아이디를 확인하기위해서 필요하므로 세션을 가져온다. throws Exception { // 로그인한 사용자의 아이디 String writer=(String)session.getAttribute("userid"); dto.setWriter(writer); //로그인한 아이디를 dto에 저장 //레코드 저장 boardService.create(dto); //서비스와 dao를 통해서 레코드에 추가가된다. //게시물 목록으로 이동 return "redirect:/board/list.do"; } | cs |
5. BoardServiceImpl.java 중 일부
1 2 3 4 | @Override public void create(BoardDTO dto) throws Exception { boardDao.create(dto); //dto를 매개값으로 dao를 호출 } | cs |
6. BoardDAOImpl.java 중 일부
1 2 3 4 5 | @Override public void create(BoardDTO dto) throws Exception { sqlSession.insert("board.insert", dto); } | cs |
7. boardMapper.xml 중 일부 (namespace와 id를 맵핑)
1 2 3 4 | <!--다른 mapper과 중복되지 않도록 namespace를 기재--!> <mapper namespace = "board"> <insert id="insert"> //게시글번호는 시퀀스로 하고 (생성할때마다 숫자가 1씩 증가하게함) //제목 //글 내용 //작성자를 삽입함 insert into board (bno,title,content,writer) values ( seq_board.nextval, #{title}, #{content}, #{writer} ) </insert> | cs |
8. 글번호를 시퀀스로 만들기 위해서 board 테이블을 삭제함
1 | delete from board; | cs |
9. 게시물 번호 계산용 시퀀스를 생성
1 2 3 | create sequence seq_board start with 1 increment by 1; | cs |
10. 게시글이 정상적으로 삽입되는지 확인해보기위해서 게시글을 삽입
1 2 | insert into board (bno, title, content, writer) values (seq_board.nextval, '제목', '내용', 'park'); | cs |
11. 생성이 끝난후에는 작업완료를 하기위해 commit을 실시함.
1 | commit; | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <beans:bean id="loginInterceptor" class="com.example.spring02.interceptor.LoginInterceptor" /> ============================================================================================== <!-- 인터셉터 호출을 위한 url mapping --> <!-- write.do나 insert.do를 호출하면 loginInterceptor 한번 거쳐 가라는 뜻--!> <interceptors> <interceptor> <mapping path="/shop/**" /> <beans:ref bean="sampleInterceptor" /> </interceptor> <interceptor> <mapping path="/board/write.do" /> <mapping path="/board/insert.do" /> <mapping path="/board/update.do" /> <mapping path="/board/delete.do" /> <mapping path="/shop/cart/list.do"/> <mapping path="/shop/cart/insert.do"/> <beans:ref bean="loginInterceptor"/> </interceptor> | cs |
LoginInterceptor.java 중 일부
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | //메인 액션이 실행되기 전 @Override public boolean preHandle(HttpServletRequest request , HttpServletResponse response, Object handler) throws Exception { //세션 객체 생성 HttpSession session=request.getSession(); //세션이 없으면(로그인되지 않은 상태) if(session.getAttribute("userid") == null) { //login 페이지로 이동 response.sendRedirect(request.getContextPath() +"/member/login.do?message=nologin"); return false; //메인 액션으로 가지 않음 }else { return true; //메인 액션으로 이동 } } | cs |
'Back-End > Spring' 카테고리의 다른 글
게시판 만들기 (검색기능) (0) | 2019.07.03 |
---|---|
게시판 만들기 (페이지 나누기) (1) | 2019.07.03 |
게시판 만들기 (게시글 목록) (0) | 2019.07.02 |
게시판 만들기 (기본구조, 테이블 생성) (0) | 2019.07.02 |
Spring Editor (CKEditor, SummerNote) 사용 (1) | 2019.07.02 |