Results 1 to 5 of 5

Thread: replacing button with href for post submission

  1. #1

    Default replacing button with href for post submission

    Below is the form submit part of the jsp code:

    Code:
     
    <button type="submit" name="search">Search</button>
    <button type="submit" name="add">Add</button>
    <button type="submit" name="save">Save</button>
    <button type="submit" name="remove">Remove</button>
    Below is the controller class handling the above 4 submit button calls.

    Code:
     
        @RequestMapping(method = RequestMethod.POST, params = "search")
        public String search(@ModelAttribute("consolidate") ConsolidateDto consolidate,
                BindingResult errors, ModelMap model) {
    }
     
        @RequestMapping(method = RequestMethod.POST, params = "add")
        public String add(@ModelAttribute("consolidate") ConsolidateDto consolidate,
                BindingResult errors, ModelMap model) {
    }
     
        @RequestMapping(method = RequestMethod.POST, params = "save")
        public String save(@ModelAttribute("consolidate") ConsolidateDto consolidate,
                BindingResult errors, ModelMap model) {
    }
     
        @RequestMapping(method = RequestMethod.POST, params = "remove")
        public String remove(@ModelAttribute("consolidate") ConsolidateDto consolidate,
                BindingResult errors, ModelMap model) {
    }
    Now, how to replace the above buttons with hyperlinks in the jsp page, so that i dont have to do any changes in the controller class.

    I tried below code:

    Code:
     
    <a href="#" type="submit" name="search">Search</a>
    <a href="#" type="add" name="add">Add</a>
    <a href="#" type="save" name="save">Save</a>
    <a href="#" type="remove" name="remove">Remove</a>
    but it is not working.

    please help.

  2. #2

    Default

    please someone advice me on this. Thank you.

  3. #3
    Join Date
    Oct 2005
    Location
    Mobile, AL
    Posts
    345

    Default

    I would suggest you use a javascript function to accomplish the task. The sample I wrote uses JQuery to make things simple:

    Code:
    <script type="text/javascript">
    function submitForm(invokeParamVal) {
        
        // set the hidden input's name to the value you want.
        $('#submitParam').attr('name',invokeParamVal);
        
        // submit the form.
        $('#crudForm').submit();
    
    }
    </script>
    
    <form id="crudForm" action="<your action here>" method="POST">
    
        <input type="hidden" name="search" id="submitParam" value="true">
        
        <a href="#" onClick="submitForm('search')">Search</a>
        <a href="#" onClick="submitForm('add')">Add</a>
        <a href="#" onClick="submitForm('save')">Save</a>
        <a href="#" onClick="submitForm('remove')">Remove</a>
    
    </form>

  4. #4

    Default

    MartyJones, Thank you very much. it works perfectly fine!

    However, I dont fully understand the params attribute of @RequestMapping(method = RequestMethod.POST, params = "add") annotation.

    Till date, I was using it to map multiple submit buttons in the jsp form to controller methods.

    today, I checked a code on the net, where params is used like @RequestMapping(method = RequestMethod.POST, params = "option=second")

    it confused me more about params attribute as its checking option value inside the param.

    can you please explain me how params attribute works. Thank you.

  5. #5
    Join Date
    Oct 2005
    Location
    Mobile, AL
    Posts
    345

    Default

    Take a look at the documentation. It does a good job of explaining the params option. Here is the link.

    Basically, if you have params="myParam=myValue" it means that in order for the request mapping to be hit there must exist a request parameter named "myParam" with a value of "myValue".

    If you have params="myParam" it means that in order for the request mapping to be hit there must exist a request parameter named "myParam". It can have any value.

    Hope this helps.

    Marty

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •