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
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

Sling Models - @Named annotation not working in AEM 6.4

srikanthp689160
Level 4
Level 4

Hi,

Until AEM 6.2/6.3 @Named annotation was working as expected but in AEM 6.4 it does not seem to be working i.e. for example if property name is desc in jcr repository but in respective sling model if below is the code then property values are not injected.

@ValueMapValue

@Named("desc")

private String shortDescription;

Am i missing something here? Is there any alternative to @Named annotation in AEM 6.4 and versions above?

Any suggestions on this is highly appreciated.

Thanks

Srikanth Pogula.

1 Accepted Solution
Shashi_Mulugu
Correct answer by
Community Advisor
Community Advisor

Did you tried using " @ValueMapValue(injectionStrategy = InjectionStrategy.OPTIONAL,name = "pId")

View solution in original post

9 Replies
Arun_Patidar
Community Advisor
Community Advisor

Hi,

How are you adapting the model, using Resource or Request?

I am using @Name attribute along with @Inject and adapting as Resource type and it is working.

Can you share a sample code so that we can try to reproduce the issue?

srikanthp689160
Level 4
Level 4

Hi Arun,

We are adapting the model to Resource. Here is the sample code.

We are on AEM 6.4 SP4

@Model(adaptables = Resource.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class ProductCategoryModel {

/** The product category title. */
@ValueMapValue

@Named("pId")
private String productid;

public String getProductid() {

  return productid;

}

}

@Inject does not inject value hence we are using @ValueMapValue which works.

Thanks,

Srikanth Pogula

Arun_Patidar
Community Advisor
Community Advisor

Hi,

Try with inject

@Inject

@Named("pId")
private String productid;

srikanthp689160
Level 4
Level 4

Hi Arun,

If we use @Inject annotation, respective property values are not injected, they are null. We are using below maven dependency for sling models in our project.

<dependency>

    <groupId>org.apache.sling</groupId>

    <artifactId>org.apache.sling.models.api</artifactId>

    <version>1.0.0</version>

    <scope>provided</scope>

</dependency>

Since @Inject was not working, we replaced it with @ValueMapValue which works but along with that @Named does not seem to work.

Thanks

Srikanth Pogula

Jörg_Hoh
Employee
Employee

Can you please describe your usecase plus the bundle versions you use, and post it to the sling user mailing list [1]?  There should be the right people which can have a good insight into the details.

Jörg

[1] Apache Sling :: Project Information

Shashi_Mulugu
Correct answer by
Community Advisor
Community Advisor

Did you tried using " @ValueMapValue(injectionStrategy = InjectionStrategy.OPTIONAL,name = "pId")

View solution in original post

BrijeshYadav
Level 5
Level 5

Use this for AEM 6.4

<dependency>

        <groupId>com.adobe.aem</groupId>

        <artifactId>uber-jar</artifactId>

        <version>6.4.1</version>

        <scope>provided</scope>

        <classifier>apis</classifier>

</dependency>

<dependency>

        <groupId>javax.inject</groupId>

        <artifactId>javax.inject</artifactId>

        <version>1</version>

        <scope>provided</scope>

</dependency>

srikanthp689160
Level 4
Level 4

Thanks everyone for the inputs.

Using @ValueMapValue(injectionStrategy = InjectionStrategy.OPTIONAL,name = "pId") resolved the issue.

Thanks

Srikanth Pogula.

srikanthp689160
Level 4
Level 4

Hi,

If you are asking about Apache Sling Models APIorg.apache.sling.models.api) version, it is 1.3.8

Usecase is we are using a sling model for a component where for example if property name is pId but would prefer variable name in respective sling model as productId, we were using below code in AEM 6.2 which was working fine

@Inject

@Named("pId")
private String productid;

We recently upgraded to AEM 6.4 where the above piece of code stopped working.

We have replaced @Inject with @ValueMapValue but could not achieve the functionality with @Named.

Solution proposed by shashi1223 i.e. @ValueMapValue(injectionStrategy = InjectionStrategy.OPTIONAL,name = "pId") worked.

Thanks

Srikanth Pogula.