iknowahra / cspiEdu

CSPI OJT
0 stars 0 forks source link

과제 : post 한개 보기 #28

Open iknowahra opened 2 years ago

iknowahra commented 2 years ago

과제 : post 한개 보기

  1. Mapper 작성하기
    <select id="selectBoardReader" resultType="formTestVo">
        SELECT
             NO
            ,TITLE
            ,CONTENT
            ,REG_ID AS regId 
            ,REG_DT AS regDt
            ,UPD_ID AS updId
            ,UPD_DT AS updDt
        FROM BOARD
        WHERE NO = #{no}
    </select>
  1. DAO 작성하기
    public FormTestVo selectBoardReader(int no) {
        return sqlSession.selectOne("board.selectBoardReader", no);
    }
  1. Service 작성하기
@Override
    public FormTestVo selectBoardReader(int no) {
        // TODO Auto-generated method stub
        return dao.selectBoardReader(no);
    }
  1. Controller 작성하기
@RequestMapping(value="/{itemId}", method=RequestMethod.GET )
    String boardOne(HttpServletRequest request,
            Model model) {

        Map<String, Object> pathVariables = (Map)request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
        Integer itemId = Integer.parseInt((String) pathVariables.get("itemId"));
        FormTestVo post = service.selectBoardReader(itemId);
        System.out.printf("post: %S", post);
        model.addAttribute("post", post);

        return "/board/detail";
    }
@RequestMapping(value="/{itemId}", method=RequestMethod.GET )
    String boardOne(@PathVariable("no") String no, Model model) {
        int no;
        try {
            no = Integer.parseInt(itemId);
        } catch (Exception e) {
            // error는 404로 하는게 좋다... 500이면 보안상 안좋다.
            return "redirect:/board/list";
        }
        FormTestVo post = service.selectBoardReader(no);
        model.addAttribute("post", post);

        return "/board/detail";
    }
  1. View 작성하기

    a) <a href="<c:url value="/board/${item.no}" />">${item.no }</a> 추가하기

<!-- list.jsp -->
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page session="false"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<html>
<head>
<title>List</title>
</head>
<body>
    <h1>List</h1>

    <table>
        <thead>
            <tr>
                <th>No</th>
                <th>Title</th>
                <th>User</th>
                <th>Content</th>
            <tr>
        </thead>
        <tbody>

            <c:forEach var="item" items="${list}">
                <tr>
                    <td>
                    <a href="<c:url value="/board/${item.no}" />">
                    ${item.no }</a></td>
                    <td>${item.title }</td>
                    <td>${item.regId }</td>
                    <td>${item.content }</td>
                <tr>
            </c:forEach>
        </tbody>
    </table>
    <a href="board/write">write</a>
</body>
</html>

​ b) 하나를 볼 수 있는 detail.jsp작성하기

<!-- detail.jsp -->
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page session="false"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<html>
<head>
<title>view post</title>
</head>
<body>
    <h1>View Post ${post.no }</h1>

    <table>
        <thead>
            <tr>
                <th>No</th>
                <th>Title</th>
                <th>User</th>
                <th>Content</th>
            <tr>
        </thead>
        <tbody>
                <tr>
                    <td>${post.no }</td>
                    <td>${post.title }</td>
                    <td>${post.regId }</td>
                    <td>${post.content }</td>
                <tr>
        </tbody>
    </table>
    <br><br>
    <a href="/board">Go back to list</a>
</body>
</html>