我正在构建一个应用程序,我需要显示从下拉菜单中选择的类别列表。
@ModelAttribute("categories")
public List<Category> getAllCategories(){
return categoryRepository.findAll();
}
我有实体:产品,类别-他们是ManyToOne(产品-〉类别)。当我选择任何下拉选项,我得到:
Failed to convert property value of type java.lang.String to required type com.slodkacysia.bakeryshop.entity.Category for property category; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@javax.persistence.ManyToOne com.slodkacysia.bakeryshop.entity.Category] for value 2; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: Provided id of the wrong type for class com.slodkacysia.bakeryshop.entity.Category. Expected: class java.lang.Long, got class java.lang.Integer; nested exception is java.lang.IllegalArgumentException: Provided id of the wrong type for class com.slodkacysia.bakeryshop.entity.Category. Expected: class java.lang.Long, got class java.lang.Integer
我已经尝试了以下转换器:
@Configuration
public class WebAppConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
}
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(CategoryConverter());
}
@Bean
public Converter CategoryConverter(){
return new CategoryConverter();
}
}
import com.slodkacysia.bakeryshop.entity.Category;
import com.slodkacysia.bakeryshop.repository.CategoryRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
@Component
public class CategoryConverter implements Converter<String, List<Category>> {
@Autowired
private CategoryRepository categoryRepository;
@Override
public List<Category> convert(String s) {
List<Category> result = new ArrayList<>();
result.add(categoryRepository.findById(Long.parseLong(s)));
return result;
}
}
实体:
package com.slodkacysia.bakeryshop.entity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import javax.persistence.*;
import java.math.BigDecimal;
import java.util.List;
@Entity
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String description;
private String image_url;
private BigDecimal price;
public List<CartItem> getCartItems() {
return cartItems;
}
public void setCartItems(List<CartItem> cartItems) {
this.cartItems = cartItems;
}
@ManyToOne
private Category category;
@OneToMany(mappedBy = "product")
private List<CartItem> cartItems;
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
private Integer available_quantity;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getImage_url() {
return image_url;
}
public void setImage_url(String image_url) {
this.image_url = image_url;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public Integer getAvailable_quantity() {
return available_quantity;
}
public void setAvailable_quantity(Integer available_quantity) {
this.available_quantity = available_quantity;
}
}
package com.slodkacysia.bakeryshop.entity;
import javax.persistence.*;
@Entity
public class Category {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFullNameCategory() {
return name;
}
@Override
public String toString() {
return "Category{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
并查看:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
<html>
<head>
<title>Product administration</title>
<style>
.error {
color: red;
}
</style>
</head>
<a>
<%@include file="/WEB-INF/header.jsp" %></a><br>
<%--@elvariable id="product" type="pl.coderslab.entity.Product"--%>
<form:form modelAttribute="product" >
Nazwa <form:input path="name"/>
<form:errors path="name" cssClass="error" /><br>
Opis <form:input path="description" />
<form:errors path="description" cssClass="error" /><br>
Image URL <form:input path="image_url" />
<form:errors path="image_url" cssClass="error" /><br>
<br/>
Price <form:input path="price"/>
<form:errors path="price" cssClass="error" /><br>
<br>
Available Quantity <form:input path="available_quantity"/>
<form:errors path="available_quantity" cssClass="error" /><br>
<br>
<form:select path="category" items="${categories}" itemLabel="fullNameCategory" itemValue="id"/>
<form:errors path="category" cssClass="error" /><br>
<br>
<input type="submit">
</form:form>
</body>
</html>
问题:我需要什么来消除这些错误?谢谢
1条答案
按热度按时间cbjzeqam1#
我设法以一种丑陋的方式解决了这个问题。通过向类别对象添加属性,它允许将窗体的内容推到控制器。