2016-12-19 3 views
2

私はファイル入力とテキスト入力のコレクションを持っています。各ファイル入力には、対応するタイプ・フィールドがあります。両方の値をリストとして取得する必要があります。春にファイルとテキスト入力フィールドを同時に扱うMVC

コントローラーコード:

@RequestMapping(value = "/{id}", method = RequestMethod.POST) 
public OutletIndex updateOutlet(@PathVariable String id, 
           @ModelAttribute @Valid OutletIndex outlet, 
           @RequestParam(value = "file", required = false) List<MultipartFile> file, 
           @RequestParam(value = "types", required = false) List<MultipartFile> types, 
           HttpServletRequest request 
) { 
    //...... 
} 

HTMLコード:現在、私は次のよう試みたのだ

<input type="text" name="types"/> 
<input type="text" name="types"/> 
<input type="text" name="types"/> 
<input type="text" name="types"/> 
<input type="file" name="file"/> 
<input type="file" name="file"/> 
<input type="file" name="file"/> 
<input type="file" name="file"/> 

ファイルの入力は任意のファイルを選択した場合、それはうまく働いています。ファイルを選択しないと、リストには表示されませんが、空の場合でもテキスト入力が表示されます。どうすれば解決できますか?

私はまた、request.getParameterMap()を使用して値を取得しようとしましたが、両方の結果が同じです。

2つのファイルを選択すると、出力は同じようになります。

enter image description here

typesリストのサイズが4fileリストのサイズが2です。

ファイルが選択されていない場合、nullをリストに追加できますか?

答えて

2

最後に、POJOクラスを使用して@RamanaManoj答えを少し変更して問題を解決しました。私はFileWrapperのリストを保持する追加のPOJOクラスを追加しました。


FileWrapper.java:

public class FileWrapper { 
    private MultipartFile file; 

    private String types; 

    public MultipartFile getFile() { 
     return file; 
    } 

    public void setFile(MultipartFile file) { 
     this.file = file; 
    } 

    public String getTypes() { 
     return types; 
    } 

    public void setTypes(String types) { 
     this.types = types; 
    } 
} 


FileWrappers。Javaの:

public class FileWrappers { 
    private List<FileWrapper> fileWrappers=new ArrayList<>(); 

    public List<FileWrapper> getFileWrappers() { 
     return fileWrappers; 
    } 

    public void setFileWrappers(List<FileWrapper> fileWrappers) { 
     this.fileWrappers = fileWrappers; 
    } 
} 

コントローラー:

@RequestMapping(value = "/{id}", method = RequestMethod.POST) 
public OutletIndex updateOutlet(@PathVariable String id, 
           @ModelAttribute("OutletIndex") @Valid OutletIndex outlet, 
           BindingResult resultOutlet, 
           @ModelAttribute("FileWrappers") FileWrappers fileWrappers, 
           BindingResult resultWrappersoutletImage 
) { 
    //.... 
} 

HTMLフォームフィールド:

<input type="text" name="fileWrappers[0].types"/> 
<input type="text" name="fileWrappers[1].types"/> 
<input type="text" name="fileWrappers[3].types"/> 
<input type="text" name="fileWrappers[4].types"/> 
<input type="file" name="fileWrappers[0].file"/> 
<input type="file" name="fileWrappers[1].file"/> 
<input type="file" name="fileWrappers[2].file"/> 
<input type="file" name="fileWrappers[3].file"/> 
3

POJOは.Iはあなたが達成しようとしているが、これはあなたを助けるかもしれないものを確認していないファイルやテキスト入力をマッピング持っているだろうこれを行うための最善の方法..

あなたのPOJO

public class FileWrapper { 
private MultipartFile file; 

private String type; 

public MultipartFile getFile() { 
    return file; 
} 

public void setFile(MultipartFile file) { 
    this.file = file; 
} 

public String getType() { 
    return type; 
} 

public void setType(String type) { 
    this.type = type; 
} 
} 

あなたのコントローラ

@RequestMapping(value = "/file", method = RequestMethod.POST) 
public OutletIndex updateOutlet(@ModelAttribute List<FileWrapper> fileWrapper) { 
    // ...... 

    for (FileWrapper file : fileWrapper) { 

     if (file.getFile() != null && StringUtils.isNotEmpty(file.getType())) { 
      // your logic 
     } 
    } 

} 

あなたのJSPファイル

<form method="post" action="/file" modelAttribute="fileWrapper"> 
    <input type="text" name="types[0]" /> <input type="text" 
     name="types[1]" /> <input type="text" name="types[2]" /> <input 
     type="text" name="types[3]" /> <input type="file" name="file[0]" /> <input 
     type="file" name="file[1]" /> <input type="file" name="file[2]" /> <input 
     type="file" name="file[3]" /> 
</form> 

これはあなたには快適です。試してみてください。

+0

おかげでたくさんの男...最後に 'FileWrapper'のリストを保持する追加POJOクラスを作成した後、私は問題を解決しました:) –

+1

ようこそPranav。 –

関連する問題