Form submit

An example of a simple form that gets submit as map or mapped object on the Controller side

Back to overview

Form submit as mapped object

Automatically map a form submit to a Hashmap

JDK22.0.2 w/ Medusa 0.9.6-SNAPSHOT

Client

<form m:submit="displayName(123, :{form}, 'sample')">
    <label for="firstName">First name: </label>
    <input type="text" id="firstName" name="firstName" />

    <label for="lastName">Last name: </label>
    <input type="text" id="lastName" name="lastName" />

    <input type="submit" value="Submit">
</form>

<div class="example-result" th:text="${result}"></div>

Server

package sample.getmedusa.showcase.samples.textinputs.formsubmit;

import io.getmedusa.medusa.core.annotation.UIEventPage;
import io.getmedusa.medusa.core.attributes.Attribute;

import java.util.HashMap;
import java.util.List;

@UIEventPage(path = "/detail/sample/form-submit", file = "/pages/sample/form-submit.html")
public class FormSubmitController {

    public List<Attribute> setupAttributes(){
        return List.of(new Attribute("result", ""));
    }

    public List<Attribute> displayName(Integer i, HashMap form, String s){
        return List.of(new Attribute("result",
            form.get("firstName") + " " + form.get("lastName")
        ));
    }

}

Form submit as form

Automatically map a form submit to a POJO

JDK22.0.2 w/ Medusa 0.9.6-SNAPSHOT

Client

<form m:submit="displayNameAsForm(567, :{form}, 'hi')">
    <label for="firstName2">First name: </label>
    <input type="text" id="firstName2" name="firstName" value="hello" />

    <label for="lastName2">Last name: </label>
    <input type="text" id="lastName2" name="lastName" value="world" />

    <input type="submit" value="Submit">
</form>

<div class="example-result" th:text="${resultFromForm}"></div>

Server

package sample.getmedusa.showcase.samples.textinputs.formsubmit;

import io.getmedusa.medusa.core.annotation.UIEventPage;
import io.getmedusa.medusa.core.attributes.Attribute;

import java.util.HashMap;
import java.util.List;

@UIEventPage(path = "/detail/sample/form-submit", file = "/pages/sample/form-submit.html")
public class FormSubmitController {

    public List<Attribute> setupAttributes(){
        return List.of(new Attribute("resultFromForm", ""));
    }

    public List<Attribute> displayNameAsForm(Integer i, SampleForm form, String s){
        return List.of(
            new Attribute("resultFromForm", form.firstName() + " " + form.lastName())
        );
    }

    public record SampleForm(String firstName, String lastName) { }

}