Say I have an Entity class, Car.
@Entity public class Car
My IDE lets me automatically generate session beans from entity classes, so I end up with a CarFacade
@Stateless public class CarFacade
I can also generate JSF Managed beans
@ManagedBean @RequestScoped public class RegistrationController
I can understand the meaningful difference between the Entity class and other beans, but what are the differences between a stateless session bean and a managed bean? I read that a stateless session bean is for implementing your business logic that operates on the entities and managed beans are for interacting with the web-based front-end, by having the webpage call methods on the managed bean, and having the managed bean call business methods on the session bean.
So in my example, the RegistrationController would feature a +register(String carRegistration) method that the webpage would call. The RegistrationController would in turn instantiate a Car and call +create(Car car) on the session bean, which would persist it.
Is this correct?
1) BB: A backing bean is any bean that is referenced by a form. MB: A managed bean is a backing bean that has been registered with JSF (in faces-config. xml) and it automatically created (and optionally initialized) by JSF when it is needed.
Managed beans are container-managed objects with minimal supported services, such as resource injection, life cycle callbacks and interceptors, and have the following characteristics: A managed bean does not have its own component-scoped java:comp namespace.
There are 3 types of session bean. 1) Stateless Session Bean: It doesn't maintain state of a client between multiple method calls. 2) Stateful Session Bean: It maintains state of a client across multiple requests.
The JSF managed bean is the glue (controller) between the entity (model), the JSF page (view) and the enterprise bean (business service).
So, yes, you are basically right in your understanding that the JSF page should invoke the managed bean's action method which should in turn delegate the model and the action further to the business service and eventually handle the navigation outcome based on the result of the service call.
But you are not entirely right in how the model should be used and passed around. Usually you make the model a property of the managed bean so that you can just bind it to the form's input elements and finally pass it unchanged through to the business service.
E.g.
<h:inputText value="#{registrationController.car.make}" /> <h:inputText value="#{registrationController.car.model}" /> <h:inputText value="#{registrationController.car.year}" /> <h:commandButton value="Save" action="#{registrationController.save}" />
with
private Car car; private @EJB CarFacade carFacade; public RegistrationController() { this.car = new Car(); } public String save() { carFacade.create(car); return "someoutcome"; } // ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With