ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [SPRING] SPRING 게시판 (6) - CRUD [게시물 상세보기]
    JAVA/SPRING 2020. 3. 16. 16:15
    728x90

    Mapper 작성

    // mappers/BoardMapper.xml
    
    ...
    	<!-- 추가 -->
    	<select id="detail" resultType="com.ms.board.Vo.BoardVo">
    		SELECT * FROM board WHERE bno = #{bno}
    	</select>

     

    Dao 작성

    // Dao/BoardDao.java
    
    ...
    
    	//추가
    	public BoardVo detailPosts(int bno) throws Exception{
    		
    		return sqlSession.selectOne(Namespace + ".detail", bno);
    	}
    	

     

    테스트 케이스 작성

    // test/boardDaoTest.java
    
    ...
    
    	//추가
    	@Test
    	public void testDetailPosts() throws Exception{
    		
    		int bno = 2;
    		BoardVo post = boardDao.detailPosts(bno);
    		
    		logger.info(post.getTitle());
    	}

     

    Service 작성

    // Service/BoardService.java
    
    ...
    
    	//추가
    	public BoardVo detailPosts(int bno) throws Exception{
    		
    		return boardDao.detailPosts(bno);
    	}

     

    Controller 작성

     

    // Controller/BoardController.java
    
    ...
    
    	//추가
    	//게시글 조회
    	@RequestMapping(value="/posts/{bno}", method=RequestMethod.GET)
    	public ModelAndView detail(@PathVariable int bno) throws Exception{
    		
    		BoardVo post = boardService.detailPosts(bno);
    		
    		ModelAndView model = new ModelAndView();
    		model.setViewName("detail");
    		model.addObject("post", post);
    		
    		return model;
    	}

     

    View 작성

    <!-- views/index.jsp -->
    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <!DOCTYPE html>
    <html lang="en" dir="ltr">
      <head>
        <meta charset="utf-8">
        <title></title>
        <style>
          #list{
            border: 1px solid black;
          }
          th, td {
            border: 1px solid #444444;
          }
        </style>
      </head>
      <body>
        <table id="list">
          <thead>
            <tr>
              <th>NO</th>
              <th>TITLE</th>
              <th>WRITER</th>
              <th>COUNT</th>
              <th>DATE</th>
            </tr>
          </thead>
          <tbody>
            <c:forEach var="item" items="${boardList}">
              <tr>
                <td>${item.bno}</td>
                <td><a href="/board/${item.bno}">${item.title}</a></td>
                <td>${item.writer}</td>
                <td>${item.count}</td>
                <td>${item.createTime}</td>
              </tr>
            </c:forEach>
          </tbody>
        </table>
    
        <button type="button" id="write">WRITE</button>
      </body>
    </html>
    
    <!-- Jquery -->
    <script src="https://code.jquery.com/jquery-3.4.1.js" integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU=" crossorigin="anonymous"></script>
    <script>
      //게시글 작성 페이지 이동
      $('#write').click(function(){
        location.href = "/board/posts";
      })
    </script>
    
    <!-- views/detail.jsp -->
    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <!DOCTYPE html>
    <html lang="en" dir="ltr">
      <head>
        <meta charset="utf-8">
        <title></title>
        <style>
          #list{
            border: 1px solid black;
          }
          th, td {
            border: 1px solid #444444;
          }
        </style>
      </head>
      <body>
        <table id="list">
          <thead>
            <tr>
              <th>NO</th>
              <th>TITLE</th>
              <th>WRITER</th>
              <th>COUNT</th>
              <th>DATE</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td>${post.bno}</td>
              <td>${post.title}</td>
              <td>${post.writer}</td>
              <td>${post.count}</td>
              <td>${post.createTime}</td>
            </tr>
          </tbody>
        </table>
      </body>
    </html>
    
    <!-- Jquery -->
    <script src="https://code.jquery.com/jquery-3.4.1.js" integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU=" crossorigin="anonymous"></script>
    
    728x90
Designed by Tistory.