gpt4 book ai didi

java - 具有一对多关系的thymeleaf表单字段的设置值

转载 作者:行者123 更新时间:2023-12-01 20:25:03 25 4
gpt4 key购买 nike

我正在尝试从 thymeleaf 提交表单数据,其中类之间的关系是一对多。该表单有多个具有相同属性的字段,因此我使用数组来提交表单。我遇到这样的字段未找到异常。具有一对多关系的类之间如何设置属性?

org.springframework.beans.NotReadablePropertyException: Invalid property 'education.name[0]' of bean class [pro.budthapa.domain.Resume]: Bean property 'education.name[0]' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?

index.html

<form th:action="@{/resume/new}" th:method="post" th:object="${resume}" class="form-horizontal" enctype="multipart/form-data">
<div class="form-group">
<label class="control-label col-sm-3" for="college">College/University Name:</label>
<div class="col-sm-6">
<input type="text" class="form-control" th:field="*{education.name[0]}" placeholder="college /university" />
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3 col-sm-offset-2" for="college">Course:</label>
<div class="col-sm-6">
<input type="text" class="form-control" th:field="*{education.course[0]}" placeholder="course of study" />
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" for="college">College/University Name:</label>
<div class="col-sm-6">
<input type="text" class="form-control" th:field="*{education.name[1]}" placeholder="college /university" />
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3 col-sm-offset-2" for="college">Course:</label>
<div class="col-sm-6">
<input type="text" class="form-control" th:field="*{education.course[1]}" placeholder="course of study" />
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-success">Submit</button>
</div>
</div>
</form>

实体类

教育.class

@Entity
public class Education {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
@Column(name="college_name")
private String name;
@Column(name="course_name")
private String course;

@ManyToOne
@JoinColumn(name="resume_id")
private Resume resume;

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 getCourse() {
return course;
}

public void setCourse(String course) {
this.course = course;
}

public Resume getResume() {
return resume;
}

public void setResume(Resume resume) {
this.resume = resume;
}
}

简历.class

@Entity
public class Resume {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String name;

@OneToMany(mappedBy="resume")
private Set<Education> education;

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 Set<Education> getEducation() {
return education;
}

public void setEducation(Set<Education> education) {
this.education = education;
}
}

Controller

ResumeController.class

@Controller
public class ResumeController {

private static final String ADD_RESUME="resume/addResume";

@Autowired
private ResumeService resumeService;

@GetMapping("/resume/new")
public String addResume(Resume resume, Model model){
model.addAttribute("resume",resume);
return ADD_RESUME;
}

@PostMapping("/resume/new")
public String addResume(@Valid Resume resume, BindingResult result, Model model){

resumeService.save(resume);
model.addAttribute("resume",resume);
return ADD_RESUME;
}
}

最佳答案

您的属性导航略有错误,请更改为此(其他字段也类似):

<input type="text" class="form-control" th:field="*{education[0].name}" placeholder="college /university" />

然后,如果没记错的话,您必须使用 List 而不是 Set:

private List<Education> education;

public List<Education> getEducation() {
return education;
}

public void setEducation(List<Education> education) {
this.education = education;
}

因为Set没有索引。

关于java - 具有一对多关系的thymeleaf表单字段的设置值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43985098/

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