@Autowired
private WritingRepo writingRepo;
@GetMapping("/")
public ModelAndView every(Model model){
ModelAndView mav = new ModelAndView();
//1. ๋ชจ๋ ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ
List<Writing> writingEntityList = writingRepo.findAll();
//2. ๋ชจ๋ธ์ ๋ฐ์ดํฐ ๋ฑ๋กํ๊ธฐ
model.addAttribute("writingList",writingEntityList);
mav.setViewName("main");
//3. ๋ทฐ ํ์ด์ง ์ค์ ํ๊ธฐ
return mav;
}
@Autowired
private WritingService wrService;
@GetMapping("/")
public ModelAndView every(Model model){
ModelAndView mav = new ModelAndView();
//1. ๋ชจ๋ ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ
List<Writing> writingEntityList = wrService.index();
//2. ๋ชจ๋ธ์ ๋ฐ์ดํฐ ๋ฑ๋กํ๊ธฐ
model.addAttribute("writingList",writingEntityList);
mav.setViewName("main");
//3. ๋ทฐ ํ์ด์ง ์ค์ ํ๊ธฐ
return mav;
}
@Autowired
private WritingRepo writingRepo;
@GetMapping("/detail")
public ModelAndView detail(@RequestParam("no") long no){
ModelAndView mav = new ModelAndView();
Writing data = writingRepo.findById(no).orElse(null);
mav.addObject("data", data);
mav.setViewName("content");
return mav;
}
โ๏ธโ๏ธโ๏ธ ์ฒ์์๋ Optional<T> ํํ๋ก ๋ฐ์์์๋๋ฐ ์ด๊ฑด ์ฒ์๋ณด๋ ํํ๋ผ ๋ญ์ง ๋ชจ๋ฅด๊ฒ ์ด์ ์๋ฌ๊ฐ ๊ณ์ ๋ฐ์ํ์. ๊ทธ๋์ ๊ทธ๋ฅ ๋ฐ๋ก Writing Entity ๊ฐ์ฒด๋ก ๋ฐ๋ก ๋ฐ์์์.
@Autowired
private WritingService wrService;
@GetMapping("/detail")
public ModelAndView detail(@RequestParam("no") long no){
ModelAndView mav = new ModelAndView();
Writing data = writingRepo.findById(no).orElse(null);
mav.addObject("data", data);
mav.setViewName("content");
return mav;
}
โ๏ธโ๏ธโ๏ธ ๊ทธ๋ฆฌ๊ณ JSP์์๋ ์ด์ฐจํผ ๋จ์ผ ๋ฐ์ดํฐ์ด๊ธฐ ๋๋ฌธ์ ${data.title} ์ด๋ฐ์์ผ๋ก ์ฐ๋ฉด ๋จ!!