4.1.2. Using Client Tier Beans
Let’s complicate the task from the previous section a bit. Now we want to invoke the calculation both from the customer browser and editor screens. To not repeat yourself, we should extract the logic to a common place available for both controllers. It can be a managed bean of the client tier.
A managed bean is a class annotated with the @Component
annotation. It can be injected into other beans and screen controllers, or obtained via the AppBeans.get()
static method. If the bean has a separate interface, you can access the bean through the interface instead of the class.
Please note that in order to be accessible for screen controllers, the bean must be located in global, gui or web modules of your project. In the former case the bean will be also accessible for the middleware.
See the Calculate discount button on both browser and editor screens of the demo application and the implementation:
-
CustomerBrowse.java - browser controller.
-
CustomerEdit.java - editor controller.
-
DiscountCalculator.java - discount calculator bean. It uses DataManager to load the list of orders for the given customer from the database.