2017-01-09 1 views
1

스프링 부트의 전자 상거래 앱에 양식이 있습니다. 잘 작동한다. 내 컨트롤러 부분은 다음과 같습니다 많은 입력 + 업로드가있는 봄 부팅 양식

@RequestMapping(value = "/admin/add",method = RequestMethod.POST) 
public String adminAddProductSubmit(@ModelAttribute(value = "product") Product product){ 
    productServiceJpa.addProduct(product); 
    return "/admin/added"; 
} 

는 지금은 업로드 이미지 업로드 입력을 추가 할. 문제가 있습니다.

@RequestMapping(value = "/admin/add",method = RequestMethod.POST) 
public String adminAddProductSubmit(final @ModelAttribute(value = "product") Product product, final @RequestAttribute(value = "image") MultipartFile uploadingFile){ 
    File file = new File(uploadingdir + uploadingFile.getOriginalFilename()); 

    try { 
     uploadingFile.transferTo(file); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    productServiceJpa.addProduct(product); 
} 

불행하게도, 그것은 작동하지 않습니다 이 시도.

내 양식 :

<form th:action="@{/admin/add}" th:object="${product}" class="form-horizontal" method="post" enctype="multipart/form-data"> 
    <h2>Nazwa przedmiotu</h2> 
    <div class="form-group"> 
     <label for="title" class="col-sm-3 control-label">Tytuł</label> 
     <div class="col-sm-9"> 
      <input type="text" th:field="*{title}" class="form-control" /> 

     </div> 
    </div> 
    <div class="form-group"> 
     <label for="category" class="col-sm-3 control-label">Kategoria</label> 
     <div class="col-sm-9"> 
      <input type="text" th:field="*{category}" class="form-control" /> 

     </div> 
    </div> 
    <div class="form-group"> 
     <label for="amount" class="col-sm-3 control-label">Ilość</label> 
     <div class="col-sm-9"> 
      <input type="text" th:field="*{amount}" class="form-control" /> 

     </div> 
    </div> 

    <div class="form-group"> 
     <label for="shortDesc" class="col-sm-3 control-label">Krótki opis</label> 
     <div class="col-sm-9"> 
      <input type="text" th:field="*{shortDesc}" class="form-control" /> 

     </div> 
    </div> 
    <div class="form-group"> 
     <label for="description" class="col-sm-3 control-label">Opis</label> 
     <div class="col-sm-9"> 
      <input type="text" th:field="*{description}" class="form-control" /> 

     </div> 
    </div> 

    <div class="form-group"> 
     <label for="price" class="col-sm-3 control-label">Cena</label> 
     <div class="col-sm-9"> 
      <input type="text" th:field="*{price}" class="form-control" /> 

     </div> 
    </div> 

    <div class="form-group"> 
     <label for="image" class="col-sm-3 control-label"> 
      <div class="col-sm-9"> 
       <input type="file" th:field="*{image}" class="custom-file-input"/> 

       <span class="custom-file-control"></span> 
      </div> 
     </label> 
    </div> 
    <input type="submit" class="btn btn-info" value="Dodaj"/> 
</form> 

당신이 어떻게 내 개체를 보내고 @ModelAttribute에 그것을 얻을 수 말해 및 파일 입력으로부터 파일을받을 수 있습니까?

스프링 부트 설명서에는 많은 자습서 f.e가 있지만 업로드 양식 만 있습니다. 많은 텍스트 입력과 파일 입력이있는 양식이 필요합니다.

+1

'@ RequestAttribute'! ='@ RequestParam' ... 적절한 주석을 사용하십시오. –

답변

0

Product 매개 변수에 @Valid 주석을 추가해야하며 다음 매개 변수는 BindingResult이어야합니다. 그래서 방법은 다음과 같습니다

@RequestMapping(value = "/admin/add",method = RequestMethod.POST) 
public String adminAddProductSubmit(final @ModelAttribute(value = "product") @Valid Product product, BindingResult bindingResult, final @RequestParam(value = "image") MultipartFile uploadingFile){ 
    File file = new File(uploadingdir + uploadingFile.getOriginalFilename()); 

    try { 
     uploadingFile.transferTo(file); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    productServiceJpa.addProduct(product); 
} 

HereBindingResult@Valid 주석 쫓을하는 이유에 대한 설명입니다.

+0

@ Michalf94 읽으십시오 : [내 질문에 누군가 대답 할 때 어떻게해야합니까?] (https://stackoverflow.com/help/someone-answers) –