私のJSPページにいくつかの入力フィールドがあり、フィールドを動的に作成または削除することができます。ですから、入力フィールドをspring mvcの@ModelAttributesにマップする必要があります。しかし、入力フィールドのマッピング方法はわかりません。spring mvcで同じ名前のフォームの複数の入力を取得する
<form enctype="multipart/form-data">
<!-- code omitted -->
<div>
<input type="text" name="content">
<input type="file" name="file">
</div>
<div>
<input type="text" name="content">
<input type="file" name="file">
</div>
<div>
<input type="text" name="content">
<input type="file" name="file">
</div>
</form>
とフィールドは、Javaクラッセ(@ModelAttribute)に導かれるべきであること:
は私が構成というフォームを送信したいクラスを命じる
package com.musicovery12.cookingstep.web.command;
import java.util.Arrays;
import java.util.List;
public class AddRecipeCommand {
private String mainImageUrl;
private String foodName;
private String kcal;
private String ingredients;
private String foodDescription;
// I do not know how to map into this field.
private List<RecipeStep> recipeSteps;
private String videoUrl;
public String getMainImageUrl() {
return mainImageUrl;
}
public void setMainImageUrl(String mainImageUrl) {
this.mainImageUrl = mainImageUrl;
}
public String getFoodName() {
return foodName;
}
public void setFoodName(String foodName) {
this.foodName = foodName;
}
public String getKcal() {
return kcal;
}
public void setKcal(String kcal) {
this.kcal = kcal;
}
public String getIngredients() {
return ingredients;
}
public void setIngredients(String ingredients) {
this.ingredients = ingredients;
}
public String getFoodDescription() {
return foodDescription;
}
public void setFoodDescription(String foodDescription) {
this.foodDescription = foodDescription;
}
public List<RecipeStep> getRecipeSteps() {
return recipeSteps;
}
public void setRecipeSteps(List<RecipeStep> recipeSteps) {
this.recipeSteps = recipeSteps;
}
public String getVideoUrl() {
return videoUrl;
}
public void setVideoUrl(String videoUrl) {
this.videoUrl = videoUrl;
}
@Override
public String toString() {
return "AddRecipeCommand [mainImageUrl=" + mainImageUrl + ", foodName="
+ foodName + ", kcal=" + kcal + ", ingredients=" + ingredients
+ ", foodDescription=" + foodDescription + ", recipeSteps="
+ recipeSteps + ", videoUrl=" + videoUrl + "]";
}
}
性質を有していますプリミティブ型ではありません。それはRecipeStepです:
package com.musicovery12.cookingstep.web.command;
import org.springframework.web.multipart.MultipartFile;
public class RecipeStep {
private String content;
private MultipartFile image;
public RecipeStep() {}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public MultipartFile getImage() {
return image;
}
public void setImage(MultipartFile image) {
this.image = image;
}
@Override
public String toString() {
return "RecipeStep [content=" + content + ", image=" + image + "]";
}
}
だから私の質問は、上記のJavaクラスに、フォームの入力フィールドをマッピングする方法を、 のですか? 入力フィールドの名前を変更すると可能でしょうか?それで、どう?
私はそれをやった。それはJSPのフォームタグのenctypeにタイプミスがあったためです。フィールド[0]フィールド[1]フィールド[2]フィールド[n]の名前を付けてフィールドの値を取得できます。 – PLAYMAKER