Expand my Community achievements bar.

SOLVED

Unable to access the path variable outside '@postconstruct'

Avatar

Level 1

Here, path is variable that stores the path value of video

P00rnima_0-1655984195247.png

=> 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)

P00rnima_2-1655985364097.png

Any insights please?

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@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

 

View solution in original post

3 Replies

Avatar

Community Advisor

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

Avatar

Correct answer by
Community Advisor

@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

 

Avatar

Community Advisor

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