在使用springboot从前端绑定数据的时候,出现如下错误
图片如下
内容如下:
There was an unexpected error (type=Bad Request, status=400).
Validation failed for object='mainCategory'. Error count: 1
org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'mainCategory' on field 'subCategories': rejected value [[SubCategory{id=1, name='课后作业'}, SubCategory{id=2, name='实验报告'}, SubCategory{id=3, name='课堂课件'}]]; codes [typeMismatch.mainCategory.subCategories,typeMismatch.subCategories,typeMismatch.java.util.List,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [mainCategory.subCategories,subCategories]; arguments []; default message [subCategories]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.List' for property 'subCategories'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'com.zjq.hbut.csdn.entity.SubCategory' for property 'subCategories[0]': no matching editors or conversion strategy found]
前端代码如下
<form action="" class="ui form" th:object="${mainCategory}" th:action="*{id}==null ? @{/admin/type/add}:@{/admin/type/add/{id}(id=*{id})}" method="post">
<input type="hidden" name="id" th:value="*{id}">
<input type="hidden" name="subCategories" th:value="*{subCategories}">
<div class="required field">
<div class="ui left labeled input">
<label class="ui teal basic label">名称</label>
<input type="text" name="name" placeholder="名称..." th:value="*{name}">
</div>
</div>
<div class="ui right aligned container">
<!--onclick事件返回上一个页面-->
<button type="button" class="ui button" onclick="window.history.go(-1)">返回</button>
<button class="ui teal submit button">提交</button>
</div>
</form>
后端Controller代码如下
@PostMapping({"/type/add","/type/add/{id}"})
public String add(@Valid MainCategory mainCategory, BindingResult result,
@PathVariable(name = "id",required = false)Long id,
@RequestParam(name = "subCategories") String subCategories,
RedirectAttributes attributes)
{
if(imainCategoryService.getByName(mainCategory.getName())!=null)
{
result.rejectValue("name","nameError","该标签已存在");
}
if(result.hasErrors())
{
System.out.println(result.getAllErrors());
return "admin/type-input";
}
if(id!=null&&id>0)
{//修改
MainCategory update = imainCategoryService.update(mainCategory);
if(update==null)
{
attributes.addFlashAttribute("message","更新成功");
}else {
attributes.addFlashAttribute("message","更新失败");
}
return "redirect:/admin/type";
}else {
//新增
MainCategory save = imainCategoryService.save(mainCategory);
if(save==null)
{
attributes.addFlashAttribute("message","保存成功");
}else {
attributes.addFlashAttribute("message","保存失败");
}
return "redirect:/admin/type";
}
}
实体类代码如下
package com.zjq.hbut.csdn.entity;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
@Entity
@Table(name = "t_maincategory")
public class MainCategory {
@Id
@GeneratedValue
private Long id;
private String name;
@ManyToMany
private List<SubCategory> subCategories=new ArrayList<>();
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 List<SubCategory> getSubCategories() {
return subCategories;
}
public void setSubCategories(List<SubCategory> subCategories) {
this.subCategories = subCategories;
}
@Override
public String toString() {
return "MainCategory{" +
"id=" + id +
", name='" + name + '\'' +
", subCategories=" + subCategories +
'}';
}
}