每当我尝试通过form:select传递整个对象时,我都会不断收到400 Bad Request错误。
HTTP状态400 –错误的请求
类型状态报告
说明由于某些东西被认为是客户端错误(例如,格式错误的请求语法,无效的请求消息框架或欺骗性的请求路由),服务器无法或不会处理请求。
这是我选择的形式:
<html>
<head>
<title>Dodaj produkt do aukcji</title>
</head>
<body>
<form:form action="saveProduct${auction.id}" modelAttribute="newProduct" method="POST">
<label>Nazwa:</label> <form:input path="name"/><br>
<label>Cena:</label> <form:input path="price"/><br>
<label>Kategoria:</label>
<form:select path="productCategory">
<form:options items="${productCategories}" itemLabel="name"/>
</form:select><br>
<input type="submit" value="Dodaj" class="save"/><br>
</form:form>
</body>
</html>
控制器:
@GetMapping("/addProductPage")
public String addProductPage(@RequestParam("auctionId") int id,Model theModel) {
Collection <ProductCategory> pCategories = productCategoryService.getProductCategories();
Auction auction = auctionService.getAuction(id);
Product product = new Product();
ProductCategory pCategory = new ProductCategory();
theModel.addAttribute("auction", auction);
theModel.addAttribute("newProduct", product);
theModel.addAttribute("productCategories", pCategories);
return "add-product";
}
@PostMapping("/saveProduct{someId}")
public String saveProduct(@ModelAttribute("newProduct") Product product, @PathVariable(value="someId") String someId) {
Auction auction = auctionService.getAuction(Integer.parseInt(someId));
Collection<Product> products = auction.getProducts();
products.add(product);
auction.setProducts(products);
product.setAuction(auction);
auctionService.saveAuction(auction);
productService.saveProduct(product);
return "redirect:/showMyAuctions";
}
产品实体:
@Entity
@Table(name="product")
public class Product {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="product_id")
private int id;
@Column(name="name")
private String name;
@Column(name="price")
private float price;
@ManyToOne
@JoinColumn(name="category_id")
private ProductCategory productCategory;
@ManyToOne
@JoinColumn(name="auction_id")
private Auction auction;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public ProductCategory getProductCategory() {
return productCategory;
}
public void setProductCategory(ProductCategory productCategory) {
this.productCategory = productCategory;
}
public Auction getAuction() {
return auction;
}
public void setAuction(Auction auction) {
this.auction = auction;
}
@Override
public String toString() {
return "Product [id=" + id + ", name=" + name + ", price=" + price + ", productCategory=" + productCategory
+ "]";
}
}
产品类别实体:
@Entity
@Table(name="product_category")
public class ProductCategory {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="category_id")
private int id;
@Column(name="name")
private String name;
@OneToMany(mappedBy="productCategory", cascade=CascadeType.ALL, fetch=FetchType.EAGER)
Collection<Product> products;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Collection<Product> getProducts() {
return products;
}
public void setProducts(Collection<Product> products) {
this.products = products;
}
@Override
public String toString() {
return "ProductCategory [id=" + id + ", name=" + name + "]";
}
}
我想要的是将要添加到产品中的所选产品类别。
答案 0 :(得分:0)
Spring期望productCategory
是一个对象,但它是name
的{{1}},如标记中所指定。
您需要尝试以下操作:
productCategory