How to refer a sling model with in a sling servlet? | Community
Skip to main content
Level 2
Mar 20, 2018 5:29 PM UTC
Solved

How to refer a sling model with in a sling servlet?

I have a sling model which inject an OSGi service. The request parameter for the service call is configurable. This service request is also generated in the sling model. This will invoke an API call and process the result in the sling model. I need to invoke this sling model inside a sling servlet call because a click event should trigger this functionality. So is there a way to invoke a sling model from a sling servlet.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
OlivBur
OlivBurAccepted solution
Level 2
Mar 27, 2018 11:37 AM UTC

Which url do you use to execute your click request to your servlet?

It is Best Practice to register your serlvet with a resourceType and if needed some selector or extension. With this you simply then can call an url like this for example: /content/myweb/par/componentOne.html

Then in the Servlet you will have the current resource available with request.getResource(). From this you can easily read your authored data or also adapt it to some Sling model, etc.

14 replies

Level 2
Mar 27, 2018 4:36 PM UTC

Thanks OlivBur for reminding me that. My servlet is already registered as resource type. I almost forgot about the getResource, my bad.

Translate
OlivBur
OlivBurAccepted solution
Level 2
Mar 27, 2018 11:37 AM UTC

Which url do you use to execute your click request to your servlet?

It is Best Practice to register your serlvet with a resourceType and if needed some selector or extension. With this you simply then can call an url like this for example: /content/myweb/par/componentOne.html

Then in the Servlet you will have the current resource available with request.getResource(). From this you can easily read your authored data or also adapt it to some Sling model, etc.

Translate
Level 2
Mar 25, 2018 3:18 AM UTC

Thanks for your suggestion but it may need to include login credentials specific for each environment, right. I was just curious about whether a sling model can be referred from a sling servlet. As it is not possible, I'll look for a different approach that suits my requirements. Thank you once again.

Translate
smacdonald2008
Level 10
Mar 24, 2018 2:35 PM UTC

You can read JCR data from servlet using Node API directly from servlet

Translate
Level 2
Mar 24, 2018 2:00 PM UTC

As I have authorable parameters I have to use the Sling Model to bring the author configured data to backend. Is there any better option to bring JCR content data through servlet.

Translate
smacdonald2008
Level 10
Mar 24, 2018 1:36 PM UTC

Why do you need Sling Model to generate JSON? How do plan on invoking the model? I have never seen a servlet invoke a model like it can a Service.

You can invoke a Service using @Reference from a servlet. 

Translate
Level 2
Mar 24, 2018 10:31 AM UTC

I need to call a rest service by passing a JSON parameter and accept a JSON result. This service invocation should be triggered by a click event. Some request parameters should be configurable to the author, some should be read from cookie and another parameter should be passed from front end by a click event.

I created a sling model to read all JCR properties and to read some values from cookie. The JSON request Param will be generated inside this slingModel. I created an OSGi service which call the rest service . This OSGi service is referred inside the sling model. I also created a sling servlet which accept one value during event click. This value should be passed to the sling model. If that is possible then all the needed request parameters would be available at the sling model and it can generate the request JSON and thereby can call the OSGi service.

Translate
smacdonald2008
Level 10
Mar 24, 2018 10:13 AM UTC

What exactly are you trying to do. Purhaps there is another way of performing you use case.

Translate
Level 2
Mar 24, 2018 9:54 AM UTC

Not yet.

Translate
smacdonald2008
Level 10
Mar 24, 2018 9:47 AM UTC

Did you get this solved?

Translate