Here, path is variable that stores the path value of video
=> path is having value (i.e. path1 & path2 prints value)
But, the following method is defined outside @PostConstruct and here path value is empty (i.e. path3 & path4 are empty)
Any insights please?
Solved! Go to Solution.
Views
Replies
Total Likes
@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
Hi @P00rnima
The code you have posted here looks good and I don't see an issue here. Maybe you can post the whole code if possible?
And also the 'path' would have global scope and it's not a necessity to use 'this' keyword here.
Thanks
@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
Hi @P00rnima ,
The @PostConstruct annotation can be used to add methods which are invoked upon completion of all injections/global variables. Please check below piece of code
private String path;
@PostConstruct protected void init() {
path = "custompath"
} public String getPath() { return path; }
Reference: https://sling.apache.org/documentation/bundles/models.html#postconstruct-methods-1
Hope that helps!
Regards,
Santosh
Views
Likes
Replies
Views
Likes
Replies
Views
Likes
Replies