Click here to Skip to main content
15,885,885 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.

Java
//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


JavaScript
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 13:23pm
v2
Comments
Member 14635434 1-Nov-19 14:29pm    
I think it is not possible.

1 solution

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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900