Hi all,
I have a controller class in my spring boot application there are multiple controllers and one HTML form there is only one button.
If I make multiple buttons then the HTML form is very big and I want to show only one button at a time.
Is it possible to access all controllers by using a single button different click?
What I have tried:
I have tried to multiple buttons and this is not a good idea.
@RequestMapping(value = "/actionZoneAdd", method = RequestMethod.GET)
public String addZone(@ModelAttribute("objZone") @Valid Zone objZone, BindingResult bindingResult, Model model,
Principal principal) {
User loginedUser = (User) ((Authentication) principal).getPrincipal();
if (bindingResult.hasErrors()) {
log.info("validate Zone");
model.addAttribute("zoneData", zoneAddressRepo.findAll());
return "addMasterAddress";
}
log.info("Add one Zone");
masterAddressService.saveZoneDetails(objZone, loginedUser.getUsername());
return "redirect:showZone";
}
@RequestMapping(value = "/actionZoneUpdate", method = RequestMethod.GET)
public String updateZone(@RequestParam("zoneId") Long id, @ModelAttribute("objZone") @Valid Zone objZone,
BindingResult bindingResult, Model model, Principal principal) {
User loginedUser = (User) ((Authentication) principal).getPrincipal();
if (bindingResult.hasErrors()) {
log.info("validate update Zone");
model.addAttribute("zoneData", zoneAddressRepo.findAll());
return "addMasterAddress";
}
log.info("Update selected Zone");
Optional<Zone> zoneList = zoneAddressRepo.findById(id);
Zone currentZone = zoneList.get();
masterAddressService.updateZoneDetails(currentZone, loginedUser, objZone.getZone());
return "redirect:showZone";
}
@RequestMapping(value = "/actionMasterAddressAdd", method = RequestMethod.GET)
public String addMasterAddress(MasterAddress objMasterAddress,
BindingResult bindingResult, Model model, Principal principal) {
User loginedUser = (User) ((Authentication) principal).getPrincipal();
log.info("Add one SubOffice");
masterAddressService.saveMasterAddressDetails(objMasterAddress, loginedUser.getUsername());
return "redirect:showMasterAddress";
}
@RequestMapping(value = "/actionMasterAddressUpdate", method = RequestMethod.GET)
public String updateMasterAddress(@RequestParam("masterAddressId") Long id, MasterAddress objMasterAddress, Model model,
Principal principal) {
User loginedUser = (User) ((Authentication) principal).getPrincipal();
log.info("Update selected MasterAddress");
Optional<MasterAddress> masterAddressList = masterAddressRepository.findById(id);
MasterAddress currentMasterAddress = masterAddressList.get();
masterAddressService.updateMasterAddressDetails(currentMasterAddress, loginedUser, objMasterAddress.getSubOffice());
return "redirect:showMasterAddress";
}
jQuery
Add
Update
function saveMasterAddress() {
$('#masterAddressForm').attr("action", "actionMasterAddressAdd");
$("#masterAddressForm").submit();
}
function updateMasterAddress() {
$('#masterAddressForm').attr("action", "actionMasterAddressUpdate");
$("#masterAddressForm").submit();
}
function saveZone() {
$('#masterAddressForm').attr("action", "actionZoneAddressAdd");
$("#masterAddressForm").submit();
}
function updateZoneAddress() {
$('#masterAddressForm').attr("action", "actionZoneAddressUpdate");
$("#masterAddressForm").submit();
}