具有一对多关系的百里香叶形场的设定值

时间:2017-05-15 17:01:50

标签: java spring-boot 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>

实体类

Education.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;
    }
}

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;
    }
}

控制器

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;
    }
}

1 个答案:

答案 0 :(得分:2)

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

<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没有索引。