Unable to access the path variable outside '@postconstruct' | Community
Skip to main content
June 23, 2022
Solved

Unable to access the path variable outside '@postconstruct'

  • June 23, 2022
  • 3 replies
  • 1109 views

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?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by ShaileshBassi

@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

 

3 replies

JeevanRaj
Community Advisor
Community Advisor
June 23, 2022

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

ShaileshBassi
Community Advisor
ShaileshBassiCommunity AdvisorAccepted solution
Community Advisor
June 23, 2022

@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

 

SantoshSai
Community Advisor
Community Advisor
June 23, 2022

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

 

Santosh Sai