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

Problem with @inject annotation in a sling model

Avatar

Avatar
Validate 1
Level 2
leopor
Level 2

Likes

3 likes

Total Posts

9 posts

Correct Reply

1 solution
Top badges earned
Validate 1
Boost 3
Boost 1
Affirm 1
View profile

Avatar
Validate 1
Level 2
leopor
Level 2

Likes

3 likes

Total Posts

9 posts

Correct Reply

1 solution
Top badges earned
Validate 1
Boost 3
Boost 1
Affirm 1
View profile
leopor
Level 2

18-12-2017

Hi everyone,

I have a issue with @inject annotation, I try to inject the Resource object inside a Sling Model, but when I debug the code it's null.

This is part of my code:


import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.apache.sling.models.annotations.Default;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.Optional;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.servlet.jsp.PageContext;

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

public class ProductFilter extends AbstractComponent {

   @Inject
   private Resource  resource;

Could someone help me with this issue?

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Validate 1
Level 2
leopor
Level 2

Likes

3 likes

Total Posts

9 posts

Correct Reply

1 solution
Top badges earned
Validate 1
Boost 3
Boost 1
Affirm 1
View profile

Avatar
Validate 1
Level 2
leopor
Level 2

Likes

3 likes

Total Posts

9 posts

Correct Reply

1 solution
Top badges earned
Validate 1
Boost 3
Boost 1
Affirm 1
View profile
leopor
Level 2

27-12-2017

I found a solution to my problem, it was related with de Sling Models Injectors.

The solution was install the ACS Commons content package from here:
Adobe AEM Commons

This package add a new in injector into AEM, and this inject the Resource Object into the sling model.

1381663_pastedImage_3.png
Thank you for your answers, they were useful to fix another parts of my sling model.

Answers (9)

Answers (9)

Avatar

Avatar
Boost 1
Level 1
GiurgiuRubin
Level 1

Like

1 like

Total Posts

1 post

Correct Reply

0 solutions
Top badges earned
Boost 1
View profile

Avatar
Boost 1
Level 1
GiurgiuRubin
Level 1

Like

1 like

Total Posts

1 post

Correct Reply

0 solutions
Top badges earned
Boost 1
View profile
GiurgiuRubin
Level 1

14-10-2020

use

@SlingObject
private Resource resource; 

Avatar

Avatar
Validate 10
Level 4
mjb54261515
Level 4

Likes

33 likes

Total Posts

132 posts

Correct Reply

32 solutions
Top badges earned
Validate 10
Validate 1
Boost 5
Boost 3
Boost 25
View profile

Avatar
Validate 10
Level 4
mjb54261515
Level 4

Likes

33 likes

Total Posts

132 posts

Correct Reply

32 solutions
Top badges earned
Validate 10
Validate 1
Boost 5
Boost 3
Boost 25
View profile
mjb54261515
Level 4

21-12-2017

Thats correct resource path will be exactly where current component is used.

To get page I always prefer PageManager and resourceResolver

  page = resourceResolver.adaptTo(PageManager.class).getContainingPage(resource);

@Inject @Source("sling-object")
  private ResourceResolver resourceResolver;

This is because the present condition you are using may fail when you drop in other place.

Avatar

Avatar
Validate 1
Level 2
leopor
Level 2

Likes

3 likes

Total Posts

9 posts

Correct Reply

1 solution
Top badges earned
Validate 1
Boost 3
Boost 1
Affirm 1
View profile

Avatar
Validate 1
Level 2
leopor
Level 2

Likes

3 likes

Total Posts

9 posts

Correct Reply

1 solution
Top badges earned
Validate 1
Boost 3
Boost 1
Affirm 1
View profile
leopor
Level 2

21-12-2017

Hi @smacldonald2008


When i use the injection by constructor i can see that t's pointing to the node that containt the component that use the sling model, i think that it's pointing well.


Debug screen:

1378335_pastedImage_0.png1378336_pastedImage_1.png

You will have to made zoom with the explorer, because this editor reduce the image size.

is the path well or i'm wrong?

Thanks!!

Avatar

Avatar
Validate 25
Level 10
smacdonald2008
Level 10

Likes

1,408 likes

Total Posts

12,671 posts

Correct Reply

2,278 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give back 900
Give back 600
View profile

Avatar
Validate 25
Level 10
smacdonald2008
Level 10

Likes

1,408 likes

Total Posts

12,671 posts

Correct Reply

2,278 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give back 900
Give back 600
View profile
smacdonald2008
Level 10

21-12-2017

Your Resource is not pointing to a valid AEM Resource. You must make sure that you are referencing something in the JCR to which the model binds to.

In our Multifield example here- the model binds to the products node in the dialog that represents the Multi-field node.

@Model(adaptables = Resource.class)

public class Multifield {

    // Inject the products node under the current node

    @Inject

    @Optional

    public Resource products;

    // No need of a post construct as we don't have anything to modify after the

    // model is constructed

}

Adobe Experience Manager Help | Creating a HTL Repeating Data Set 6.3 Component that uses Sling Mode...

In another example - we are using a Resource that we obtained:

resolver = resolverFactory.getServiceResourceResolver(param);

Resource resource = resolver.getResource("/content/testsling/slingmodel");

See how we have valid Resource. Now the Sling Model is bound to a valid JCR Resource and it works.

See this artilce - the only difference is front end is JSP.

Adobe Experience Manager Help | Creating Adobe Experience Manager 6.3 Sling Model Components

THis will point you in the correct direction.

Also - take note in this example - we are creating a system user, providing the correct permission and then use Sling Mapping service (which is the proper way now to get a ResourceResolver).

Hope this helps....

          

Avatar

Avatar
Validate 1
Level 2
leopor
Level 2

Likes

3 likes

Total Posts

9 posts

Correct Reply

1 solution
Top badges earned
Validate 1
Boost 3
Boost 1
Affirm 1
View profile

Avatar
Validate 1
Level 2
leopor
Level 2

Likes

3 likes

Total Posts

9 posts

Correct Reply

1 solution
Top badges earned
Validate 1
Boost 3
Boost 1
Affirm 1
View profile
leopor
Level 2

21-12-2017

Feike Visser I leave only one adaptable and

smacdonald2008 i insert the @optional annotation and I change de javax.inject dependency with this dependency:

<dependency>

               <groupId>org.apache.geronimo.specs</groupId>

               <artifactId>geronimo-atinject_1.0_spec</artifactId>

               <version>1.0</version>

               <scope>provided</scope>

           </dependency>

But nothing works, the Resource propertie is still null.

On the other hand, if I made the Inject with a constructor it´s work well.

Like this:

But i need to know why @Inject annotation is not working.

There is something else to try?

Avatar

Avatar
Give Back 50
Employee
Feike_Visser1
Employee

Likes

247 likes

Total Posts

970 posts

Correct Reply

221 solutions
Top badges earned
Give Back 50
Give Back 5
Give Back 3
Give Back 25
Give Back 10
View profile

Avatar
Give Back 50
Employee
Feike_Visser1
Employee

Likes

247 likes

Total Posts

970 posts

Correct Reply

221 solutions
Top badges earned
Give Back 50
Give Back 5
Give Back 3
Give Back 25
Give Back 10
View profile
Feike_Visser1
Employee

20-12-2017

Why do you have two adapters ({ Resource.class, SlingHttpServletRequest.class })

One is simpler and easier to debug.

Avatar

Avatar
Validate 10
Level 4
mjb54261515
Level 4

Likes

33 likes

Total Posts

132 posts

Correct Reply

32 solutions
Top badges earned
Validate 10
Validate 1
Boost 5
Boost 3
Boost 25
View profile

Avatar
Validate 10
Level 4
mjb54261515
Level 4

Likes

33 likes

Total Posts

132 posts

Correct Reply

32 solutions
Top badges earned
Validate 10
Validate 1
Boost 5
Boost 3
Boost 25
View profile
mjb54261515
Level 4

19-12-2017

Avatar

Avatar
Coach
Employee
Jörg_Hoh
Employee

Likes

1,111 likes

Total Posts

3,145 posts

Correct Reply

1,072 solutions
Top badges earned
Coach
Give back 600
Ignite 5
Ignite 3
Ignite 1
View profile

Avatar
Coach
Employee
Jörg_Hoh
Employee

Likes

1,111 likes

Total Posts

3,145 posts

Correct Reply

1,072 solutions
Top badges earned
Coach
Give back 600
Ignite 5
Ignite 3
Ignite 1
View profile
Jörg_Hoh
Employee

19-12-2017

Hi,

I am not a great Sling Models developer, but I would expect a @Self annotation:

@Self

Resource resource;

Avatar

Avatar
Validate 25
Level 10
smacdonald2008
Level 10

Likes

1,408 likes

Total Posts

12,671 posts

Correct Reply

2,278 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give back 900
Give back 600
View profile

Avatar
Validate 25
Level 10
smacdonald2008
Level 10

Likes

1,408 likes

Total Posts

12,671 posts

Correct Reply

2,278 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give back 900
Give back 600
View profile
smacdonald2008
Level 10

19-12-2017

What is that lining up with.

See this article that shows you how to inject a Multifield dialog.

Adobe Experience Manager Help | Creating a HTL Repeating Data Set 6.3 Component that uses Sling Mode...

When you use Sling Models - you need to make sure you are referencing a valid resource like a dialog node.