Reputation: 1757
in my project i have one to many mapping between company and location.While adding location i want company object. I have two differnt controller for company and location
In company Controller:
addCompany
@RequestMapping(value = "/add", method = RequestMethod.POST)
public String addCompany(@ModelAttribute("company")
Company company, BindingResult result,Model model) {
companyService.addCompany(company);
return "companyPage";
}
updateCompany
@RequestMapping(value = "/update", method = RequestMethod.POST)
public String updateCompany(@ModelAttribute("company")
Company company, BindingResult result,@RequestParam(value = "submitVal") String updateOrRestore
,Model model) {
if (updateOrRestore.equalsIgnoreCase("update")) {
companyService.updateCompany(company);
model.addAttribute("location", new Location());
} else if (updateOrRestore.equalsIgnoreCase("restore")) {
Company prevCompany = companyService.restoreCompany();
model.addAttribute("company", prevCompany);
model.addAttribute("location", new Location());
}
return "companyPage";
}
In location Controller:
addLocation
@RequestMapping(value="/addLocation", method = RequestMethod.POST )
public String addLocation(@ModelAttribute("location")
Location location,BindingResult reult, Model model){
logger.info("Location is added"+location);
//Here b4 adding location in db i want to set company obj
//location.setCompany(company);
locationService.addLocation(location);
}
How can i get company object that one is save or updated in company controller action??
Upvotes: 0
Views: 668
Reputation: 6118
Just get Company object from DB with help of Its ID. You have to maintain this ID in hidden input box inside form post and do in controller like below
@RequestMapping(value="/addLocation", method = RequestMethod.POST )
public String addLocation(@ModelAttribute("location")
Location location,BindingResult reult, Model model,@requestParam("cmpID") long ID){
//Company companyObj=get from DB with help of ID
//location.setCompany(companyObj);
locationService.addLocation(location);
return "yourview";
}
Upvotes: 1