188.8.131.52. Views Creation
A view can be created in two possible ways:
Programmatically – by creating a
Viewinstance, for example:
View view = new View(Order.class) .addProperty("date") .addProperty("amount") .addProperty("customer", new View(Customer.class) .addProperty("name") );
Typically, this way can be appropriate for creating views that are used in a single piece of business logic.
Declaratively – by creating an XML descriptor and deploying it to
Viewinstances are created and cached when the XML descriptor is deployed. Further on, the required view can be retrieved in any part of the application code by a call to
ViewRepositoryproviding the entity class and the view name.
Let us consider in details the declarative way for creation and working with views.
ViewRepository is a Spring bean, accessible to all application blocks. The reference to
ViewRepository can be obtained using injection or through the Metadata infrastructure interface.
ViewRepository.getView() methods are used to retrieve view instances from the repository.
deployViews() methods from
AbstractViewRepository basic implementation are used to deploy XML descriptors to the repository.
Three views named
_base are available in the views repository for each entity by default:
_localcontains all local entity attributes.
_minimalcontains the attributes which are included to the name of the entity instance and specified in the @NamePattern annotation. If the
@NamePatternannotation is not specified at the entity, this view does not contain any attributes.
_baseincludes all local non-system attributes and attributes defined by
The detailed structure of view XML descriptors is explained here.
The example below shows a view descriptor for the
Order entity which provides loading of all local attributes, associated
Customer and the
<view class="com.sample.sales.entity.Order" name="order-with-customer" extends="_local"> <property name="customer" view="_minimal"/> <property name="items" view="itemInOrder"/> </view>
The recommended way of grouping and deployment of view descriptors is as follows:
Create views.xml file in the
srcroot of the global module and place all view descriptors that should be globally accessible (i.e. on all application tiers) into it.
Register this file in the cuba.viewsConfig application property of all blocks, i.e. in
app.propertiesof the core module,
web-app.propertiesof the web module, etc. This will ensure automatic deployment of the views upon application startup into the repository.
If there are views which are used only in one application block, they can be specified in the similar file of this block, for example,
web-views.xml, and registered in cuba.viewsConfig property of this block only.
If the repository contains a view with certain name for some entity, an attempt to deploy another view with this name for the same entity will be ignored. If you need to replace the existing view in the repository with a new one and guarantee its deployment, specify
overwrite = "true"attribute for it.
It is recommended to give descriptive names to the views. For example, not just "browse", but "customerBrowse". It simplifies the search of views in XML descriptors.