0
我有两个表格:Company和Automotive。一家公司可以有许多汽车。 我无法正确持续使用汽车。公司从下拉菜单中选择“查看”页面。带休眠数据的Spring MVC数据保存错误
我的控制器
@RequestMapping("/")
public String view(ModelMap model) {
Map<String, String> companyList = new HashMap<String, String>();
List<Company> companies = companyService.listAllCompanies();
for (Company company : companies) {
companyList.put(String.valueOf(company.getId()), company.getName());
}
model.addAttribute("companies", companyList);
model.addAttribute("automotive", new Automotive());
return "automotive/index";
}
@RequestMapping("manage")
public String manage(@ModelAttribute Automotive automotive,
BindingResult result, ModelMap model) {
model.addAttribute("automotive", automotive);
Map<String, String> companyList = new HashMap<String, String>();
List<Company> companies = new ArrayList<Company>();
for (Company company : companies) {
companyList.put(String.valueOf(company.getId()), company.getName());
}
model.addAttribute("companies", companyList);
automotiveService.addAutomotive(automotive);
return "automotive/index";
}
我查看
<form:form action="/Automotive/manage" modelAttribute="automotive">
Name : <form:input path="name" />
Description : <form:input path="description" />
Type : <form:input path="type" />
Company : <form:select path="company" items="${companies}" />
<input type="submit" />
</form:form>
Q1>按道理预期公司ID不会因为这里鉴于其一个id保存,但实际上同时节省它应该是一个对象型公司。我应该如何解决这个问题。我需要使用DTO还是有直接的方法?
Q2>我无法直接通过公司列表查看,而不是在控制器中创建新的地图?