Click here to Skip to main content
15,897,518 members

How to access different controller using jqery only one button.

Member 14635434 asked:

Open original thread
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();
    }
Tags: Javascript, Java, Ajax, jQuery, Springboot

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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