ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 2. view단에서 컨트롤러로 ajax 요청하기.
    java/Spring블로그 만들기 2020. 7. 14. 17:25
    728x90

    스프링 3.0이하에서만

    우선 먼저 pom.xml에서

    <!-- ajax 사용시 필요한듯 -->
    		<dependency>
           		<groupId>org.codehaus.jackson</groupId>
           		<artifactId>jackson-mapper-asl</artifactId>
           		<version>1.9.13</version>
    		</dependency>
    		<dependency>
    		        <groupId>org.codehaus.jackson</groupId>
    		        <artifactId>jackson-core-asl</artifactId>
    		        <version>1.9.13</version>
    		</dependency>
    		<dependency>
    		    <groupId>com.fasterxml.jackson.core</groupId>
    		    <artifactId>jackson-databind</artifactId>
    		    <version>2.10.0</version>
    		</dependency>

    라이브러리 추가

     

    BoardController.java에서 

    /*
    	 * 댓글
    	 * @ResponseBody 이게 있어야 스프링 3.0이하 버전에서 ajax 비동기 통신이 가능
    	 * 댓글 리스트 가져오기.
    	 */
    	@ResponseBody 
    	@RequestMapping(value = "/getReqlyList", method = RequestMethod.POST)
    	public List<ReplyVO> getReplyList(@RequestParam("bid") int bid) throws Exception {
    
    		return replyService.getReplyList(bid);
    
    	}

    @ResponseBody 추가하기. 위에 라이브러리랑 @ResponseBody 추가하면 반환 방식이 json인가봄?

     

    servlet-context.xml에서

     

    <!-- Enables the Spring MVC @Controller programming model -->
    	<annotation-driven />
    

    추가

    마지막으로 View단에서 ajax요청 테스트

    var url = "${pageContext.request.contextPath}/board/getReqlyList";
    
    		var paramData = {"bid" : "${boardContent.bid}"};
    
    		$.ajax({
    
                type: 'POST',
    
                url: url,
    
                data: paramData,
    
                dataType: 'json',
    
                success: function(result) {
    
                   	var htmls = "";
    
    			if(result.length < 1){
    
    				htmls.push("등록된 댓글이 없습니다.");
    
    			} else {
    
    	                    $(result).each(function(){
    
    	                     htmls += '<div class="media text-muted pt-3" id="rid' + this.rid + '">';
    
    	                     htmls += '<svg class="bd-placeholder-img mr-2 rounded" width="32" height="32" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMidYMid slice" focusable="false" role="img" aria-label="Placeholder:32x32">';
    
    	                     htmls += '<title>Placeholder</title>';
    
    	                     htmls += '<rect width="100%" height="100%" fill="#007bff"></rect>';
    
    	                     htmls += '<text x="50%" fill="#007bff" dy=".3em">32x32</text>';
    
    	                     htmls += '</svg>';
    
    	                     htmls += '<p class="media-body pb-3 mb-0 small lh-125 border-bottom horder-gray">';
    
    	                     htmls += '<span class="d-block">';
    
    	                     htmls += '<strong class="text-gray-dark">' + this.reg_id + '</strong>';
    
    	                     htmls += '<span style="padding-left: 7px; font-size: 9pt">';
    
    	                     htmls += '<a href="javascript:void(0)" onclick="fn_editReply(' + this.rid + ', \'' + this.reg_id + '\', \'' + this.content + '\' )" style="padding-right:5px">수정</a>';
    
    	                     htmls += '<a href="javascript:void(0)" onclick="fn_deleteReply(' + this.rid + ')" >삭제</a>';
    
    	                     htmls += '</span>';
    
    	                     htmls += '</span>';
    
    	                     htmls += this.content;
    
    	                     htmls += '</p>';
    
    	                     htmls += '</div>';
    
    
    
    	                });	//each end
    
    
    
    			}
    
    			$("#replyList").html(htmls);
    
                    
    
                }	   // Ajax success end
    
    		});	// Ajax end

    결과는 성공!

    728x90

    'java > Spring블로그 만들기' 카테고리의 다른 글

    ㄷㄱ  (0) 2021.05.25
    1. 기본세팅하기  (0) 2020.07.02
Designed by Tistory.