Basics - 4.0
This section describes basics of Slice - it gives an overview of how Slice works and how it uses dependency injection to build and provide models.
Overview
The above figure depicts a simplified model of working:
- Components written Sightly way or as JSP scripts are using respectively
data-sly-use.model
attribute or<slice:lookup>
tag to fetch a required model of data (Java object). - Tags resolves its models.
- The
data-sly-use.model
adapts a Resource to the model using Adapters Factory provided by Slice. You don't need to write your own adapters. Under the hood it uses the same mechanism as<slice:lookup>
tag. - The
<slice:lookup>
tag uses Guice Injector to create requested model and map it from current Resource.
- The
- Tag exposes resolved model (Java object) to the calling script, either Sightly or JSP.
- Script uses the model to render its view.
Sightly side by side with JSP
<div data-sly-use.model="com.example.components.text.TextModel"> <p>${model.text}<p> </div>
<slice:lookup var="model" type="<%=com.example.components.text.TextModel.class%>" /> <p>${model.text}</p>
As you can see, both examples are very similar. Usage is very similar.
model
is a variable that is used to access properties of the model. For example to output vale of text
property of TextModel
you simply do: ${model.text
}
Models - @SliceResource
and @JcrProperty
Models are simple Java classes, and what is great is a fact that you can use the same model both for Sightly and for classic JSP scripts.
The @SliceResource
annotation is used to inform Slice that such a class should be mapped from current resource by built-in object-resource Mapper. An example class looks like this:
@SliceResource public class TextModel { @JcrProperty private String text; public String getText() { return text; } }
Fields annotated by @JcrProperty
will be set with values of corresponding properties from current resource. To let Slice know what resource you'd like to map to an object you need to use ModelProvider
(see below). After its get
method has been called (as in example below), the returned object is mapped and its text
field stores a value of the current resource's text property. When you use slice:lookup
or data-sly-use
then the ModelProvider
is called automatically and you don't have to write any code!
ModelProvider
ModelProvider
(described in details at ModelProvider) is a core interface in Slice and is used for providing objects (models). Its functionality is very similar to injector's getInstance()
; however it is extended by a resource, so that object fields can be mapped using specified resource (or path). A simple use of ModelProvider
:
ModelProvider modelProvider = injector.getInstance(ModelProvider.class); TextModel model = modelProvider.get(TextModel.class, resource);
ModelProvider
is accessible through injector (line 1). The whole magic is done in line 2. modelProvider#get
takes two parameters:
- class - class of an object we want to have
- resource - JCR resource which will be read and mapped to object fields.
In order to map an object from specified resource, the object must be annotated by @SliceResource
annotation.
Summary
The subsections above described very briefly core mechanisms of Slice. To sum it up, take a look at the image below which put all these things together.