[๋ฉ๋ชจ] ๐ JSP ํผ ๋ค์ค ์ ๋ ฅ ๊ฐ์ MVC์์ List๋ก ๋ฐ์ธ๋ฉ
์์ฑ์์ด๋์ค์์ฑ์๊ฐ25.10.01์กฐํ์30 ๋ชฉ๋ก ๋๊ธ 0spring MVC ๋ฐ์ธ๋ฉ ๊ท์น
spring์ @ModelAttribute ๋ก ๋์ด์จ ํผ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ฒด ๋จ์๋ก ๋ฐ์ธ๋ฉ
๋จ์ผ VO๋ input name="name" ๊ฐ์ ๊ฑธ ์๋์ผ๋ก ๋งคํ ๊ฐ๋ฅ
๊ทผ๋ฐ List<VO>๋ก ๋ฐ๋ก ๋ฐ์ผ๋ ค๋ฉด
name[0].name ์ฒ๋ผ ์ธ๋ฑ์ค ๊ธฐ๋ฐ์ ๊ตฌ์กฐ์ฌ์ผํ๊ณ , ๊ทธ๊ฑธ List๋ผ๋ ์ธํฐํ์ด์ค์ ์ง์ ์ง์ด๋ฃ์ ์ ์์....
- ๋จ์ผ ์์ ๊ธฐ๋ฅ์ ์ด๋ฆฌ๊ณ ํด๋น ๋น์ฆ๋์ค๋ก์ง์ผ๋ก ๋ค์ค ์ฒ๋ฆฌ๊ฐ ๊ฐ๋ฅํ ๊ฒ ๊ฐ์์ ์ด๊ฒ์ ๊ฒ ํด๋ดค๋๋ฐ ์ ์๋ผ๋๋ผ๊ตฌ์?
* List๋ ์ง์ ์ธํฐํ์ด์คํ ๋ถ๊ฐ - new ๋ชปํจ.
ย
๊ป๋ฐ๊ธฐ VO ํด๋์ค์์ List <vo> ํ๋ ๋ง๋ค๊ณ ๊บผ๋ด์ ์ฐ๊ธฐ๋ก ํจ.
๋ด๋ถ์์ List<vo> = new ArrayList ์ฒ๋ผ ์ปฌ๋ ์ ์ ์ด๊ธฐํ
spring์ new List()๋ ํ ์ ์์ผ๋๊น wrapper ๊ฐ์ฒด๋ฅผ ์์ฑ - ๋ด๋ถ List์ name[0], name[0] ... ๋ฐ์ธ๋ฉ....ย
ย
๊ป๋ฐ๊ธฐ VO ํด๋์ค ํ๋๋ก ๊ธฐ์กด ๋ก์ง์ ์ ์งํ๊ณ ์ปจํธ๋กค๋ฌ์ JSP name๋ง ์์ ํด์ ์ ํ ํญ๋ชฉ ์์ ๊ธฐ๋ฅ ์์ฑ ํ ์ ์์์....
ย
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 | package com.spring.springGroupS08.vo; import java.util.List; import lombok.Data; @Data public class ProductVOList { private List<ProductVO> products; } -------------------------------------------------------------------------------------------------------------- @Controller @RequestMapping("/personal/admin/productService") public class AdminProductController { @Autowired ProductService productService; /* ๋ฑ๋ก๋ ์ํ ์์ */ @PostMapping("/productUpdate") public String productUpdatePost(ProductVO vo, RedirectAttributes redirectAttributes) { int res = productService.updateProduct(vo); String msg = (res > 0) ? msg = "์ํ ์์ ์๋ฃ" : "์์ ์คํจ"; redirectAttributes.addFlashAttribute("msg", msg); return "redirect:/personal/admin/productService/productDelete"; } /* ์ ํ๋ ์ํ ์์ */ @PostMapping("/selectUpdate") public String selectUpdate(@ModelAttribute("products") ProductVOList wrapper, @RequestParam(value="selectId", required=false) List<Long> selectIds, RedirectAttributes redirectAttributes) { int count = 0; if (selectIds != null) { for (ProductVO vo : wrapper.getProducts()) { if (selectIds.contains(vo.getProductId())) { count += productService.updateProduct(vo); } } } redirectAttributes.addFlashAttribute("msg", count + "๊ฐ ์ํ ์์ ์๋ฃ"); return "redirect:/personal/admin/productService/productDelete"; } } | cs |
ย