Versions Compared

Key

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

...

In some cases the default name matching between properties and fields is not enough, e.g. if you want to map jcr:title property. In such cases you can specify a name of a property from which to read the value. You can do this specifying the String argument in @JcrProperty which will instruct mapper to read a property of specified name.

...

It processes fields which are annotated with com.cognifide.slice.mapper.annotation.SliceReference. It allows for mapping of models from a different resource (path) than a current resource

The @SliceReference annotation takes a path as a value. The value can be either an absolute path (like /content/test/home/jcr:content/par) or child path (like childResource/grandChildResource) - then it will be resolved relatively to current resource. Bear in mind that parent resources (like ../../test) are not supported.

The value of a path can contain placeholders in form of: ${placeholderName}. This can be useful when the path is dynamic and changes depending on request, e.g. contains language. Instead of hardcoding the language in the path (like: /content/site/en/home), you can put placeholder and have this path to be dynamically evaluated, e.g. /content/site/${language}/home.

...

The InitializableModel interface has only one, parameterless method: void afterCreated(). Here's an example use:

Code Block
languagejava
@SliceResource
public class ExampleModel implements InitializableModel {
    
    private final static Logger LOG = LoggerFactory.getLogger(ExampleModel.class);
	
    @JcrProperty
    private String text;
    
    @Override
    public void afterCreated() {
        if (text == null) {
		    LOG.warn("There is no text property in the resource");
	    }
	}
}

How to extend mapper?

Writing your own processor

...

@PreMapping and @PostMapping annotations

Aside from InitializableModel there are method annotations introduced in Slice 4.3.

Having a model annotated with @SliceResource you can annotate method with @PreMapping or @PostMapping annotation. Such method will be called before or after mapping is done respectively.

Code Block
languagejava
@SliceResource
public class MyComponentModel {

  @JcrProperty
  private String property;

  @Inject
  private ModelProvider modelProvider;

  @PreMapping
  public void preMapping() {
    // modelProvider is already set; property is not set
  }

  @PostMapping
  public void postMapping() {
    // modelProvider and property are already set
  }
}

 

How to extend mapper?

Include Page
How to extend mapper - 4.3
How to extend mapper - 4.3