Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Thanks to this you can make your code even cleaner. Check the following code snippets and see how Report

Code Block
languagejava
titleFollow 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
languagexmltitleClass 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
languagexml
titleModel 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
languagexmltitleAvoiding bidings
<%@page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
<%@include file="/libs/foundation/global.jsp"%>
<slice:defineObjects/>

Richtext content: ${model.text}

...