강의

멘토링

커뮤니티

인프런 커뮤니티 질문&답변

alrnr3521님의 프로필 이미지
alrnr3521

작성한 질문수

스프링 MVC 2편 - 백엔드 웹 개발 활용 기술

예제로 구현하는 파일 업로드, 다운로드

@ModelAttribute

작성

·

300

0

@GetMapping("/items/new")
public String newItem(@ModelAttribute ItemForm form) {
return "item-form";
}

@PostMapping("/items/new")
public String saveItem(@ModelAttribute ItemForm form, RedirectAttributes redirectAttributes) throws IOException {
UploadFile attachFile = fileStore.storeFile(form.getAttachFile());
List<UploadFile> storeImageFiles = fileStore.storeFiles(form.getImageFiles());

//데이터베이스에 저장
Item item = new Item();
item.setItemName(form.getItemName());
item.setAttachFile(attachFile);
item.setImageFiles(storeImageFiles);
itemRepository.save(item);

redirectAttributes.addAttribute("itemId", item.getId());

return "redirect:/items/{itemId}";
}

그 전 강의들이랑 질문들 전부 찾아봤는데 그때는 html파일에 th:object={}이런식으로 해서 데이터를 넘긴 다는걸 알겠는데

이런 경우에는 html파일에 모델 데이터를 저장하는 변수명이 없는데 어떻게 데이터가 @PostMapping에 있는 form으로 전달이 될수있나여? 

답변 1

1

안녕하세요. alrnr3521님, 공식 서포터즈 David입니다.

이 부분은 스프링 MVC 1편 - '상품 등록 처리 - @ModelAttribute'에 자세히 설명하고 있습니다.

참고해주세요.

https://www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81-mvc-1/lecture/71236

감사합니다.

alrnr3521님의 프로필 이미지
alrnr3521

작성한 질문수

질문하기