A view can be created in two possible ways:
-
Programmatically – by creating
View
instance, 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
ViewRepository
.View
instances are created and cached when XML-based descriptor is deployed. Further on the required view can be retrieved in any part of the application code by repository call providing the entity class and 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 link to ViewRepository
can be obtained using Metadata infrastructure interface. getView()
methods are used to retrieve View
instance from the repository. deployViews()
methods from AbstractViewRepository
basic implementation are used to deploy XML view descriptors to the repository.
Two views named _local
and _minimal
are available in the repository for each entity by default:
-
_local
contains all local entity attributes. -
_minimal
contains the attributes which are included to the name of the entity instance and which are specified by @NamePattern annotation. If@NamePattern
annotation is not specified in the entity, this view does not contain any attributes.
The detailed structure of XML descriptors is provided in Section A.11, “views.xml”.
The example below shows view descriptor for Order
entity which provides loading of all local attributes, associated Customer
and ordered Items
collection:
<view class="com.sample.sales.entity.Order" name="orderWithCustomer" extends="_local"> <property name="customer" view="_minimal"/> <property name="items" view="itemsInOrder"/> </view>
The recommended way of grouping and deployment of view descriptors is as follows:
-
Create
views.xml
file in the global module insrc
root and place all view descriptors which should be globally accessible (i.e. on all application tiers into it. -
Register this file in cuba.viewsConfig application property of all used blocks, i.e. in
app.properties
of the core module,web-app.properties
of the web module, etc. This will ensure automatic deployment of the views upon application startup in the repository (SeeAbstractViewRepository.init()
method). -
If there are views which are used only in one application block, they can be specified in the similar file in 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.