gpt4 book ai didi

java - 使用什么类型的对象来制作带有选择字段的表单?

转载 作者:行者123 更新时间:2023-12-02 03:15:39 26 4
gpt4 key购买 nike

在我的模型中,我有这两个类 SportSportSession :

@Entity
@Table(name = "sport")
public class Sport implements Serializable {

@Id
@Column(name="sport_id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@Column(name = "name", nullable = false, unique = true)
private String name;
}


@Entity
@Table(name = "sport_session")
public class SportSession implements Serializable {

@Id
@Column(name="sport_session_id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

// ...

@ManyToOne
@JoinColumn(name = "sport_id", nullable = false)
private Sport sport;
}
}

使用 Thymeleaf,我想做一个允许创建 SportSession 的表单.
我希望此表格包含 <select>字段与所有运动的名称,以便关联 SportSportSession .
我的问题是:应该将什么样的对象从我的 Controller 发送到 View ?
我应该创建一个新对象SportSessionForm其中包含所有运动项目的列表,以及 Sport成员来存储用户选择的运动?
SportSession 足以做到这一点吗?那么 DTO 对象呢?简而言之,这种情况下的最佳实践是什么?

已经谢谢了。

最佳答案

最好维护两个单独的 Bean,一个用于表示层 (SportSessionFormBean),一个用于业务层(服务层)的业务/整体 Bean (SportSession)。

您需要将表示 Bean 数据 (SportSessionFormBean) 转换为 Controller 类中的业务 Bean,然后将其传递给业务 Bean 对象 (SportSession) 到服务层。服务层使用此业务 Bean 与 DAO 类交互,这些 DAO 类持久保存或与数据库执行一些事务。

How kind of object should be send from my controller to the view ? Should I create a new object SportSessionForm that contains the list of all the sports, and also a Sport member to store the sport selected by the user?

是的,SportSessionFormBean 与 HTML 前端交互。

Is SportSession is sufficient to do that? And what about DTO object ? In short, what is the best practice in this case?

如上所述,请勿将表示层数据与业务层(服务层)数据混淆。服务是可重用的组件,它们与多个端点交互(例如 Controller 或不同的 Web 服务等)。

关于java - 使用什么类型的对象来制作带有选择字段的表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40350300/

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