Expand my Community achievements bar.

SOLVED

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

Avatar

Level 2

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

1 Accepted Solution

Avatar

Correct answer by
Employee

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

View solution in original post

2 Replies

Avatar

Correct answer by
Employee

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

Avatar

Level 10

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() ;
}
}