0

I have single form with multiple submit buttons, each performs separate function like:

  • Search
  • Update
  • Add
  • Delete

All actions are POST, and use name attribute in button tag mapped to params in Controller side. Everything is working fine so far. Now I need to disable the click of button while form is getting submitted and on the server side db updates are completed and server reply back.

So using jquery I added below code and then it stopped working.

JS

$('form').submit(function(e) {
   $(this).find("button[type='submit']").attr('disabled',true);
});

HTML

<form id="formSecurity"
    th:action="@{/security}" th:object="${securityDTO}" method="POST">
    <div class="form-group col-md-6">
        <div class="input-group">
            <input class="form-control" type="text" th:field="*{secId}" id="secId" placeholder="Find by SecId..." />
            <div class="input-group-append">
                <button class="btn btn-primary" name="search" type="submit">Search</button>
            </div>
        </div>
        ...
    </div>
    ...
</form>

Controller

@PostMapping(value="/security", params={"search"})
    public String searchSecurity(final SecurityDTO securityDTO, final BindingResult result, Model model) {
    ...
}

@PostMapping(value="/security", params={"update"})
    public String searchSecurity(final SecurityDTO securityDTO, final BindingResult result, Model model) {
    ...
}

@PostMapping(value="/security", params={"addRow"})
    public String searchSecurity(final SecurityDTO securityDTO, final BindingResult result, Model model) {
    ...
}

@PostMapping(value="/security", params={"removeRow"})
    public String searchSecurity(final SecurityDTO securityDTO, final BindingResult result, Model model) {
    ...
}

But now looks like name params is not getting submitted. And I am getting below error:

WARN 16660 --- [nio-8989-exec-8] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.bind.UnsatisfiedServletRequestParameterException: Parameter conditions "search" OR "update" OR "addRow" OR "removeRow" not met for actual request parameters: secId={1234}]
Dev
  • 91
  • 6
  • When you use `params={"search"}` in your `PostMapping` configuration, `Spring` requires the url to contain `?search=mySearchTerm`. Your request does not contain the query string, so you get the error. – The Head Rush Jun 03 '19 at 15:21
  • It's a POST request and params get resolved using the `name` attribute and is working fine if I comment the JS code. – Dev Jun 03 '19 at 15:29
  • You might wanna try this out first - https://github.com/spring-projects/spring-mvc-showcase. After deploying the war on your local, try this url - http://localhost:8080/spring-mvc-showcase/#messageconverters. I hope you are looking something similar. – Ajay Kumar Jun 03 '19 at 16:47
  • @Dev The error clearly states that the request contains parameter `secId` instead of the known parameters `"search" OR "update" OR "addRow" OR "removeRow"`. – The Head Rush Jun 03 '19 at 17:45
  • @Ajay, I can see the demo you shared is using RestController, but that's not the case with me.. Also I am using same request mapping, just that `params` is different. And `params` is assigned based on `name` in button. – Dev Jun 04 '19 at 13:50
  • @The Head Rush, I have updated Controller code for your reference. Yes, I can see request is not sending this params, when I add the disable code of jquery. – Dev Jun 04 '19 at 13:57
  • I don't really work with either `Thymeleaf` or `jQuery` so i can't really help there. If it seems like what you need to do is dynamically append the input value to the url assigned to `th:action`. – The Head Rush Jun 04 '19 at 14:18
  • Try https://stackoverflow.com/a/36845948/9176702 – Jorge.V Jun 07 '19 at 09:53

0 Answers0