osagie
osagie

Reputation: 269

Form with multiple action?

I have this thymleaf page:

<form method="post" action="#" th:object="${userData}"  >
    <table>
        <tr>
            <td><label th:text="#{manageusers.firstnamelabel}">Surname</label></td>
            <td><input required="required" type="text" th:field="*{Firstname}" /></td>
        </tr>
        <tr>
            <td><label th:text="#{manageusers.lastnamelabel}">Lastname</label></td>
            <td><input required="required" type="text" th:field="*{Lastname}" /></td>
        </tr>

    </table>

    <input type="submit" name="back" value="Back"/>
    <input type="submit" name="confirm" value="Confirm"/>
</form>

What I want is different action (ie. different page controller action) based on submit button clicked. Is there a way I can handle this in thymeleaf or spring boot (In my controller class)?

Upvotes: 3

Views: 4039

Answers (1)

Neil McGuigan
Neil McGuigan

Reputation: 48256

You don't want to change the form action, you want different handler methods depending on the button pressed.

back:

@RequestMapping( value="/your-url", method=POST, params={"back"} )

confirm:

@RequestMapping( value="/your-url", method=POST, params={"confirm"} )

Personally I name my buttons btnX, so as to differentiate them from model attribute field names.

Also, use <button type=submit> instead of <input type=submit> as this lets you specify name, text and value.

While we're at it, don't use html tables for layouts. Try Bootstrap.

Upvotes: 6

Related Questions