Micronaut - What is the @ModelAttribute equivalent

194 Views Asked by At

In Spring I know I can bind thymeleaf form inputs to a command object using @ModelAttribute in the controller, but is there an equivalent in Micronaut? I can't see it documented anywhere

1

There are 1 best solutions below

0
saw303 On

Mapping form data in Micronaut is straight forward.

Given a form containing two input fields

  • <input name="name"/>
  • <input name="location"/>

you can handle them on the backend either

Binding them to parameters

@Controller("/form")
public class FormController {

    @Post
    public HttpResponse processForm(String name, String location) {
        return HttpResponse.created();
    }

}

or

Bind form data to POJO

public class MyForm {

   private String name;
   private String location;

   // getter/setter omitted
}

@Controller("/form")
public class FormController {

    @Post
    public HttpResponse processForm(@Body MyForm) {
        return HttpResponse.created();
    }

}