gpt4 book ai didi

java - 在 SPRING MVC 中绑定(bind)嵌套对象

转载 作者:行者123 更新时间:2023-11-30 03:56:02 25 4
gpt4 key购买 nike

我是 SPRING MVC 的新手。我没有获得包含电话和地址对象的员工的所有详细信息。实际上,这些对象并没有与员工绑定(bind)。

按照代码操作:

//( Controller )

EmployeeController.java

@Controller
@RequestMapping(value = "/employee")
public class EmployeeController {
@Autowired
EmployeeService employeeService;
@RequestMapping("/employee")
public ModelAndView registerEmployeer(@ModelAttribute Employee employee) {
Map<String, Object> modelMap = new HashMap<>();
modelMap.put("employee", new Employee());
return new ModelAndView("employee", modelMap);
}
@RequestMapping("/add")
public String addemployee(@ModelAttribute Employee employee) {
employeeService.save(employee);
return "result";
}
@ModelAttribute("phoneTypeList")
public Map<String,String> populatePhoneTypeList()
{
return Phone.getPhoneTypes();
}
@ModelAttribute("addressTypeList")
public Map<String,String> populateAddressTypeList()
{
return Address.getAddressTypes();
}
}

//获取员工详细信息的表单员工.jsp

<h2>Employee Information</h2>
<form:form method="POST" action="employee/add"
modelAttribute="employee">
<table>
<tr>
<td><form:label path="ssn">SSN</form:label></td>
<td><form:input path="ssn" /></td>
</tr>
<tr>
<td><form:label path="firstname">First Name</form:label> </td>
<td><form:input path="firstname" /></td>
</tr>
<tr>
<td><form:label path="lastname">Last Name</form:label></td>
<td><form:input path="lastname" /></td>
</tr>
<tr>
<td><form:label path="dob">Date of Birth</form:label></td>
<td><form:input path="dob" /></td>
</tr>
<tr>
<td><form:label path="emailid">Email</form:label></td>
<td><form:input path="emailid" /></td>
</tr>
<tr>
<td>Phone Type:</td>
<td><form:select path="phoneList[0].phonetype" multiple="false">
<form:option value="NONE" label="--- Select ---" />
<form:options items="${phoneTypeList}" />
</form:select></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td><form:label path="phoneList[0].phoneno">Phone</form:label></td>
<td><form:input path="phoneList[0].phoneno" /></td>
</tr>
<tr>
<td>Address Type:</td>
<td><form:select path="addressList[0].addresstype"
multiple="false">
<form:option value="NONE" label="--- Select ---" />
<form:options items="${addressTypeList}" />
</form:select></td>
<tr>
<td><form:label path="addressList[0].street">Street</form:label></td>
<td><form:input path="addressList[0].street" /></td>
</tr>
<tr>
<td><form:label path="addressList[0].city">City</form:label></td>
<td><form:input path="addressList[0].city" /></td>
</tr>
<tr>
<td><form:label path="addressList[0].state">State</form:label></td>
<td><form:input path="addressList[0].state" /></td>
</tr>
<tr>
<td><form:label path="addressList[0].zip">Zip</form:label></td>
<td><form:input path="addressList[0].zip" /></td>
</tr>
<tr>
<td><form:label path="addressList[0].country">Country</form:label></td>
<td><form:input path="addressList[0].country" /></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit"
value="Submit" /></td>
</tr>
</table>
</form:form>

//提交表单后的结果结果.jsp

<h2>Submitted Employee Information</h2>
<table>
<tr>
<td>SSN</td>
<td>${employee.ssn}</td>
</tr>
<tr>
<td>First Name</td>
<td>${employee.firstname}</td>
</tr>
<tr>
<td>Last Name</td>
<td>${employee.lastname}</td>
</tr>
<tr>
<td>Date of Birth</td>
<td>${employee.dob}</td>
</tr>
<tr>
<td>Email</td>
<td>${employee.emailid}</td>
</tr>
<tr>
<td>Phone Type</td>
<td>${employee.phoneList[0].phonetype}</td>
</tr>
<tr>
<td>Phone</td>
<td>${phoneList[0].phoneno}</td>
</tr>
<tr>
<td>Address Type</td>
<td>${addressList.addressTypeList}</td>
</tr>
<tr>
<td>Street</td>
<td>${addressList.street}</td>
</tr>
<tr>
<td>City</td>
<td>${addressList.city}</td>
</tr>
<tr>
<td>State</td>
<td>${addressList.state}</td>
</tr>
<tr>
<td>Zip</td>
<td>${addressList.zip}</td>
</tr>
<tr>
<td>Country</td>
<td>${addressList.country}</td>
</tr>
</table>

//我的员工 bean 看起来像这样员工.java

public class Employee {
private List<Phone> phoneList = new ArrayList<Phone>();
private List<Address> addressList = new ArrayList<Address>();
private long ssn;
private String firstname;
private String lastname;
private String dob;
private String emailid;
public long getSsn() {
return ssn;
}
public void setSsn(long ssn) {
this.ssn = ssn;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getDob() {
return dob;
}
public void setDob(String dob) {
this.dob = dob;
}
public String getEmailid() {
return emailid;
}
public void setEmailid(String emailid) {
this.emailid = emailid;
}
public void addPhone(Phone ph) {
phoneList.add(ph);
}
public void addAddress(Address ad) {
addressList.add(ad);
}
public void setPhoneList(List<Phone> phoneList) {
this.phoneList = phoneList;
}
public List<Phone> getPhoneList() {
return phoneList;
}
public List<Address> getAddressList() {
return addressList;
}
public void setAddressList(List<Address> adList) {
this.addressList = adList;
}
}

我只能获取员工的详细信息,如 ssn、名字、姓氏等,但不能获取电话类型、电话号码、地址详细信息,这些信息是另一个对象的字段。我的假设是绑定(bind)在这里不起作用。有什么想法吗?

最佳答案

我遇到了类似的问题,通过删除此行解决了:

@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)

在未复制的@ElementCollection之前。

关于java - 在 SPRING MVC 中绑定(bind)嵌套对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23175199/

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