Expand my Community achievements bar.

SOLVED

I want to do a path check in Model Class

Avatar

Level 7

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

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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> 

View solution in original post

6 Replies

Avatar

Community Advisor

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" ? 

Avatar

Level 7

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;
}
}

Avatar

Level 7

@Siva_Sogalapalli 

 

In HTL Text is not printing anything

 

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

Avatar

Correct answer by
Community Advisor

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> 

Avatar

Level 7

@Siva_Sogalapalli 
Thank you so much it is working fine now


How could I write Junit for this? 

Avatar

Community Advisor

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.

BrianKasingli_2-1637818528956.png

 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.

BrianKasingli_3-1637818542796.png


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>