티스토리 뷰
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 | /* 주소 검색 API */ function getAddr(){ // AJAX 주소 검색 요청 $.ajax({ url:"/sample/getAddrApi.do" // apiSampleJSONCotroller호출 ,type:"post" ,data:$("#addrform").serialize() // 요청변수설정 ,dataType:"json" // 데이터결과JSON ,success:function(jsonStr){ // jsonStr: 주소검색결과JSON 데이터 $("#list").html(""); var errCode= jsonStr.results.common.errorCode; var errDesc= jsonStr.results.common.errorMessage; if(errCode != "0" ){ alert(errCode+"="+errDesc); }else{ if(jsonStr!= null){ makeListJson(jsonStr); // JSON데이터HTML형태로변환 pageMake(jsonStr); } } } ,error: function(xhr,status, error){ alert("에러발생"); // AJAX 호출에러 } }); } | cs |
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 | @RequestMapping(value="/sample/getAddrApi.do") public void getAddrApi(HttpServletRequest req, HttpServletResponse response) throws Exception{ // 요청변수설정 String currentPage= req.getParameter("currentPage"); String countPerPage= req.getParameter("countPerPage"); String resultType= req.getParameter("resultType"); String confmKey= req.getParameter("confmKey"); String keyword = req.getParameter("keyword2"); // API 호출URL 정보설정 String apiUrl= "http://www.juso.go.kr/addrlink/addrLinkApi.do?currentPage="+currentPage+ "&countPerPage="+countPerPage+ "&keyword="+URLEncoder.encode(keyword,"UTF-8")+ "&confmKey="+confmKey+"&resultType="+resultType; URL url = new URL(apiUrl); BufferedReader br = new BufferedReader(new InputStreamReader( url.openStream(),"UTF-8")); StringBuffer sb = new StringBuffer(); String tempStr = null; while(true){ tempStr = br.readLine(); if(tempStr == null) break; sb.append(tempStr); // 응답결과XML 저장 } br.close(); response.setCharacterEncoding("UTF-8"); response.setContentType("text/xml"); response.getWriter().write(sb.toString()); // 응답결과반환 } | cs |
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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | /* 주소 검색 결과 리스트 생성 */ function makeListJson(jsonStr){ var htmlStr = ""; // jquery를 이용한 JSON 결과 데이터 파싱 htmlStr += "<table >"; htmlStr += "<tbody>"; $(jsonStr.results.juso).each(function(){ var temp = "'"+this.roadAddrPart1+"' , '"+this.zipNo+"'"; htmlStr += "<tr>"; htmlStr += "<td onclick=\"insertAddr("+temp+"); return false;\">" +this.roadAddrPart1+"</td>"; htmlStr += "</tr>"; }); htmlStr += "</tbody>"; htmlStr += "</table>"; htmlStr += "<div class='paginate' id='pageApi'></div>"; // 결과 HTML을 FORM의 결과 출력 DIV에 삽입 $("#list").html(htmlStr); } //주소 검색 결과 리스트 페이지 이동 function goPage(pageNum){ document.addrform.currentPage.value=pageNum; getAddr(); } /* json타입으로 페이지 처리시 수정 */ function pageMake(jsonStr){ var total = jsonStr.results.common.totalCount; // 총건수 var pageNum = document.addrform.currentPage.value;// 현재페이지 var paggingStr = ""; if(total < 1){ }else{ var PAGEBLOCK=document.addrform.countPerPage.value;; var pageSize=document.addrform.countPerPage.value;; var totalPages = Math.floor((total-1)/pageSize) + 1; var firstPage = Math.floor((pageNum-1)/PAGEBLOCK) * PAGEBLOCK + 1; if( firstPage <= 0 ) firstPage = 1; var lastPage = firstPage-1 + PAGEBLOCK; if( lastPage > totalPages ) lastPage = totalPages; var nextPage = lastPage+1 ; var prePage = firstPage-5 ; for( i=firstPage; i<=lastPage; i++ ){ if( pageNum == i ) paggingStr += "<a style='font-weight:bold;color:green;font-size:100%;' href='javascript:goPage("+i+");'>" + i + "</a> "; else paggingStr += "<a href='javascript:goPage("+i+");'>" + i + "</a> "; } $("#pageApi").html(paggingStr); } } /* 검색된 주소값 input 태그에 넣어주기 */ function insertAddr(roadAddrPart1, zipNo) { console.log("roadAddrPart1>>"+roadAddrPart1); console.log("zipNo >>"+zipNo); $('#coAddr1').val(roadAddrPart1); $('#coZipcode').val(zipNo); } /* 주소검색창 form 값 넘겨주기 */ function paramSet(){ $("input[name='keyword2']").val($("input[name='keyword']").val()); } | cs |
작성중인 페이지에는 form 태그가 이미 있어서 아래 paramSet이라는 function을 통해서
검색값을 넘겨 주었다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <tr> <th scope="row">사업자우편번호</th> <td colspan="2"> <input type="text" class="inp_txt w60p" name="keyword" value="" onchange="paramSet();" onkeydown="enterSearch();"/> <div class="btn-pack gray" onClick="getAddr(); return false;" >검색</div> <input type="text" class="inp_txt w60p" name="coZipcode" id="coZipcode" /> <!-- <div class="btn-pack gray" onClick="goPopup(); return false;" >검색</div> --> </td> </tr> <tr> <th scope="row">리스트</th> <td colspan="2" id="list" ></td> </tr> <tr> <th scope="row">사업자주소</th> <td colspan="2"><input type="text" class="inp_txt w100p" name="coAddr1" id="coAddr1"></td> </tr> | cs |
위와 같은 창에서 검색을 하고.....
검색값을 다시 다른 form 태그로 넘겨준다.....
1 2 3 4 5 6 7 8 9 10 | <form name="addrform" id="addrform" method="post"> <input type="hidden" name="currentPage" value="1"/> <!-- 요청 변수 설정 (현재 페이지) --> <input type="hidden" name="countPerPage" value="10"/> <!-- 요청 변수 설정 (페이지당 출력 개수) --> <input type="hidden" name="resultType" value="json"/> <!-- 요청 변수 설정 (검색결과형식 설정, json) --> <input type="hidden" name="confmKey" value="키키키ㅣ키키키"/> <!-- 요청 변수 설정 (승인키) --> <input type="hidden" name="keyword2" value=""/> <!-- 요청 변수 설정 (키워드) --> </form> 그리고 아래 있는 폼태그에 API 키를 적어 넣는다.... 저건 내거.. | cs |
사실 중간에 페이지네이션이 딱히 필요가 없어서....
양 옆의 꺽쇠를 제거 했지만....
있는 소스를 그대로 엎어쳐도.... 그부분이 오류가 났다.
'JAVA' 카테고리의 다른 글
redirect / forward [update, delete, insert] (0) | 2017.07.23 |
---|---|
설치 파일과 java파일이 맞지 않는다? 일시적 오류... (0) | 2017.07.22 |
[전자정부프레임워크] 3.5 기반 개발 시작하기(Getting Started) (0) | 2017.07.21 |
[전자정부프레임워크] 프로젝트 생성 (0) | 2017.07.21 |
[전자정부프레임워크] 페이징 커스터마이징 (0) | 2017.07.21 |
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 힘냉면록
- 맥
- 오라클
- 터미널
- 이클립스
- Eclipse
- 정규식
- html
- node관리
- 조직도관리
- 토라식당
- 최고심
- Mac
- floating button
- Tomcat
- Oracle
- server.xml
- jstree
- input
- 아파치
- tree로만들기
- Lalavel
- 성수뚝떡
- SQL
- 성수밥
- 위잇딜라이트
- 메뉴관리
- 르프리크
- Apach
- 톰캣
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함