@p00rnima In the sling model if you are getting any variable with the help of the annotation @Inject, @SlingModel, @Inject @Via("resource"), etc then those variable have the global scope.
But if you are having any variable that do not have the annotations associated with it, then those variables have to be assigned the value using the business logic within the @PostConstruct method, or any other method, but @PostConstruct is recommended.
Sample Code
package com.mysite.core.models;
import static org.apache.sling.api.resource.ResourceResolver.PROPERTY_RESOURCE_TYPE;
import javax.annotation.PostConstruct;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.models.annotations.Default;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.InjectionStrategy;
import org.apache.sling.models.annotations.injectorspecific.OSGiService;
import org.apache.sling.models.annotations.injectorspecific.SlingObject;
import org.apache.sling.models.annotations.injectorspecific.ValueMapValue;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import java.util.Optional;
@Model(adaptables = Resource.class)
public class HelloWorldModel {
@ValueMapValue(name=PROPERTY_RESOURCE_TYPE, injectionStrategy=InjectionStrategy.OPTIONAL)
@Default(values="No resourceType")
protected String resourceType;
@SlingObject
private Resource currentResource;
@SlingObject
private ResourceResolver resourceResolver;
private String message;
@PostConstruct
protected void init() {
PageManager pageManager = resourceResolver.adaptTo(PageManager.class);
String currentPagePath = Optional.ofNullable(pageManager)
.map(pm -> pm.getContainingPage(currentResource))
.map(Page::getPath).orElse("");
message = "Hello World!\n"
+ "Resource type is: " + resourceType + "\n"
+ "Current page is: " + currentPagePath + "\n";
}
public String getMessage() {
return message;
}
}Hope this helps!
Thanks