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
Solved! Go to Solution.
Views
Replies
Total Likes
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>
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" ?
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;
}
}
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>
@Siva_Sogalapalli
Thank you so much it is working fine now
How could I write Junit for this?
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>
Views
Likes
Replies