Back-end/Spring-핵심& webMVC

Querystring vs. @PathVariable 이용상 차이점

prden 2021. 6. 7. 17:45

1. Querystring 

https://prde.tistory.com/61?category=946261

※Controller에서는 아래와 같이 

쿼리 스트링일 때는 getParameter로 ~

@RequestMapping("/boardRead")
    public String boardRead(HttpServletRequest request, Model model){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        UserDTO user = userDetailsDao.findUserByEmail(authentication.getName());
        String userid = user.getUserid().toString();

        //Integer alertcount = etcSvc.selectAlertCount(userid);
        //model.addAttribute("alertcount", alertcount);

        String bgno = request.getParameter("bgno");
        String brdno = request.getParameter("brdno");

        //이렇게 필드 주입하는 거?? 일반적인가?
        Field3VO f3 = new Field3VO(brdno, userid, null);

        boardDetailsDao.updateBoardRead(f3);
        BoardVO boardInfo = boardDetailsDao.selectBoardOne(f3);
        List<?> listview = boardDetailsDao.selectBoardFileList(brdno);
        List<?> replylist = boardDetailsDao.selectBoardReplyList(brdno);

        BoardGroupVO bgInfo = boardDetailsDao.selectBoardGroupOne4Used(boardInfo.getBgno());
        if(bgInfo == null){
            return "board/BoardGroupFail";
        }

        model.addAttribute("boardInfo", boardInfo);
        model.addAttribute("listview", listview);
        model.addAttribute("replylist", replylist);
        model.addAttribute("bgno", bgno);
        model.addAttribute("bgInfo", bgInfo);

        return "board/BoardRead";
    }

※ 화면에서 어떤식으로 요청하는지 

                    <button type="button" class="btn btn-default pull-right" onclick="fn_moveToURL('boardForm?bgno=<c:out value="${searchVO.bgno}"/>')">

  <div class="row">
            <div class="col-lg-12">
                <button id="boardlistBtn" type="button" class="btn btn-default" onclick="showBoardList()"><i class="fa  fa-files-o fa-fw"></i> <c:out value="${bgInfo.bgname}"/></button>
                <div id="boardlistDiv" style="width: 250px; height:300px; display: none;" class="popover fade bottom in" role="tooltip">
                    <div style="left:15%;" class="arrow"></div>
                    <div class="popover-content">
                        <div id="tree"></div>
                    </div>
                </div>

                <c:if test="${bgInfo.bgreadonly=='N' || sessionScope.userrole=='A'}">
                    <button type="button" class="btn btn-default pull-right" onclick="fn_moveToURL('boardForm?bgno=<c:out value="${searchVO.bgno}"/>')">
                        <i class="fa fa-edit fa-fw"></i> <s:message code="board.new"/></button>
                </c:if>
       </div>
  </div>

2. @PathVariable

예를 들어 게시판을 만들었을 때 게시글 하나를 불러오는 경우, view 뒤에 파라미터가 들어가서

 

※Controller는 다음과 같이 표시 

@GetMapping("/view/{id}")
	public String getBoardView(@PathVariable long id, HttpSession session, Model model) {

		Authentication auth = SecurityContextHolder.getContext().getAuthentication();
		User user = userDetailsDao.findUserByUsername(auth.getName());
		
		if (user == null) {
			return session.getServletContext().getContextPath() + "/login";
		}
		

		boardMapper.increment(id, user.getUname());
		model.addAttribute("board", boardMapper.selectById(id));
		model.addAttribute("replylist", boardReplySvc.selectBoardReplyList(id));
		model.addAttribute("user", user);
		
		return "board_view";
	}

※화면은 아래와 같이 표시

 

<table class="table table-condensed table-hover table-striped">
	<thead>
		<tr>
			<th class="col-xs-1 col-sm-1 col-md-1 col-lg-1">No</th>
			<th class="col-xs-6 col-sm-6 col-md-6 col-lg-6">Title</th>
			<th class="col-xs-2 col-sm-2 col-md-2 col-lg-2">Writer</th>
			<th class="col-xs-2 col-sm-2 col-md-2 col-lg-2">Date</th>
			<th class="col-xs-1 col-sm-1 col-md-1 col-lg-1">Hits</th>
	     </tr>
	</thead>
	<tbody>
			<c:forEach items="${board }" var="board">
				<tr>
					<td>${board.id }</td>
					<td><a href="<c:url value='/boards/view/${board.id }'/>">${board.title }</a></td>
					<td>${board.writer }</td>
					<td><fmt:formatDate pattern="MM-dd hh:mm" value="${board.regDate }" /></td>
					<td>${board.hitCount }</td>
				</tr>
			</c:forEach>
	</tbody>
   </table>

3. 가장 큰 차이: 

화면단에서 URL을 어떤 형식으로 요청하는지, 그에 따라 어떤식으로 Controller에서 처리하는지