Spring MVC 错误:无法将 java.lang.String 类型的属性值转换为所需类型 [英] Spring MVC Error: Failed to convert property value of type java.lang.String to required type
本文介绍了Spring MVC 错误:无法将 java.lang.String 类型的属性值转换为所需类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不能放过这个例外:
Failed to convert property value of type java.lang.String to required type com.company.springdemo.entity.Product for property productId; nested exception is java.lang.IllegalStateException: Cannot convert value of type java.lang.String to required type com.company.springdemo.entity.Product for property productId: no matching editors or conversion strategy found
订单模型
@Entity
@Table(name = "orders") // naming the table only order, will throw exception
public class Order {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "order_id")
private Integer orderId;
@OneToOne(cascade = {CascadeType.DETACH,CascadeType.MERGE,CascadeType.PERSIST,CascadeType.REFRESH})
@JoinColumn(name = "product_id")
private Product productId;
@ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.DETACH,CascadeType.MERGE,CascadeType.PERSIST,CascadeType.REFRESH})
@JoinColumn(name = "client_id")
private Client client;
....
产品型号
@Entity
@Table(name = "product")
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "product_id")
private Integer id;
@Column(name = "product_name")
private String productName;
@Column(name = "product_serial")
private String productSerial;
...
客户模型
@Entity
@Table(name = "clients")
public class Client {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@NotEmpty
@Column(name = "first_name")
private String firstName;
@NotEmpty
@Column(name = "last_name")
private String lastName;
@NotEmpty
@Email
@Column(name = "email")
private String email;
@NotEmpty
@Column(name = "location")
private String location;
@OneToMany(mappedBy = "client",cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<Order> orders;
控制器,我在其中保存相关客户和产品的订单
@PostMapping("add")
public ModelAndView addOrder( @Validated @ModelAttribute("ords") Order order, BindingResult bindingResult ){
if (bindingResult.hasErrors()) {
System.out.println("Having errors: " + bindingResult.getAllErrors());
Iterable<Product> products = productService.listProducts();
Iterable<Client> clients = clientService.listClients();
System.out.println("Error "+ bindingResult.getAllErrors());
ModelAndView mv = new ModelAndView("orders/add-order");
mv.addObject("products",products);
mv.addObject("clients",clients);
return mv;
}
try {
orderService.saveOrder(order);
} catch (Exception e) {
e.printStackTrace();
}
ModelAndView mv = new ModelAndView("redirect:list");
return mv;
}
最后是我的JSP表单查看页面
<form:form action="add" method="post" modelAttribute="ords">
<label for="productId" >Product Id</label>
<form:select path="productId" >
<c:forEach var="product" items="${products}">
<form:option value="${product.id}">${product.productName}</form:option>
</c:forEach>
</form:select>
<form:errors path="productId"/>
<br>
<label for="client" >Client Id</label>
<form:select path="client" >
<c:forEach var="client" items="${clients}">
<form:option value="${client.id}">${client.id} - ${client.lastName}</form:option>
</c:forEach>
</form:select>
<form:errors path="client"/>
<br>
<input type="submit" value="Place Order">
</form:form>
我做错了什么?
推荐答案
您很可能需要构建这样一个转换器类:
You most likely need to build a converter class such as this one :
@Component("facilityConverter")
public class FacilityConverter implements Converter<String, Facility>
{
@Autowired
FacilityService facilityService;
@Override
public Facility convert(String id)
{
return facilityService.findById(Integer.parseInt(id));
}
}
然后,您需要通过在实现 WebMvcConfigurer 的配置类中实现 addFormatters 方法来注册它,如下所示:
Then, you need to register it by implementing the addFormatters method inside of a configuration class implementing WebMvcConfigurer like so :
@Override
public void addFormatters (FormatterRegistry registry)
{
registry.addConverter((FacilityConverter)ctx.getBean("facilityConverter"));
}
然后您的实体将从下拉选择中正确映射.此外,这可能不是您问题的一部分,但您可以像这样构建下拉列表:
Your entities will then correctly be mapped from a dropdown selection. Also, this might not be part of your issue but you can just build your dropdowns like this :
<form:select name="linkedInterface" path="linkedInterface" id="linkedInterface">
<form:options items="${interfaces}" itemLabel="name" itemValue="id"/>
</form:select>
这篇关于Spring MVC 错误:无法将 java.lang.String 类型的属性值转换为所需类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文