从具有 1:N 关系的字段中设置百里香叶的值。我该怎么办?

在妍

类之间的关系是1:N。在 Thymeleaf 中,我创建了一个拆分表单对象来提交表单数据。但是当我按下发送时,它会发送,但我在屏幕上看不到任何数据。

教授班

@Entity
@Getter
@Setter
@Table(name = "professor")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@ToString(of = {"id", "username", "userid", "position", "sal", "hiredate"})
public class Professor {

    @Id @GeneratedValue
    @Column(name = "profno", nullable = false)
    private Long id;

    @Column(name = "name", nullable = false, length = 50)
    private String username;

    @Column(nullable = false, length = 50)
    private String userid;

    @Column(nullable = false, length = 20)
    private String position;

    private int sal;

    private String hiredate;

    private int comm;

    @ManyToOne(fetch = LAZY)
    @JoinColumn(name = "deptno")
    private Department department;

    @OneToMany(mappedBy = "professor")
    private List<Student> students = new ArrayList<>();

    public Professor(String username) {
        this.username = username;
    }

    public Professor(String username, String position) {
        this.username = username;
        this.position = position;
    }

    public Professor(String username, String position, int sal, String hiredate, int comm, String userid, Department department) {
        this.username = username;
        this.hiredate = hiredate;
        this.userid = userid;
        this.sal = sal;
        this.comm = comm;
        this.position = position;
        if (department != null) {
            setDepartment(department);
        }
    }

    // ===== Method ===== //
    public void setDepartment(Department department) {
        this.department = department;
        department.getProfessors().add(this);
    }

部门.班级

@Entity
@Getter
@Setter
@Table(name = "department")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@ToString(of = {"id", "name", "loc"})
public class Department {

    @Id @GeneratedValue
    @Column(name = "deptno", nullable = false)
    private Long id;

    @Column(name = "dname", length = 100)
    private String name;

    @Column(length = 100)
    private String loc;

    @OneToMany(mappedBy = "department")
    private List<Professor> professors = new ArrayList<>();

    @OneToMany(mappedBy = "department")
    private List<Student> students = new ArrayList<>();
    
    public Department(String name) {
        this.name = name;
    }

    public Department(String name, String loc) {
        this.name = name;
        this.loc = loc;
    }
}

教授表格.class

@Getter
@Setter
public class ProfessorForm {

    private Long id;

    @NotEmpty()
    private String username;
    private String userid;
    private String position;
    private String hiredate;
    private int sal;
    private int comm;
    private Department department;
}

控制器类

Professor professor = new Professor(form.getUsername(), form.getUserid(),
                form.getComm(), form.getHiredate(), form.getSal(), form.getPosition(), form.getDepartment());

百里香叶

    <form th:action="@{/professor/add}" th:object="${professorForm}" method="post">
        <div class="form-group">
            <label th:for="username">name</label>
            <input type="text" th:field="*{username}" class="form-control" placeholder="">
        </div>

        <div class="form-group">
            <label th:for="userid">id</label>
            <input type="text" th:field="*{userid}" class="form-control" placeholder="">
        </div>

        <div class="form-group">
            <label th:for="position">position</label>
            <input type="text" th:field="*{position}" class="form-control" placeholder="">
        </div>

        <div class="form-group">
            <label th:for="sal">sal</label>
            <input type="number" th:field="*{sal}" class="form-control" placeholder="">
        </div>

        <div class="form-group">
            <label th:for="hiredate">hiredate</label>
            <input type="date" th:field="*{hiredate}" class="form-control" placeholder="">
        </div>

        <div class="form-group">
            <label th:for="comm">comm</label>
            <input type="range" th:field="*{comm}" class="form-control" min="0" max="100" value="0" placeholder="">
        </div>

        <div class="form-group">
            <label th:for="departmentId">loc</label>
            <input type="text" th:field="*{department.loc}" class="form-control" placeholder="">
        </div>

最后确认是department.loc问题。我们应该如何处理这个问题?

维姆·德布劳威

我假设您希望 HTML 表单的用户为要创建的教授实体选择某个部门。支持这一点的最简单方法是添加一个 HTML 选择,显示所有可能的部门,允许用户选择一个。

要使用 Thymeleaf 实现这一点,您需要以下内容:

  1. 更新ProfessorForm以使用应引用的实体的主键(Department在本例中,因此使用long):
public class ProfessorForm {

   // instead of: private Department department;
    private long departmentId;
}
  1. @GetMapping控制器方法中,返回所有部门名称和 ID。这需要填写 HTML 选择
List<DepartmentNameAndId> departments = ...
model.addAttribute( "departments", departments);
  1. 更新 Thymeleaf 模板以使用 HTML 选择:
<select th:field="*{departmentId}">
            <option th:each="department : ${departments}"
                    th:text="${department.name}"
                    th:value="${department.id}">
        </select>
  1. @PostMapping控制器的de中,使用departmentId来获取相应的Department对象并使用它来创建Professor对象。

有关更多详细信息,请参阅在 Thymeleaf 中使用 HTML 选择选项

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

的具有1M的gradle中的一个错误,我应该怎么办?

如何使用百里香在th:if标签中具有多种条件

如何将所有百里香输入字段转换为UpperCase?

具有共享主键的OneToOne关系生成n + 1个选择;任何解决方法?

什么是百里香叶?使用百里香叶有什么好处?

spring-data-jdbc:查询包含具有1-n关系的实体

尝试百里香没有成功

如何在具有src的<img>标记中附加百里香变量?

来自百里香的带有路径变量的多个requestmapping值

为什么html页面没有在百里香中显示?

如何选择具有(n-1)D个轴索引的nD阵列轴上的值?

如何在百里香中循环1到5?

如何在没有春季安全保障的情况下在百里香叶中获得会议

在VHDL中如何实现具有n位输入,1位输出的异或门

具有N列和N行的矩阵,列必须具有N-1,N-2等值

SML中具有有限数量的类型n> 1的值

sapply()具有值的向量,而不是1:n

具有1:N关系的两个实体之间如何关联?

将具有1到N关系的表合并为1条记录,其中N条记录的最后一个值

ERD:将具有属性的1:N关系映射到关系模式

有没有一种方法可以将百里香叶的数据发送到角度?

如何在Azure App Service中插入具有1:n关系的实体

MySQL n:m从表1中查找仅具有表2中的元素的元素

带有百里香叶的HTML列表

图片附在带有百里香叶的邮件中

SQL 检查在子实体中具有特定特征的记录(1 到 n)

在 Spring MVC 处理程序中使用 PDF 视图有条件地返回百里香叶视图

百里香+弹簧靴。我尝试制作动态字段,但是当按下按钮时什么也没有发生

如何清理存储在字典中的具有 1 到 n 个条件的熊猫数据框?