Versions Compared

Key

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

...

Every call of ModelProvider#get(class, resource) within a single request causes that a resource (or path) passed as a parameter is pushed to the stack of current execution, then the guice injector is called, and after the object has been created the resource is popped from the stack. This resource is available for all other classes as a so called current resource and can also be injected to any object. In the below example the resource injected through constructor is just the current resource - the resource from the top of the execution stack. It should be noted that at the beginning of execution (most usually in <slice:lookup>), the resource from sling request (request.getResource() is passed to ModelProvider (exactly as it is described in here).

Code Block
java
java
public class SimpleModel {
    
    private final String text;

    @Inject
    public SimpleModel(final Resource resource) {
        ValueMap valueMap = resource.adaptTo(ValueMap.class);
        text = valueMap.get("text");
    }
    ...
}

...