URL from address bar | Community
Skip to main content
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by SantoshSai

 

Hi @sarah_la ,

If you are adapting a request to your Sling Model, then you can use 

@SlingObject
private SlingHttpServletRequest request;

then, get URL from request using 

request.getRequestURI();

Please refer sample code snippet as below,

package com.mysite.core.models;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.SlingObject;

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

    @SlingObject
    private SlingHttpServletRequest request;

    public String getUrl(){
        return request.getRequestURI(); //get uri from request using
    }
}

Hope that helps!

Regards,
Santosh

1 reply

SantoshSai
Community Advisor
SantoshSaiCommunity AdvisorAccepted solution
Community Advisor
November 28, 2022

 

Hi @sarah_la ,

If you are adapting a request to your Sling Model, then you can use 

@SlingObject
private SlingHttpServletRequest request;

then, get URL from request using 

request.getRequestURI();

Please refer sample code snippet as below,

package com.mysite.core.models;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.SlingObject;

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

    @SlingObject
    private SlingHttpServletRequest request;

    public String getUrl(){
        return request.getRequestURI(); //get uri from request using
    }
}

Hope that helps!

Regards,
Santosh

Santosh Sai