1

I have two submit buttons in thymeleaf template. I want it to submit it to 2 different actions in the same controller. I did the request mapping like this:

@RequestMapping(value="/directBind",  params={"send"})
public String send(Model model, @ModelAttribute(value="directBind") DirectBind directBind){

@RequestMapping(value="/directBind", params={"addPolicy"})
public String addPolicy(final DirectBind directBind, Model model){

Two input buttons in html:

 <input id="send"  type="submit" value="send" name="send" class="btn btn-success finish" data-loading-text="Sent!"/>

 <input type="submit"  name="addPolicy" class="btn btn-default" style="margin-left: 1rem; margin-bottom: 1rem;"><span class="fa fa-plus"></span> </input>

Form :

<form enctype="multipart/form-data" class="ui form" th:object="${directBind}" th:action="@{/send}" method="post"  style="padding:0 10px;">

Its submitting the form only to send action.

2 Answers 2

1

I infer, you intent is to use one spring controller to process submit actions from two submit buttons.

  • You can use same name and different value attribute for the two html input button`s. The buttons name and value attribute are sent to controller as request parameter. This will allow the controller to identify which button was clicked and take appropriate action.

    <input id="send_id"  type="submit" name="action" value="send"/>
    <input id="addPolicy_id"  type="submit" name="action" value="addPolicy"/>

    @RequestMapping(value="/directBind")
    public String doExecute(@RequestParam("action") String action, Model model, @ModelAttribute(value="directBind") DirectBind directBind){
    if (action=="send"){
    //do something
    send();
    }
    if (action=="addPolicy"){
    //do something
    addPolicy();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

I managed to solve this issue. I used "directBind" in form action attribute and used param discriminator in the request mapping. It worked!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.