I want to do a path check in Model Class | Community
Skip to main content
Level 6
November 24, 2021
Solved

I want to do a path check in Model Class

  • November 24, 2021
  • 2 replies
  • 1504 views

Hi,

 

I have added one component in experience fragment.....Now I want to make the page available in every page where page name is "Product"

 

Now I am facing challenge in geting the page name in ModelImpl.class......Whenever I uses

 currentPage=resourceResolver.adaptTo(PageManager.class).getContainingPage(resource); currentPage.getPath();

I am getting path of experience fragment

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 Siva_Sogalapalli

@siva_sogalapalli 

 

In HTL Text is not printing anything

 

If I remove postconstruct and SlingHttpServletRequest.class from adaptable then only it is working


did you try to print the log in the model and see whether you're getting page details in the log or not? so that you know whether it's an issue with sling model or issue at HTL level. 

 

I've both postconstruct and SlingHttpServletRequest.class adaptable in my test model and works for me: 

@Model(adaptables = {SlingHttpServletRequest.class, Resource.class })
@Inject
private Page currentPage;

private String pagePath;

private String pageName;

private String pageTitle;
@PostConstruct
protected void init() {
log.debug("** Start of sling Model **");
pagePath = currentPage.getPath();
pageName =currentPage.getName();
pageTitle = currentPage.getPageTitle();
}

And in sightly:

 

<sly data-sly-use.model="com.test.TestModel">

    ${model.pagePath}<br>
    ${model.pageTitle}<br>
    ${model.pageName}

</sly> 

2 replies

Siva_Sogalapalli
Community Advisor
Community Advisor
November 24, 2021

I could simply get the page name, path and title with sling model itself. it works on both experience fragments as well as page. 

 

@Inject
private Page currentPage;

private String pagePath;

private String pageName;

private String pageTitle;
@PostConstruct
protected void init() {
log.debug("** Start of sling Model **");
pagePath = currentPage.getPath();
pageName =currentPage.getName();
pageTitle = currentPage.getPageTitle();
}

 

Could you please elaborate on requirement? I see you mentioned "Now I want to make the page available in every page where page name is "Product"

Are you trying to include the experience fragment dynamically on all the pages where the page name is "Product" ? 

Ronnie09Author
Level 6
November 24, 2021

This is working but component is not displaying anything

@Model(adaptables = {SlingHttpServletRequest.class, Resource.class}, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)

public class TextingModelImpl {
    
    @ValueMapValue
    private String text;
    
    @Inject
    private Page currentPage;
 
@PostConstruct
    public void init()  {
        if(!page.getName().equalsIgnoreCase("Product"))
        {      
this.text=""; } } public String getText() {
return text;
}
}
Ronnie09Author
Level 6
November 24, 2021

@siva_sogalapalli 

 

In HTL Text is not printing anything

 

If I remove postconstruct and SlingHttpServletRequest.class from adaptable then only it is working

BrianKasingli
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
November 25, 2021

When you have a component with Sling Model backend, the currentPage object is returned to you via the context of where your component lives.

Example A

When the Sling Model calls currentPage, this is actually getting the current page of its contained page, in this case, it is the experience fragment itself. So when you are viewing the content fragment from a url like /content/experience-fragments/xf/test-1/master.html, this is expected.

 Example B
When the Sling Model calls currentPage, this is actually getting the current page of its contained page, in this case, it is the content page itself, giving you theexpected results.


The solution here is to test your experience fragment from the content page context, your currentPage object should be returned to you as expected. 

////////////////////////////// Sling Model: boolean isProductPage = currentPage.getName().equals("Product"); if (isProductPage) { do something because this is a page with the name equals to "Product". } ////////////////////////////// Sightly: <sly data-sly-test="${currentPage.name == 'Product'}> do something because this is a page with the name equals to "Product". </sly>