How can I get the request object in SlingModel to know the requested page path in @PostConstruct method? | Community
Skip to main content
Level 2
September 26, 2016
Solved

How can I get the request object in SlingModel to know the requested page path in @PostConstruct method?

  • September 26, 2016
  • 2 replies
  • 17654 views

Hi,

I have a requirement to get the current browser URL to identify the type of URL, for doing so we need the request object also in the SlingModel with Resource model.

We are doing like as below,

After adding 2 adaptables classes like,

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

@Inject
private SlingHttpServletRequest slingHttpServletRequest;
@Inject
private Resource resource;

Still getting errors like,

26.09.2016 10:47:47.111 *WARN* [0:0:0:0:0:0:0:1 [1474867052138] GET /content/abc/homepage.html HTTP/1.1] org.apache.sling.models.impl.ModelAdapterFactory Required properties [private org.apache.sling.api.SlingHttpServletRequest org.abc1.abc.core.model.hero.HeroComponentModel.slingHttpServletRequest] on model class class org.abc1.abc.core.model.hero.HeroComponentModel were not able to be injected.

26.09.2016 10:47:47.111 *WARN* [0:0:0:0:0:0:0:1 [1474867052138] GET /content/abc/homepage.html HTTP/1.1] org.apache.sling.models.impl.ModelAdapterFactory Required properties [private org.apache.sling.api.SlingHttpServletRequest org.abc1.abc.core.model.hero.HeroComponentModel.slingHttpServletRequest] on model class class org.abc1.abc.core.model.hero.HeroComponentModel were not able to be injected.

26.09.2016 10:47:47.123 *WARN* [0:0:0:0:0:0:0:1 [1474867052138] GET /content/abc/homepage.html HTTP/1.1] org.apache.sling.models.impl.ModelAdapterFactory Required properties [protected org.apache.sling.api.resource.Resource org.abc1.abc.core.model.common.AbstractNodeNameModel.resource, private org.apache.sling.api.resource.Resource org.abc1.abc.core.model.hero.HeroComponentModel.currentResource] on model class class org.abc1.abc.core.model.hero.HeroComponentModel were not able to be injected.

Please let me know on this ?

Thanks,

Mayur

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 Feike_Visser1

Couple of ways to do this..

1. @Self

@Self 

private SlingHttpServletRequest slingHttpServletRequest;

2. Constructor 

You can also add a constructor with SlingHttpServletRequest

3. Name based

If you rename slingHttpServletRequest to just request it find a match (or you can use @Named to point to a different name).

2 replies

Feike_Visser1
Adobe Employee
Feike_Visser1Adobe EmployeeAccepted solution
Adobe Employee
October 4, 2016

Couple of ways to do this..

1. @Self

@Self 

private SlingHttpServletRequest slingHttpServletRequest;

2. Constructor 

You can also add a constructor with SlingHttpServletRequest

3. Name based

If you rename slingHttpServletRequest to just request it find a match (or you can use @Named to point to a different name).

edubey
Level 10
October 4, 2016

This should help you, tried and test code

@Model(adaptables = SlingHttpServletRequest.class) 
public class HelloWorldModel {

private final Logger logger = LoggerFactory.getLogger(getClass());

@Inject
SlingHttpServletRequest request;

private String message;

private String sel = null;

private List<SimplePage> pageList = null;

SimplePage page = null; private List<String> file;

@Inject @Source("osgi-services")

PaginationInterface service;

@PostConstruct protected void init()
{
String url = request.getRequestPathInfo().getResourcePath() ;
}
}