问题描述
说我有一个实体类,Car。 @Entity
public class Car
我的IDE让我自动从实体类生成会话bean,所以我最终得到一个CarFacade
@Stateless
public class CarFacade
我也可以生成JSF托管bean
@ManagedBean
@RequestScoped
public class RegistrationController
我可以理解Entity类和其他bean之间的有意义的区别,但是无状态会话bean和受管Bean之间的区别?我读到一个无状态会话bean用于实现您的业务逻辑,该实体和托管bean用于与基于Web的前端进行交互,通过在托管bean上调用网页调用方法,并使用托管bean调用会话bean中的业务方法。
所以在我的例子中,RegistrationController将使用网页调用的+注册(String carRegistration)方法。 RegistrationController反过来实例化一个Car,并在会话bean上调用+ create(Car car),这将持续存在。
这是正确吗?
JSF托管bean是实体(模型),JSF页面(view)和企业bean(业务服务)之间的粘合(控制器) )
所以,是的,你基本上是正确的你的理解,JSF页面应该调用托管bean的动作方法,这应该进一步委托模型和动作进一步对于业务服务,并根据服务电话的结果最终处理导航结果。
但是,如何使用和传递模型并不完全正确。通常,您将模型作为受管Bean的属性,以便您可以将其绑定到表单的输入元素,并最终将其不变地传递到业务服务。
例如
< h:inputText value =#{registrationController.car.make}/> ;
< h:inputText value =#{registrationController.car.model}/>
< h:inputText value =#{registrationController.car.year}/>
< h:commandButton value =保存action =#{registrationController.save}/>
与
私家车;
私人@EJB CarFacade carFacade;
public RegistrationController(){
this.car = new Car();
}
public String save(){
carFacade.create(car);
返回someoutcome;
}
// ...
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?
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";
}
// ...
这篇关于受管Bean和会话Bean之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!