Thymeleaf multiple submit button in one form

后端 未结 4 1614
时光说笑
时光说笑 2020-12-03 03:17

I have a fragment of HTML page with one form and 2 button:

相关标签:
4条回答
  • 2020-12-03 03:17

    Instead of an if-case you could have a switch case, should you not want to take in every option as a new request mapping.

    @RequestMapping(value="/edit", method=RequestMethod.POST)
    public ModelAndView edit(@ModelAttribute SomeModel model, 
            @RequestParam(value="action", required=true) String action) {
        switch(action) {
            case "save":
                // do stuff
                break;
            case "cancel":
                // do stuff
                break;
            case "newthing":
                // do stuff
                break;
            default:
                // do stuff
                break;
        }
    }
    
    0 讨论(0)
  • 2020-12-03 03:20

    You can create separate methods with different @RequestMappings using the params variable.

    @RequestMapping(value="/edit", method=RequestMethod.POST, params="action=save")
    public ModelAndView save() {}
    
    
    @RequestMapping(value="/edit", method=RequestMethod.POST, params="action=cancel")
    public ModelAndView cancel() {}
    
    0 讨论(0)
  • 2020-12-03 03:23

    You can know which submit button has been clicked and then act upon the button Here is the code

    String btnName = request.getParameter("action");
    
    if(btnName.equals("save"))
        // you code....
    else if(btnName.equals("cancel"))
        // you code....
    
    0 讨论(0)
  • 2020-12-03 03:28

    this works in my problem. use th:formaction on submit button this is work on how many you have submit button and this is also usefull for give more links to one form with different submit button

    <form action="#"  class="form" th:action="@{'/publish-post/'+${post.id}}" method="post">
    <input class="savebtn" type="submit" value="Save" th:formaction="'/save-post/'+${post.id}">
    <input class="publish" type="submit" value="Publish Article">
    </form>
    
    0 讨论(0)
提交回复
热议问题