Hello,
I am trying to redirect to .html page from servlet -
String cased = "abcd4444";
response.sendRedirect(redirect_URL);
I need to pass the caseId parameter to redirect url from servlet and show it on the page. What is the best way to achieve it ? Is it possible via sling Model ?
Solved! Go to Solution.
Views
Replies
Total Likes
It is not possible to pass query string value using the sling model to another page, you can use the below code.
response.sendRedirect(redirect_URL + "?cased=" + cased);
in the redirected page you need to read the query string value for this, you can create a sling model class in the post construct method of sling model you can read the query string value and assign it to the property.
In the redirected page where you want to render cased, you need to create an object for the sling model and display cased value.
you can also use Jquery to read query string value and render on the page, it is up to your comfort level
Instead of passing via the query string, you can also follow another approach which is storing cased value in the browser cookie and read either in the sling model or in javascript and display on the redirected page.
@bunny87948290
Can you try the combination of below two references:
https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/redirect-to-a-content-page...
https://stackoverflow.com/questions/17001185/pass-hidden-parameters-using-response-sendredirect
Thanks,
Nikhil Kumar
Views
Replies
Total Likes
@bunny87948290,
There's a procedure in how code is structured while passing params from invoking a response.sendRedirect(), have a look at this article where code examples are present, https://www.mysamplecode.com/2011/06/java-servlet-redirect-vs-forward.html
Views
Replies
Total Likes
It is not possible to pass query string value using the sling model to another page, you can use the below code.
response.sendRedirect(redirect_URL + "?cased=" + cased);
in the redirected page you need to read the query string value for this, you can create a sling model class in the post construct method of sling model you can read the query string value and assign it to the property.
In the redirected page where you want to render cased, you need to create an object for the sling model and display cased value.
you can also use Jquery to read query string value and render on the page, it is up to your comfort level
Instead of passing via the query string, you can also follow another approach which is storing cased value in the browser cookie and read either in the sling model or in javascript and display on the redirected page.
Views
Replies
Total Likes
I feel this request.getSession().setAttribute("caseID",caseID) is not recommended one and it is tied to a user session, what if the request goes to other publish server, you need to use sticky connections and setup dispatcher configurations. please refer this for more information https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/new-httpsession-on-every-r...
create a sling model called CaseDetails, inside the sling model. you can read the cookie value by using the below code, once you have cookie value assign it to the property.
public String getCaseID()
{
return readCookie(SlingHttpServletRequest slingRequest)
}
private String readCookie(SlingHttpServletRequest slingRequest) {
Cookie[] cookies = slingRequest.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
if (cookie.getName().equals("cased")) {
return cookie.getValue();
}
}
}
}
Create a new component and instantiate data-sly-use.case="com.org.CaseDetails" then in the sightly call case.getCaseID to display cased id on the page.
You need to author this component on the redirected page.
Javascript:
You can not read query string in the JS use API, you need to use plain javascript, put a placeholde on the redirected page something lie this <span id="cased"></span>
In javascript/jquery write logic to read cookie value then if you use jQuery you need to assign cookie value to cased somethig like this
$("cased").text(<cookievalue">)
Views
Replies
Total Likes
Got it! This is helpful, will get it try. One more question - Once I get the case ID, I have text component and text authored as "Thank you for your information, your case ID is caseIDNew" and on slightly page, have this script added to replace "caseIDNew" with the ID I get from sling model or JS, it seems to work, but just want to check if there is any other approach.
<sly data-sly-use.info="${'formparam.js'}">
<script id="temp-script">
$(document).ready(function () {
$('body').find('*').each(function() {
var html= $(this).html().replace(/caseIDNew/g, ${info.title @ context='scriptString'});
$(this).html(html);
});
});
</script>
</sly>
Views
Replies
Total Likes
If you use java sling model or js use api, you do not need script, you can directly use HTL expression language., ${' Thank you for your information, your case ID is {1}' @ format=[cased.getCaseId]}, refer this https://docs.adobe.com/content/help/en/experience-manager-htl/using/htl/expression-language.html
if you want to read query string value using JS Use API, then the above HTL expression language also should work.
If you want to read query string value using plain js, then you need to follow below code.
<script id="temp-script">
$(document).ready(function () {
$('body').find('*').each(function() {
var html= $(this).html().replace(/caseIDNew/g, ${info.title @ context='scriptString'});
$(this).html(html);
});
});
Views
Replies
Total Likes
Views
Replies
Total Likes
So I used the model code as you have mentioned, had to update little but as it was giving error for SlingHttpServletRequest in model class. So here is my code -
In the servlet, I am setting cookie value as before redirect -
Cookie cookie = new Cookie("caseid", caseId);
response.addCookie(cookie);
response.sendRedirect(redirect_URL);
In HTL/Slightly , printing the value as -
<sly data-sly-use.case="xyz.core.sling.models.CaseDetails">
<h1> Thank you for information, your case id is:${case.getCaseID} </h1>
</sly>
It is is printing all the log statements from model class, but can't find the cookie with name caseid, so it's not displaying on page, Am I missing anything while setting cookies ?
I think setting cookie max age and path worked! Thanks for the sling model solution.
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies