Suldo
Foundation of me
Suldo
전체 방문자
오늘
어제
  • 분류 전체보기 (33)
    • Back (18)
      • Spring (13)
      • node.js (2)
      • C# (3)
    • Front (1)
      • html (1)
      • css (0)
      • js (0)
      • react (0)
    • Sql (2)
    • 기초지식 (10)
      • 네트워크 (3)
      • DB 및 그 외 (7)
    • Error (1)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • 삼항연산자
  • node.js#node
  • spring#aop#logging
  • RPC
  • 직렬화#serializble
  • spring#annotation#@Component
  • html#js
  • spring#api
  • api#spring#aop
  • 컨트롤러#Controller#spring
  • thymeleaf#jsp

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
Suldo

Foundation of me

Back/Spring

Spring ajax , list <map> 으로 데이터 넘기기

2022. 8. 16. 11:17
728x90

View

핵심코드만 넣었습니다.

 let idList = [];
 let listdata = {"id" : this.id ,"seq" : '1'};
 idList.push(listdata);

이런 식으로 리스트에 키쌍값을 대입해 넘겨주고

  $.ajax({
contentType:'application/json',
type: "POST",                   
url: "ajax/syncOrderDelete.do",                    
data: JSON.stringify(idList),                 
dataType: "json",                    
success: function (res) {
if (res) {                            
window.location.reload();   //페이지 새로고침
alert("작업 삭제 요청이 수행되었습니다."); } 
else {                          
alert("작업 삭제 요청이 실패되었습니다.");                  }
                    }
                });

contentType과 data를 설정해주어야한다.
서버에 데이터를 json 형식으로 보내기위해 contentType을 저렇게 지정해주었고 보통 request 안에 포함된 json 형태의 데이터를 받았을 때 , 이것을 보통 VO 나 , 본인의 경우는 LIST에 다시 담아 사용했는데
AJAX는 데이터를 문자열화 해주지 않기 때문에 보낼데이터를 JSON.stringify()로 감싸주었다.

그렇지 않으면 json데이터의 "key","value" 형태의 패턴을 인식하지 못한다.


Contoller

@RequestMapping(value= "/ajax/syncOrderDelete.do", method= RequestMethod.POST)
    @ResponseBody
    public boolean syncOrderDelete(@RequestBody List<Map<String, Object>>  idList) throws Exception {
        log.debug("########syncOrderDelete########  idList: " + idList);
        int success = 0;
        for(int i=0;i<idList.size();i++) {
            String delId = idList.get(i).get("id").toString();
            String seq = idList.get(i).get("seq").toString();
            if(soService.syncOrdDelete(delId,seq)!=0 )
                success += 1;
        }
        return success == idList.size();
    }

받아올 때 List<Map<>> 형태로 받아와

값들을 지정해주고

service 메소드에 값을넣어 호출한다.

728x90
저작자표시 (새창열림)

'Back > Spring' 카테고리의 다른 글

Spring mvc 환경에서 aop : logging 적용예제  (0) 2022.08.26
Spring Mvc 기반 api 예외처리  (0) 2022.08.26
Thymeleaf 와 jsp의 차이점  (0) 2022.08.16
Dispatcher-Servlet (디스패처 서블릿) 이란?  (0) 2022.08.16
Lombok 롬복 설치방법  (0) 2022.08.16
    'Back/Spring' 카테고리의 다른 글
    • Spring mvc 환경에서 aop : logging 적용예제
    • Spring Mvc 기반 api 예외처리
    • Thymeleaf 와 jsp의 차이점
    • Dispatcher-Servlet (디스패처 서블릿) 이란?
    Suldo
    Suldo

    티스토리툴바