Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

I want to do a path check in Model Class

Ronnie09
Level 4
Level 4

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
SivaPrasadReddyS
Correct answer by
Level 8
Level 8

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
SivaPrasadReddyS
Level 8
Level 8

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

Ronnie09
Level 4
Level 4

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;
}
}
Ronnie09
Level 4
Level 4

@SivaPrasadReddyS 

 

In HTL Text is not printing anything

 

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

SivaPrasadReddyS
Correct answer by
Level 8
Level 8

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

BrianKasingli
Community Advisor
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>