2

Take this POJO as the domain.

public class Invoice {
    private String codeNumber;
    private BigDecimal amount;
    private String notes;

    private List<InvoiceRows> rows;      
}

I have a form which takes that as @ModelAttribute. The form has got four text inputs: codeNumber, amount, notes and "emailAddressToNotify". The POJO's "rows" property is filled in another place, so it's ignored and that's right.

On the other side, the "emailAddressToNotify" property is sent by the form's 'POST' but obviously it's not related with the Invoice POJO. Actually, I need it in my controller "processForm()" method.

Can I simply add a @RequestParam parameter to get it?

And here's the second part of the question, as it's related to the first one:

in my web application, I will often write "huge" forms, in which the fields do not always correspond to the fields of one of my domain objects. So, I ask you for what is the best practice:

Do I have to write a "form-dedicated" POJO, so I can always use @ModelAttribute and validate fields the easy way? or... ?

Fabio B.
  • 9,138
  • 25
  • 105
  • 177

1 Answers1

0

Can I simply add a @RequestParam parameter to get it?

I think you're asking if you can use both @RequestParam and POJO binding in the one controller method. Yes, this is possible. Maybe you should've just tried it rather than asking about it?

Do I have to write a "form-dedicated" POJO, so I can always use @ModelAttribute and validate fields the easy way? or... ?

This depends... You need to provide an example of what you're doing for a useful answer. I.e. are you wanting to bind to multiple POJOs with one form, do the field names simply not match, do you have extra fields in the POJO you don't want displayed, do the POJOs have heavily nested classes, etc...

Its quite common to have a simple, flat bean/POJO to use as a "form-backing bean". They're often also called a DTO (data transfer objects). See What is Data Transfer Object?.

Community
  • 1
  • 1
nickdos
  • 8,348
  • 5
  • 30
  • 47