Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn More

View all

Sign in to view all badges

How to get session inside slingmodel

Avatar

Avatar
Validate 1
Level 2
dipu2
Level 2

Likes

13 likes

Total Posts

48 posts

Correct Reply

2 solutions
Top badges earned
Validate 1
Ignite 1
Give Back 3
Give Back
Boost 5
View profile

Avatar
Validate 1
Level 2
dipu2
Level 2

Likes

13 likes

Total Posts

48 posts

Correct Reply

2 solutions
Top badges earned
Validate 1
Ignite 1
Give Back 3
Give Back
Boost 5
View profile
dipu2
Level 2

28-07-2020

Dear All,

 

I have one requirement like below. When Author will put the SKU Id in dialog , as shown in below,

dipu2_0-1595957871572.png

 

After author will put the SKU Id (for example 12345) , then this ID will search in content/dam/site/skuid folder and the document where this sku id is attached will come as link .

 

I am trying to do this logic inside

@PostConstruct
private void initModel() throws Exception {

 

But , I am not getting any session "or" resource inside  initModel().

 

Can anybody tell me how can I achieve the above scenario ?

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Establish
MVP
Nupur_Jain
MVP

Likes

165 likes

Total Posts

186 posts

Correct Reply

76 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 25
View profile

Avatar
Establish
MVP
Nupur_Jain
MVP

Likes

165 likes

Total Posts

186 posts

Correct Reply

76 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 25
View profile
Nupur_Jain
MVP

28-07-2020

Hi @dipu2 

 

In order to get session,

  • you can first get ResourceResolver object with 
    @SlingObject
    private ResourceResolver resourceResolver;

  • Then in initModel() method, you can adapt it to session like
    Session session =resourceResolver.adaptTo(Session.class);

In order to get resource, you can use
@SlingObject
private Resource resource;

 

Hope it helps!

Thanks!

Nupur

Answers (2)

Answers (2)

Avatar

Avatar
Establish
MVP
BrianKasingli
MVP

Likes

592 likes

Total Posts

574 posts

Correct Reply

220 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile

Avatar
Establish
MVP
BrianKasingli
MVP

Likes

592 likes

Total Posts

574 posts

Correct Reply

220 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile
BrianKasingli
MVP

28-07-2020

@dipu2,

It might be possible to get the session from the current session of the current request, but the session obtained may not have the correct permissions to the path: content/damsite/skuid. If the session have no permission to the content path, then you can get a content not found an exception. Also, Sling Models should be lightweight and should have minimal logic. Your requirement seems like there might be mid-weight logic that can be re-usable. This would be a great time to create an OSGIService & Service Users.

Implementation Idea:

I hope this helps,

Brian.

Avatar

Avatar
Ignite 1
MVP
Ankur_Khare
MVP

Likes

152 likes

Total Posts

363 posts

Correct Reply

56 solutions
Top badges earned
Ignite 1
Validate 10
Validate 1
Give Back 50
Give Back 5
View profile

Avatar
Ignite 1
MVP
Ankur_Khare
MVP

Likes

152 likes

Total Posts

363 posts

Correct Reply

56 solutions
Top badges earned
Ignite 1
Validate 10
Validate 1
Give Back 50
Give Back 5
View profile
Ankur_Khare
MVP

29-07-2020

Did u try to use-

@inject

SlingHttpServletRequest request;

 

init() {

request.getSession();

}