์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
- ๋ฆฌ์กํธ
- ํ๋ก๊ทธ๋๋จธ์ค
- SoftwareExpertAcademy
- Array
- Java
- ๋ฐฑ์ค
- ์์ ํ์
- HashMap
- ์๋ฐ
- Algorithm
- ๋ฌ๋นํด๋ฝ1๊ธฐ
- JPA
- SWEA
- ์ธํ๋ ์ด์ ์์ ์ด์๋จ๊ธฐ
- Stack
- ๋ ธ๋ง๋์ฝ๋ ๊ฐ์
- ๋ฌ๋น์บ ํผ์ค
- dfs
- ReactJS๋ก ์ํ ์น ์๋น์ค ๋ง๋ค๊ธฐ
- React.js
- ์๊ณ ๋ฆฌ์ฆ
- ์ฌ๊ท
- ๋ฌ๋นํด๋ฝ 1๊ธฐ
- ๋ ธ๋ง๋์ฝ๋
- ๋ฌ๋นํด๋ฝ
- ์นด์นด์ค๋ธ๋ผ์ธ๋์ฝ๋ฉํ ์คํธ
- BOJ
- programmers
- ๊ฒฝ์ ๊ณต๋ถ
- React
- Today
- Total
th42500์ TIL
Ajax์์ Controller๋ก ๋ณด๋ธ ๊ฐ์ด ์ ๋๋ก ๋ค์ด์ค์ง ์๋ ์ค๋ฅ (Feat. ์ฅ๋ฐ๊ตฌ๋ ๊ธฐ๋ฅ) ๋ณธ๋ฌธ
Ajax์์ Controller๋ก ๋ณด๋ธ ๊ฐ์ด ์ ๋๋ก ๋ค์ด์ค์ง ์๋ ์ค๋ฅ (Feat. ์ฅ๋ฐ๊ตฌ๋ ๊ธฐ๋ฅ)
th42500 2023. 2. 9. 23:57๐ ๊ฐ๋ฐํ๊ฒฝ
Editor : Intellij Ultimate
Deb Tool : SpringBoot 2.7.5
JDK : JAVA 11
Build : Gradle 6.8
Server : AWS EC2
DevOps : Docker
DB : MySql 8.0
Library : SpringBoot Web, MySQL, Spring Data JPA, Lombok, Spring Security, Thymeleaf
โ Ajax์์ Controller๋ก ๋ณด๋ธ ๊ฐ์ด ์ ๋๋ก ๋ค์ด์ค์ง ์๋๋ค โ
์ฅ๋ฐ๊ตฌ๋์์ ์ํ ์ผ๋ถ๋ฅผ ์ฃผ๋ฌธํ๋ ๊ฒฝ์ฐ๋ฅผ ๋๋นํ์ฌ ์ฃผ๋ฌธํ๊ธฐ ๋ฒํผ ํด๋ฆญ ์ ์ํ ์ฒดํฌ ์ฌ๋ถ๋ฅผ true์ false๋ก ์ ๋ฌํ๊ธฐ๋ก ํ๊ณ ๊ฐ๋ฐ์ ํ๋ ์ค์ด์๋ค.
ํ๋ก ํธ์์ ๊ฐ์ด ์ ๋ด๊ธฐ๋์ง ํ์ธํ๊ธฐ ์ํด Console์์ ๊ฒฐ๊ณผ๋ฅผ ํ์ธํ์๊ณ ๊ทธ ๊ฒฐ๊ณผ true์ false๋ก ๊ธฐ๋ก๋๋ ๊ฒ์ ํ์ธํ ์ ์์๋ค.
๊ทธ๋ฌ๋ Controller์์ log ํ์ธ ์ ๋ชจ๋ false๋ก ๊ธฐ๋ก์ด ๋์๋ค.
"๊ฐ์ด ๋งคํ์ด ์๋์ด์ CheckOrderItemDto์ ์์ ์๋ค์ด์ค๋๊ฑด๊ฐ?" ์ถ์์ง๋ง item id์๋ ์๋ง๊ฒ ๋ฐ์ดํฐ๊ฐ ์ ๋ค์ด์ค๋ ๊ฒ์ ๋ณด๋ ๊ทธ ์ ์ ์๋๊ฒ ๊ฐ์๋ค.
ํน์ isChecked๋ง ๋ฐ์ดํฐ ๋งตํ์ด ์๋์ด boolean์ ๊ธฐ๋ณธ๊ฐ์ธ false๋ก ๋ฐ๋๋ ๊ฑด๊ฐ ์ถ์ธกํด๋ณด์๋ค.
[๐ cart.js]
function saveOrder() {
console.log('purchaseOrder() ์คํ');
var items = $(".form-check-input");
console.log(items);
var orderItems = [];
for(var [idx, selectElement] of Object.entries(items)) {
console.log(isNaN(idx));
const cartItemId = $(selectElement).attr("cartItemId");
const checked = selectElement.checked;
if(!isNaN(idx)) {
console.log("orderItems์ pushํฉ๋๋ค");
console.log(idx + " ๋ฒ ์ธ๋ฑ์ค ํ์
์ฒดํฌ = " + typeof checked);
orderItems.push({
id: cartItemId,
isChecked: checked
})
}
}
console.log(orderItems);
console.log(JSON.stringify(orderItems));
$.ajax({
url: "/api/v1/carts/checkOrder",
method: "POST",
contentType: 'application/json;charset=utf-8',
data: JSON.stringify(orderItems)
}).done((data) => {
console.log(data);
// location.href = "/carts/order";
}).fail((error) => {
console.log(error);
if(error.result) {
alert(error.result);
}
}).always(() => {
console.log("์ฃผ๋ฌธ ajax ์คํ ์๋ฃ");
})
}
[๐ CartRestController.java]
@PostMapping("/checkOrder")
public Response<String> updateCheckItem (@RequestBody List<CheckOrderItem> checkCartItemDto, Authentication authentication) {
String userName = authentication.getName();
for (CheckOrderItem itemDto : checkCartItemDto) {
log.info("item id = {}", itemDto.getId());
log.info("item isChecked = {}", itemDto.isChecked());
}
cartService.updateCheckItem(checkCartItemDto, userName);
return Response.success("์ฅ๋ฐ๊ตฌ๋ ์ํ ์ฒดํฌ ์๋ฃ");
}
[๐ CheckOrderItemDto]
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
@Getter
@AllArgsConstructor
@NoArgsConstructor
public class CheckOrderItem {
private Long id;
private boolean isChecked;
}
Ajax์ ์ด์ฉํ ๋ฐ์ดํฐ ์ ๋ฌ ์ ํ๋ผ๋ฏธํฐ ํ์ ์ด ๋ฐ๋์ ์ผ์นํ์ฌ์ผ Controller๋ก ์จ์ ํ ๊ฐ์ ๋ฐ์ ์ ์๋ค. ๊ทธ๋์ ํ๋ก ํธ์์ Console์ ์ด์ฉํ์ฌ Type์ ํ์ธํด๋ณด์๋ค.
Controller๋ก ๋ณด๋ด๊ณ ์ ํ๋ ์ํ์ ๋ฐ์ดํฐ๊ฐ ๋ชจ๋ boolean ํ์ ์ธ ๊ฒ์ ์ ํ์ธํ ์ ์์์ง๋ง, ์ฌ์ ํ Controller์์๋ ๋ชจ๋ false๊ฐ ๋์ค๋ ๊ฒ์ ํ์ธํ ์ ์์๋ค.
2์๊ฐ ๋๊ฒ ๊ณ ๋ฏผํ ๊ฒฐ๊ณผ JSON์ผ๋ก ๋ณํ๋์ด ๋์ด๊ฐ ๋ boolean์ผ๋ก ์จ์ ํ ๋์ด๊ฐ๋์ง ํ์ธ ํด๋ณด๊ธฐ๋ก ํ์๋ค.
Console์ ์ถ๋ ฅํด๋ณธ ๊ฒฐ๊ณผ JSON์ผ๋ก ๋ณํํ์ ๋ booleanํ์ ์ด์๋ check ๋ณ์๊ฐ String ํ์ ์ผ๋ก ๋ณํ๋์ด ๋ฐ์ํ๋ ๋ฌธ์ ์๋ค.
์ฆ, Parameter Type Missmatch ๐ข
๐ก ํด๊ฒฐ ๋ฐฉ๋ฒ
์ฝ๋๋ฅผ ๋ค์๊ณผ ๊ฐ์ด ๋ณ๊ฒฝํด์ฃผ์ด ์ค๋ฅ๋ฅผ ํด๊ฒฐํ ์ ์์๋ค.
[๐ CartOrderItemDto.java]
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
@Getter
@AllArgsConstructor
@NoArgsConstructor
public class CheckOrderItemDto {
private Long id;
private String isChecked;
}
[๐ CartRestController.java]
@PostMapping("/checkOrder")
public Response<String> updateCheckItem (@RequestBody List<CheckOrderItemDto> checkCartItemDto, Authentication authentication) {
String userName = authentication.getName();
for (CheckOrderItemDto itemDto : checkCartItemDto) {
log.info("item id = {}", itemDto.getId());
log.info("item isChecked = {}", itemDto.getIsChecked());
}
cartService.updateCheckItem(checkCartItemDto, userName);
return Response.success("์ฅ๋ฐ๊ตฌ๋ ์ํ ์ฒดํฌ ์๋ฃ");
}
[๐ CartItemService.java]
public void updateCheckItem(List<CheckOrderItemDto> checkOrderItemDtoList, String userName) {
UserEntity user = validateUser(userName);
CartEntity cart = validateCart(user);
for (CheckOrderItemDto dto : checkOrderItemDtoList) {
log.info("cartItemEntity ๊ฒ์ฆ");
CartItemEntity cartItem = validateCartItem(cart, dto.getId());
boolean dtoCheck = dto.getIsChecked().equals("true")?true:false;
if(cartItem.isChecked() != dtoCheck) {
log.info("cartItemEntity ๋ฐ๊พธ๊ธฐ");
log.info("cartItem.isChecked : {}", cartItem.isChecked());
log.info("dto.isChecked : {}", dtoCheck);
cartItem.updateCheckItem();
log.info("๋ณ๊ฒฝ๋ cartItem.isChecked : {}", cartItem.isChecked());
}
}
}
์์ ๊ฐ์ด ์ฝ๋๋ฅผ ๋ณ๊ฒฝ ํ ๋ค์ ์คํํด๋ณด๋ฉด ์ ํํ ์ํ์ true, ์ ํํ์ง ์์ ์ํ์ false๋ก ์๋ฒ๋ก ์ ๋์ด๊ฐ๋ ๊ฒ์ ํ
์ธํ ์ ์์๋ค.