I'm trying to implement JSF backing beans using CDI beans as suggested by the depreciation of @ManagedBean and it's scope annotations, but I'm struggling with the right use examples, I'm trying to implement view backing bean with @Model (javax.enterprise.inject.Model) which is @Named @RequestScoped.
I found this question but it's using a ViewScope bean, how would I implement the same functionality with RequestScoped (Preferably @Model), What is best practice use of @Model in general?
Edit 1: I tried creating a new Product in the EditProduct PostConstruct:
@Model
public class EditProduct {
private Product product; // +getter +setter
@Inject
private ProductService productService;
@PostConstruct
public void init(){
product = new Product();
}
public String save() {
productService.save(product);
return "/products?faces-redirect=true";
}
// ...
}
and then setting the product via
<f:viewParameter name="product-id" target="#{editProduct.product}"
converter="#{productConverter}" />
it's working but I'm looking for a best practice.
A request scoped backing bean is meant to keep the application memory footprint as low as possible hence using them for supporting views with @Model
annotation makes a lot of sense, the draw back is having to reach for the persistence data storage on every request that deals with data so a best use case for @Model bean is:
Basically every thing. things like:
Those things are easily done best in request scoped beans, but then what are the roles of other beans?
In simplistic terms we can assume:
@ViewScoped
to support data heavy pages where user edits data with many interactions and each interaction is a request but hitting the database for each one will be costly.
@SessionScoped
for session data, authentication, credentials and configuration for the user.
@ApplicationScoped
the state-full singleton of CDI.
.... each other scope has it's uses, but for a good web application @Model
should be the default and the others has specific uses cases.