how to get the url query string paramters value

Avatar

Avatar

niks1

Avatar

niks1

niks1

01-03-2021

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>
 
 

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar

Anudeep_Garnepudi

MVP

Avatar

Anudeep_Garnepudi

MVP

Anudeep_Garnepudi
MVP

02-03-2021

@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>

 

 

 

 

niks1

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
Anudeep_Garnepudi

@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>

niks1

@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'}"

Anudeep_Garnepudi
@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.
niks1

@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.

Anudeep_Garnepudi
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.
niks1
@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
Anudeep_Garnepudi
Updated above code is working as expected for me.
Anudeep_Garnepudi
One observation is if it is data-sly.test the it should be data-sly-test. replace .test with -test and check.
niks1

@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

 

Anudeep_Garnepudi
@niks1 Updated the complete code. Working perfectly for me.
niks1

@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;
}
 
 
Anudeep_Garnepudi
@niks1 Which is expected right? I markup inside data-sly-test will render only if condition met.
aem_dev2
@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..
Anudeep_Garnepudi
@niks1, @aem_dev2 There should be null check for parameter, can find in above sling model example code snippet.

Answers (1)

Answers (1)

Avatar

Avatar

jbrar

Employee

Avatar

jbrar

Employee

jbrar
Employee

02-03-2021

Please check similar question which is already answered at [1]

 

[1] https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/not-able-to-use-slingreque...