[Ajax]Json List形式をサーバに送信します.


ビューのリスト形式をコントローラとして受け入れる
ソースコード
展望台
CUSTOM_CONTENT_TYPE = CONTENT_TYPE.XWWWFOM; このコードを挿入して転送する必要があります(必須)
           CUSTOM_CONTENT_TYPE = CONTENT_TYPE.XWWWFOM;
            //저장 버튼 클릭
            $(document).on('click', '#btnSave', function(e) {
                var rows = _popupExcelGrid.getCheckedRows();
                var fieldName = 'addressList';

                if (rows.length==0 && !confirm('선택한 행이 없습니다. 전체를 저장하시겠습니까?')) {
                    return;
                }
                var data;
                if (0<rows.length) {
                    data = _popupExcelGrid.getCheckedJson(fieldName);
                }
                    else {
                    data = _popupExcelGrid.getJson(fieldName);
                }
                alert('저장되었습니다.');
                $('#_excelPopup_DialogDiv').modal('hide');
Ajax
                $.ajax({
                    url :'/addressbook/test'
                    ,type : 'post'
                    ,dataType: 'json'
                    ,data : data
                    ,success: function(result){
                        console.log(result);
                    }
                    ,error: function (result){
                        console.log(result);
                    }
                });
AddressDto
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@ToString
@Builder
public class AddressDto extends BaseDto {

    private Long addressId;

    @NotNull(message = "그룹명은 필수 입력 값입니다.")
    private Group group;

    @NotBlank(message = "휴대폰번호은 필수 입력 값입니다.")
    @Pattern(regexp = "[0-9]{10,11}", message = "휴대폰번호가 잘못 입력되었습니다.")
    private String phoneNumber;

    @NotBlank(message = "수신자는 필수 입력 값입니다.")
    private String receiver;

    private String groupName;
    private String extendValue1;
    private String extendValue2;
    private String extendValue3;
    private String extendValue4;
    private String extendValue5;
    private long totalItems;
    private long visiblePages;
    private long totalItemsInGroup;
    private long visiblePagesInGroup;
    private List<AddressDto> addressList;

    public static AddressDto of(Address address, long size) {
        AddressDto addressDto = ModelMapperUtils.getModelMapper().map(address, AddressDto.class);
        addressDto.groupName = addressDto.getGroup().getGroupName();

        addressDto.totalItems = size;
        addressDto.visiblePages = ((size%10) > 0) ? (size/10)+1 : (size/10);

        long sizeByGroup = addressDto.getGroup().getAddresses().stream().count();
        addressDto.totalItemsInGroup = sizeByGroup;
        addressDto.visiblePagesInGroup = ((sizeByGroup%10) > 0) ? (sizeByGroup/10)+1 : (sizeByGroup/10);

        return addressDto;
    }
}
Controller
    @ResponseBody
    @RequestMapping(value = "/test", method = {RequestMethod.POST})
    public ResponseEntity listTest(AddressDto addressDto) {
//        System.out.println("==========리스트 테스트========================");
//        System.out.println(addressDto);

        Address address = Address.of(addressDto);
        this.addressRepository.save(address);
        return new ResponseEntity<>(address.getAddressId(), HttpStatus.OK);
    }