Click here to Skip to main content
14,383,633 members
Rate this:
Please Sign up or sign in to vote.
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.

//Cotrollers
@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";
	}

	// for update and change status of Zone
	@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();
		/* if (bindingResult.hasErrors()) {
			log.info("validate SubOffice");
			model.addAttribute("masterAddressData", masterAddressRepository.findAll());
			return "addMasterAddress";
		} */

		log.info("Add one SubOffice");
		masterAddressService.saveMasterAddressDetails(objMasterAddress, loginedUser.getUsername());
		return "redirect:showMasterAddress";
	}

	// for update and change status of MasterAddress
	@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();
		/* if (bindingResult.hasErrors()) {
			log.info("validate update MasterAddress");
			model.addAttribute("masterAddressData", masterAddressRepository.findAll());
			return "addMasterAddress";
		} */

		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();
    }
Posted
Updated 1-Nov-19 14:23pm
v2
Comments
Member 14635434 1-Nov-19 14:29pm
   
I think it is not possible.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Quote:
single button different click?
Only possible if you are handling right-click, and left-click separately. Then this requirement can be fulfilled and you can attach different handlers that send request to different controllers.

One of the way to do this would be to handle, click and contextmenu events. This SO thread talks a bit more about this, html - How can I capture the right-click event in JavaScript? - Stack Overflow[^]

But this would break the intuition of the users, and would require them to perform some strange actions that they are not very much familiar with—UX recommends against this approach of development. A good approach would be to create a form, that user can easily modify the state of—a simple checkbox—and then your form proceeds as the user wants it to.

Tip: Don't keep cards up the sleeves.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100