Java Spring MVC形式的两个模型
假设我有一个用户模型:Java Spring MVC形式的两个模型,java,spring,spring-mvc,Java,Spring,Spring Mvc,假设我有一个用户模型: @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private Integer id; @Column(name="username") private String username; @Column(name="password") private String password; @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
@Column(name="username")
private String username;
@Column(name="password")
private String password;
@OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name="user_profile_id")
private UserProfile profile;
用户配置文件模型:
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
@Column(name="email")
private String email;
@Column(name="firstname")
private String firstname;
@Column(name="lastname")
private String lastname;
我可以在控制器中添加此模型:
@RequestMapping(value = {"/user/{id}"}, method = RequestMethod.GET)
public String showUser(ModelMap model, @PathVariable int id) {
User user = userService.findById(id);
model.addAttribute("user", user);
return "UserView";
}
然后可以在.jsp页面中访问模型,如下所示:
<form:form method="POST" modelAttribute="user">
<form:input type="text" path="username"/>
<form:input type="password" path="password"/>
</form>
但问题是-在编辑从控制器传递到.jsp页面的用户模型时,如何同时编辑位于用户模型中的用户配置文件模型
这个问题不是重复的,因为我想知道“路径”是否可以处理层次属性,而不是如何将一个对象传递给视图
<form:input type="text" path="profile.email"/>
我不建议您将实体传递到前端,而是使用DTO进行解耦
<form:input type="text" path="profile.email"/>
我不建议您将实体传递到前端,而是使用DTO进行解耦请更清楚。你想编辑什么?什么时候?“编辑”是什么意思。您是否希望HTML表单也能够提交类似
user.profile.emailAddress
的字段值?@dbreaux是,exactly@ImpulseTheFox我点击添加用户,html表单被打开,输入用户名和密码。但正如您所看到的,我在用户模型中有UserProfile对象。它还有几个字段,如:电子邮件、姓名、姓氏。我想在编辑用户名和密码字段的同时编辑这些字段。因此,html表单中实际上有5个输入,其中两个引用用户模型,另外3个引用UserProfile模型,该模型本身包含在UserProfile模型中。可能的重复请更清楚。你想编辑什么?什么时候?“编辑”是什么意思。您是否希望HTML表单也能够提交类似user.profile.emailAddress
的字段值?@dbreaux是,exactly@ImpulseTheFox我点击添加用户,html表单被打开,输入用户名和密码。但正如您所看到的,我在用户模型中有UserProfile对象。它还有几个字段,如:电子邮件、姓名、姓氏。我想在编辑用户名和密码字段的同时编辑这些字段。实际上,html表单中有5个输入,其中两个引用用户模型,另外3个引用UserProfile模型,该模型本身包含在UserProfile模型中。可能是重复的,即^。path
属性可以使用层次化bean属性。是的,那^。path
属性可以使用层次bean属性。