簡體   English   中英

Springboot下拉列表

[英]Springboot dropdown

開機。 在我的系統中,有兩個名為subject,course的模型(表名稱為subject,courses,並通過外鍵course_id連接)。我試圖在addSubject百里香形式的下拉列表中選擇課程名稱,然后進行存儲表中的course_id。但是嘗試執行此操作時出錯

主題控制器

@Entity
@Table(name="subjects")
@EntityListeners(AuditingEntityListener.class)

public class Subject {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String subject_code;
    private String name;
    private String hall_code;
    private String course_code;

    public Subject() {
    }

    public Subject(String subject_code, String name, String hall_code, String course_code) {
        this.subject_code = subject_code;
        this.name = name;
        this.hall_code = hall_code;
        this.course_code = course_code;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getSubject_code() {
        return subject_code;
    }

    public void setSubject_code(String subject_code) {
        this.subject_code = subject_code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getHall_code() {
        return hall_code;
    }

    public void setHall_code(String hall_code) {
        this.hall_code = hall_code;
    }

    public String getCourse_code() {
        return course_code;
    }

    public void setCourse_code(String course_code) {
        this.course_code = course_code;
    }
}

課程DAO

@Service
public class CourseDAO {

    @Autowired
    CourseRepository courseRepository;

    //to save a course
    public Course save(Course course){
        return courseRepository.save(course);
    }

    //to search all courses
    public List<Course> findAll(){
        return courseRepository.findAll();
    }

    //get a course by id
    public Course findById(Long id){
        return courseRepository.findById(id).orElse(null);
    }


    //delete a course
    public void delete(Long id){
        courseRepository.deleteById(id);
    }


}

課程模式

@Entity
@Table(name="courses")
@EntityListeners(AuditingEntityListener.class)

public class Course {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @NotNull
    private int code;
    private String name;

    public Course() {
    }

    public Course(@NotNull int code, String name) {
        this.code = code;
        this.name = name;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

添加主題表格

<!DOCTYPE html>
<html xmlns:th="https://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Adding a Subject</title>
</head>
<body>
<div align="center">
    <h1>Add a new Subject</h1>
    <br/>
    <form action="#" th:action="@{/subject/save}" th:object="${subject}" method="post">
        <table border="0" cell[adding="10">
            <tr>
                <td>Subject code:</td>
                <td><input type="text" th:field="*{subject_code}" /></td>
            </tr>
            <tr>
                <td>Subject Name:</td>
                <td><input type="text" th:field="*{name}" /></td>
            </tr>
            <tr>
                <td>Course:</td>
                <td>
                    <select th:field="*{course}">
                        <option value="">Choose..</option>
                        <option th:each="course: ${courses}" th:value="${course.id}" th:text="${course.name}" />
                    </select>
                </td>
            </tr>
            <tr>
                <td colspan="2"><button type="submit">Save</button></td>
            </tr>
        </table>
    </form>
</div>

</body>
</html>

學科模型

@Entity
@Table(name="subjects")
@EntityListeners(AuditingEntityListener.class)

public class Subject {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String subject_code;
    private String name;
    private String hall_code;
    private String course_code;

    public Subject() {
    }

    public Subject(String subject_code, String name, String hall_code, String course_code) {
        this.subject_code = subject_code;
        this.name = name;
        this.hall_code = hall_code;
        this.course_code = course_code;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getSubject_code() {
        return subject_code;
    }

    public void setSubject_code(String subject_code) {
        this.subject_code = subject_code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getHall_code() {
        return hall_code;
    }

    public void setHall_code(String hall_code) {
        this.hall_code = hall_code;
    }

    public String getCourse_code() {
        return course_code;
    }

    public void setCourse_code(String course_code) {
        this.course_code = course_code;
    }
}

主題DAO

@Service
public class SubjectDAO {

    @Autowired
    SubjectRepository subjectRepository;

    //to save a subject
    public Subject save(Subject subject){
        return subjectRepository.save(subject);
    }

    //to search all subjects
    public List<Subject> findAll(){
        return subjectRepository.findAll();
    }

    //get a subject by id
    public Subject findById(Long id){
        return subjectRepository.findById(id).orElse(null);
    }


    //delete a subject
    public void delete(Long id){
        subjectRepository.deleteById(id);
    }


}

錯誤原因:org.springframework.beans.NotReadablePropertyException:Bean類[com.project.attendance.model.Subject]的無效屬性“ course”:Bean屬性“ course”不可讀或具有無效的getter方法:返回getter的類型與setter的參數類型匹配?

嘗試將<select th:field="*{course}">更改為<select th:field="*{course_code}"> 因為在Subject實體中沒有稱為Course的屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM