Expand my Community achievements bar.

SOLVED

how to get the url query string paramters value

Avatar

Level 3

Hi team,

 

I have a requirement where in we should not load the launch based on the <xxx> = true parameter value.

 

 we are loading launch as below :

<sly data-sly-call="${headlibRenderer.headlibs @
designPath = page.designPath,
staticDesignPath = page.staticDesignPath,
clientLibCategories = page.clientLibCategories,
clientLibCategoriesJsHead = page.clientLibCategoriesJsHead,
hasCloudconfigSupport = page.hasCloudconfigSupport}"></sly>
 
 
1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@niks1  

Below implementation working for me as expected.

Sling Model:

 

 

@Model(adaptables = {Resource.class, SlingHttpServletRequest.class})
public class DemoModel {

	@SlingObject
	SlingHttpServletRequest request;

	private String launchEnabled = "false";

	public String getLaunchEnabled() {
		if (request.getParameter("launchDisabled") == null || request.getParameter("launchDisabled").equalsIgnoreCase("false"))
			launchEnabled = "true";
		return launchEnabled;
	}
}

 

 

HTL:

 

 

<div data-sly-use.demo="com.blogspot.adapttoaem.core.models.DemoModel">
    <sly data-sly-test="${demo.launchEnabled == 'true'}"> 
        <p>Launch Enabled</p>
    </sly>
</div>

 

 

 

 

View solution in original post

21 Replies

Avatar

Correct answer by
Community Advisor

@niks1  

Below implementation working for me as expected.

Sling Model:

 

 

@Model(adaptables = {Resource.class, SlingHttpServletRequest.class})
public class DemoModel {

	@SlingObject
	SlingHttpServletRequest request;

	private String launchEnabled = "false";

	public String getLaunchEnabled() {
		if (request.getParameter("launchDisabled") == null || request.getParameter("launchDisabled").equalsIgnoreCase("false"))
			launchEnabled = "true";
		return launchEnabled;
	}
}

 

 

HTL:

 

 

<div data-sly-use.demo="com.blogspot.adapttoaem.core.models.DemoModel">
    <sly data-sly-test="${demo.launchEnabled == 'true'}"> 
        <p>Launch Enabled</p>
    </sly>
</div>

 

 

 

 

Avatar

Level 3

Thanks @anudeep_Garenepudi 

Yes we have Page model and i was trying to do this via :


public String getlaunchEnabled() {
 
if((request.getParameter("launchDisabled")).equalsIgnoreCase("true")) {
return launchEnabled;
}
launchEnabled = "true";
return launchEnabled;
}
and in htl 

<sly data-sly.test.launchEnabled="${pageTrail.launchEnabled @context='unsafe'}" data-sly-call="${headlibRenderer.headlibs @
designPath = page.designPath,
staticDesignPath = page.staticDesignPath,
clientLibCategories = page.clientLibCategories,
clientLibCategoriesJsHead = page.clientLibCategoriesJsHead,
hasCloudconfigSupport = page.hasCloudconfigSupport}"></sly>
but some how it's not working
Also , if that value is true we should not load that file else load it . Do we have any ! function in data-sly-test

Avatar

Community Advisor

@niks1 

If you want to compare Strings then ${varOne == varTwo} returns true if varOne and varTwo are equal. Or try changing launchEnabled type from String to boolean. Check the docs

https://experienceleague.adobe.com/docs/experience-manager-htl/using/htl/expression-language.html?la...

Try: 

<sly data-sly.test="${pageTrail.launchEnabled == 'true'}">  ... </sly>

Avatar

Level 3

@Anudeep_Garnepudi- I tried as suggested but it is still  not working .

public boolean getlaunchEnabled() {

return !(Boolean.parseBoolean(request.getParameter("launchDisabled"))) ;

} and

html

<sly data-sly.test.launchEnabled="${pageTrail.launchEnabled'}"

Avatar

Community Advisor
@niks1 Updated the code and HTL above. I see in your Sling Model the method name is wrong. It should be "getLaunchEnabled" instead of getlaunchEnabled. L should be caps.

Avatar

Level 3

@Anudeep_Garnepudi- when i tried above code <p> is always getting displayed as data-sly-test is checking if launchEnabled is having value or not. So i added a condition check and changed launchDisabled from boolean to String but somehow it is still not working.

public String getLaunchEnabled()

{

if (request.getParameter("launchDisabled") == null)

launchEnabled = "true" ;

return launchEnabled;

}

HTML :

<sly data-sly.test.launchEnabled="${pageTrail.launchEnabled == 'true'}" data-sly-call="${headlibRenderer.headlibs @ designPath = page.designPath, staticDesignPath = page.staticDesignPath, clientLibCategories = page.clientLibCategories, clientLibCategoriesJsHead = page.clientLibCategoriesJsHead, hasCloudconfigSupport = page.hasCloudconfigSupport}"> </sly>

 

Thanks for your help in advance.

Avatar

Community Advisor
Logic should be public String getLaunchEnabled() { if (request.getParameter("launchDisabled") == null) return "true" ; return request.getParameter("launchDisabled"); } right. You code just checking null not returning the actual parameter value.

Avatar

Level 3
@Anudeep_Garnepudi - Thanks for checking this and responding. So basically if request parameter is having launchDisable we dont want to load the data-sly-test and if it doesn't have that parameter load the data-sly-test value

Avatar

Community Advisor
One observation is if it is data-sly.test the it should be data-sly-test. replace .test with -test and check.

Avatar

Level 3

@Anudeep_Garnepudi- I tried as suggested but it is still not working . I am able to see the launch Enabled in both the cases . https://<xx> and https://<xx>/?launchDisabled=true

 

I tried exactly same code as u mentioned above ;

@SlingObject. -- as @Self is giving error i am using @SlingObject
private SlingHttpServletRequest request;

 

private String launchEnabled;

 

public String getLaunchEnabled() {

if (request.getParameter("launchDisabled") == null)
           return "true";
return "false";
}

HTML 

 

<sly data-sly-test="${pageTrail.launchEnabled == 'true'}">
<p>Launch Enabled </p>
</sly>

 

not sure what is going wrong.. I tried almost all possible combinations

 

Avatar

Level 3

@Anudeep_Garnepudi- One observation if from Model i return false then in all cases i dont see the Launch Enabled . If i return true , i always see. Here is my code.

 

public String getLaunchEnabled() {
if (request.getParameter("launchDisabled").equalsIgnoreCase("true"))
      launchEnabled = "false";
return launchEnabled;
}
 
 

Avatar

Community Advisor
@niks1 Which is expected right? I markup inside data-sly-test will render only if condition met.

Avatar

Level 2
@Anudeep_Garnepudi - So ask is - if we don't have launchDisabled in the URL then only launch should get enabled if we have that parameter, launch should not be enabled..

Avatar

Community Advisor
@niks1, @aem_dev2 There should be null check for parameter, can find in above sling model example code snippet.

Avatar

Level 3
@Anudeep_Garnepudi - Since Launch Disabled is always getting displayed even if the request url is having launchDisabled=true . In this case, launch Disabled should not be displayed. Something wrong with this if logic. if((request.getParameter("launchDisabled")).equalsIgnoreCase("true")) { return launchEnabled; }

Avatar

Level 3
@Jaideep_Brar we are on AEM 6.4 and using PageModel .. got the Parameter value but still in data-sly-test it not working

Avatar

Level 3

@Jaideep_Brar- No still not working. I tried as

Model code

 

@SlingObject
private SlingHttpServletRequest request;

private String launchEnabled;

 

public String getLaunchEnabled() {

if (request.getParameter("launchDisabled") == null)

             return "true";

return "false";

}

 

html

<sly data-sly-test="${pageTrail.launchEnabled == 'true'}">
<p>Launch Enabled </p>
</sly>

 

This is getting displayed always