Announcement Announcement Module
No announcement yet.
Can we map two buttons on the same jsp to different controllers? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Can we map two buttons on the same jsp to different controllers?

    Hi All,
    Can we map two buttons on the same jsp to different controllers? I can do it in the same controller by using params on the RequestMapping. However, I know the usual hacks:
    1. using javascript + html buttons to use
    2. using separate forms on the same page

    I am not a big fan of either one. So, is there a neater way?

    HTML Code:
    			<td><form:button>Save Organization</form:button></td>
    			<td><input type="submit" name="deleteOrg" value="Delete Organization" /></td>
    			<td><input type="submit" name="newVol" value="New Volunteer" /></td>	
    As of now both point to the form's action, but I want a diff. controller for the "New Volunteer" button.

  • #2
    Depends on your spring version. With Spring 3.1 you can with earlier versions you cannot. With Spring 3.1 you simply map the same url to different controllers and differentiate based on the params (just as you would do in a single controller).


    • #3
      Okey,probably I should have clarified it a little more, I know this is more of a cosmetic thing, but I like to purue all avenues before taking another path.
      For eg. i have an organization and an organization has a set of volunteers. Now, I have an organizationhome page where the organization's info + the volunteers' linked to it are shown. This organization jsp contains 2 buttons : update of organization and create new volunteer as I showed earlier. However, when I click on any of the volunteer, I am taken to the volunteer personal page, where i can update the volunteer. So essentially org. page has an action editOrganization and vol. page has an action of editVolunteer.

      Now, if I click on New Volunteer, I am taken to action editOrganization + param "newVol"

      If i click on Save Volunteer on the vol. page, then I am taken to editVolunteer .
      Now, I don't want to write 2 diff. methods on volunteerController -- one for insert and one for update.
      Hence this quandary. So, there's no other way?
      And did i say thank you Marten??


      • #4
        The workaround would be to create 2 methods on the volunteerController which simply delegate to another method. The java specs don't allow the same annotation on a method twice so you cannot have multiple mappings for the same method. Also does it really have to be a submit, you can also turn one of the buttons into a link with an onclick that way you only have a single mapping.


        • #5
          Yes, even I was thinking of a link based approach. I wonder why this is not already part of the spring codebase? Probably becoz there are so many w/a s.


          • #6
            IMHO a GET is better as a POST in this case why complicate things by making entering a screen for either update/create a POST makes navigation only more complex. You could then have a controller with 3 methods (as you need to do some initializing based on either a new or existing volunteer).

            public class VolunteerController {
            @RequestMapping(value="/volunteer/new", method={RequestMethod.GET})
            public String initForNew(Model model) {
              model.addObject("volunteer", new Volunteer());
              return "edit";
            @RequestMapping(value="/volunteer/{id:\\d}", method={RequestMethod.GET})
            public String initForExisting(Model model, @PathVariable("id") long id) {
              model.addObject("volunteer", volunteerService.findById(id);
              return "edit";
            @RequestMapping(value="/volunteer", method={RequestMethod.POST}) 
            public String store(@Valid @ModelAttribute("volunteer") Volunteer volunteer) {
              // Maybe some checks here
              return "edit";
            Something like that... That way y ou can use links to navigate (beware of security and access) and POST to do the actual update.


            • #7
              Righto... thanks a lot.. yeah on hindsight GET appears better.. supports the IDEMPOTENT logic as well.. thank u once again!!