...
Thanks to this you can make your code even cleaner. Check the following code snippets and see how Report
Code Block |
---|
language | java |
---|
title | Follow annotation |
---|
|
// Now - with @Follow annotation
@SliceResource
public class ReportComponent {
@Follow
@JcrProperty(value = "reportPath")
private ReportModel report; // this will build ReportModel based on resource path
// kept in reportPath property of current resource
public String getReportValue() {
return report.getValue();
}
}
// Before
@SliceResource
public class ReportComponent implements InitializableModel {
@JcrProperty
private String reportPath;
private ReportModel report;
private ModelProvider modelProvider;
@Inject
public ReportComponent(ModelProvider modelProvider) {
this.modelProvider = modelProvider
}
@Override
public void afterCreated() {
report = modelProvider.get(ReportModel.class, reportPath);
}
public String getReportValue() {
return report.getValue();
}
} |
...
For example, we can define the following component's definition:
Code Block |
---|
language | xml | title | Class declaration |
---|
|
<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:slice="http://www.cognifide.com/slice/1.0" xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
jcr:primaryType="cq:Component"
jcr:title="Rich Text"
slice:model="com.example.RichTextModel"/> |
In such case, we don't need to explicitly declare model in JSP file - it will be available out of the box, in a JSP bindings object, like in the example below:
Code Block |
---|
language | xml |
---|
title | Model available in bindings object |
---|
|
<%@page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
<%@include file="/libs/foundation/global.jsp"%>
Richtext content: ${bindings.model.text} |
To simplify the code even more, you can put <slice:defineObjects/> somewhere in global.jsp include - it allows you to avoid "bindings" part, so above code, can be simplified as follows:
Code Block |
---|
language | xml | title | Avoiding bidings |
---|
|
<%@page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
<%@include file="/libs/foundation/global.jsp"%>
<slice:defineObjects/>
Richtext content: ${model.text} |
...