gpt4 book ai didi

java - 如何在Spring Form Multiple Select中绑定(bind)模型数据

转载 作者:行者123 更新时间:2023-11-30 02:41:27 25 4
gpt4 key购买 nike

目前我正在尝试以 spring 形式多重选择绑定(bind)模型,但出现错误。

产品型号如下:

 @Entity
public class Product implements Serializable {

private static final long serialVersionUID = -3532377236419382983L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int productId;

@OneToMany(mappedBy = "product", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JsonIgnore
private List<CartItem> cartItemList;

@ManyToOne
@JoinColumn(name = "categoryId")
@JsonIgnore
private Category category;

@ManyToMany
@JsonIgnore
@JoinTable(name="PRODUCT_SUBCATEGORY",
joinColumns={@JoinColumn(name="productId")},
inverseJoinColumns={@JoinColumn(name="subCategoryId")})
private List<SubCategory> subCategoryList;


public List<SubCategory> getSubCategoryList() {
return subCategoryList;
}

public void setSubCategoryList(List<SubCategory> subCategoryList) {
this.subCategoryList = subCategoryList;
}
Getter Setter...

子类别模型如下:

@Entity
public class SubCategory implements Serializable {


private static final long serialVersionUID = 7750738516036520962L;

@Id
@GeneratedValue
private int subCategoryId;

@NotEmpty(message = "The subcategory name must not be empty")
@Size(min = 3, max = 20, message = "Minimum 3 to 20 characters allowed")
private String subCategoryName;

@ManyToOne
@JoinColumn(name="categoryId")
private Category category;
Getter and Setter...

Controller 类如下:

@Controller
@RequestMapping("/admin")
public class AdminProduct {

private Path path;

@Autowired
private ProductService productService;

@Autowired
private SubCategoryService subCategoryService;

@Autowired
private CategoryService categoryService;

@RequestMapping("/product/addProduct")
public String addProduct(Model model){
Product product = new Product();
model.addAttribute("product", product);

return "addProduct";
}
@ModelAttribute("subCategoryName")
public Map<Integer, String> populateSubCategoryTypes() {
Map<Integer, String> subCategoryNameMap = new HashMap<Integer, String>();
List<SubCategory> subCategoryList = this.subCategoryService.getAllSubCategroy();
for (SubCategory subCategory : subCategoryList) {
subCategoryNameMap.put(subCategory.getSubCategoryId(),subCategory.getSubCategoryName());
}
return sortByValue(subCategoryNameMap);
}

public <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map) {
List<Map.Entry<K, V>> list = new LinkedList<Map.Entry<K, V>>(
map.entrySet());
Collections.sort(list, new Comparator<Map.Entry<K, V>>() {
public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
return (o1.getValue()).compareTo(o2.getValue());
}
});

Map<K, V> result = new LinkedHashMap<K, V>();
for (Map.Entry<K, V> entry : list) {
result.put(entry.getKey(), entry.getValue());
}
return result;
}

addProduct.jsp如下:

 <form:form action="${pageContext.request.contextPath}/admin/product/addProduct"
method="post" commandName="product" enctype="multipart/form-data">
<div class="form-group">
<label for="category.categoryName">Category Name*</label>
<form:select id="categoryName" path="category.categoryId" class="form-Control">
<form:options items="${categoryName}" />
</form:select>
</div>

<div class="form-group">
<label for="subCategoryList">SubCategory Name*</label>
<form:select items="${subCategoryName}" multiple="true" path="subCategoryList" class="form-Control" />
</div>
<input type="submit" value="submit" class="btn btn-default">
<a href="<c:url value="/admin/productInventory" />" class="btn btn-default">Cancel</a>

</form:form>

当我运行代码时,我在选择框中获得类别,在多选框中获得子类别。但是当我单击“提交”按钮时,子类别为空。

enter image description here

当我尝试通过添加 itemvalue 和 itemid 来绑定(bind)子类别(如下所示)时,出现错误

<form:select items="${subCategoryName}" itemValue="subCategoryId" itemLabel="subCategoryName"  multiple="true" path="subCategoryList" class="form-Control" />

错误:

org.springframework.beans.NotReadablePropertyException: Invalid property 'subCategoryId' of bean class [java.lang.Integer]: Bean property 'subCategoryId' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?

I have already subCategoryId and subCategoryName property in my SubCategory Model class.

请帮助我了解如何绑定(bind)数据以获取选择的子类别值。

非常感谢。

最佳答案

替换

<form:select items="${subCategoryName}" itemValue="subCategoryId" itemLabel="subCategoryName" multiple="true" path="subCategoryList" class="form-Control" />

<form:select items="${subCategoryName}" multiple="true" path="subCategoryList" class="form-Control" />

请注意,我建议的唯一更改是删除属性 itemValueitemLabel来自form:select标签。

原因:

来自Spring docs ,我引用以下内容:

The items attribute is typically populated with a collection or array of item objects. itemValue and itemLabel simply refer to bean properties of those item objects, if specified; otherwise, the item objects themselves will be stringified. Alternatively, you may specify a Map of items, in which case the map keys are interpreted as option values and the map values correspond to option labels. If itemValue and/or itemLabel happen to be specified as well, the item value property will apply to the map key and the item label property will apply to the map value.

特别请参阅粗体文本。

从您的代码示例来看,模型属性“subCategoryName”似乎是 Integer 的映射。键和String值(value)观。因此,对于作为映射的模型属性,根据上面 Spring 文档的引用,映射键会自动解释为选项值,相应的映射值会自动解释为相应的选项标签。这就是为什么您不需要指定 itemValueitemLabel 。但你仍然指定这一点。因此,根据上面引用中粗体文本的第二行,无论您在 itemValue 中指定什么,被解释为映射键的 bean 属性 - 即 integer在你的情况下。这就是为什么 Spring 尝试在 Integer 类中查找“subCategoryId”字段及其 getter,因此失败并抛出错误。

关于java - 如何在Spring Form Multiple Select中绑定(bind)模型数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41574706/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com