Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)?

like image 694
osagie Avatar asked Aug 26 '26 08:08

osagie


1 Answers

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.

like image 93
Neil McGuigan Avatar answered Aug 28 '26 01:08

Neil McGuigan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!