作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试从 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/
我尝试通过正则表达式将文本中的单引号更改为双引号。 (单字)示例:我走了。 You gona fly to planet 'Ziqtos' => 我需要在 I'm 中保留单引号,并在 You gona
我正在构建一个 API,其中大部分将包含 JSON 和 HTML 内容。但是一些非常具体的端点只呈现 true 或 false,并且还在 POST 中接受 true 或 false。这是请求或响应的整
我是一名优秀的程序员,十分优秀!