Expand my Community achievements bar.

SOLVED

Cant we use ValueMapValue with @Model(adaptables=Resource.class) ?

Avatar

Level 7

Hi Team

 

Cant we use ValueMapValue with @Model(adaptables = Resource.class) ?

Should it be with 

@Model(adaptables = SlingHttpServletRequest.class

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@JakeCham 

 

Yes we can use ValueMapValue with both Resource and SlingHttpServletRequest. Infact, its the recommended way to access properties of a component

 

https://techrevel.blog/2017/03/18/sling-model-annotations/


Aanchal Sikka

View solution in original post

6 Replies

Avatar

Correct answer by
Community Advisor

@JakeCham 

 

Yes we can use ValueMapValue with both Resource and SlingHttpServletRequest. Infact, its the recommended way to access properties of a component

 

https://techrevel.blog/2017/03/18/sling-model-annotations/


Aanchal Sikka

Avatar

Level 7

Hi @aanchal-sikka I have doubt in below can u help to solve please ?

A custom component has one dialog field:

<Title

fieldLable=Title

name = ./title

sling:resourceType=granite/ui/components/coral/foundation/textfield

 

The developer needs to implement a Sling Model to perform a business logic on the authored value.
The developer writes the following HTL snippet.

<sly data-sly-use.display="com.adobe.aem.core.model.HelloWorldModelImpl">

<h1>${display.messageText}</h1>

</sly>

 

Do you observe any wrong in below code ?

@Model(Adaptable = Resource.class, defaultInjectionStrategy=DefaultInjectionStrategy.OPTIONAL))
public class HelloWorldModelImpl{
@ValueMapValue
@Named("title")
private String authoredVal;
private String messageText;

<relevant code>
}

Avatar

Community Advisor

Hello @JakeCham 

 

Thanks for sharing the relevant code snippets. Here are the improvements, please try once 

  • @ValueMapValue needs to be added to each field in Sling Model. A model can have various types of fields, example OSGi Service and normal variables. Thus, every field which is a component property needs to be annotates individually

 

@ValueMapValue
@Named("title")
private String authoredVal;

@ValueMapValue
private String messageText;

 

  • Second, in the dialog you only have the "Title field". The messageText isn't part of it. Unless it is added to component in any other way (like manually), it would be null/empty.

Aanchal Sikka

Avatar

Level 7

Hi @aanchal-sikka 

 

For inject it is not like that right,@inject doesn't need to be added to each field in Sling Model.

@inject

private String title;

private String messageText;

 

@inject

@Via(resource)

private String title;

private String messageText;

 

Above cases would be fine ?

 

Avatar

Community Advisor

@JakeCham 

 

I haven't tried @Inject for multiple fields with one annotation. Please do try and let us know your findings.

On the second note, avoid using @Inject. It’s advisable to prefer injector-specific annotations like @ValueMapValue or @ChildResource over @Inject. This is because value injection occurs at runtime, and using @Inject can introduce ambiguity, requiring the framework to make educated guesses. When using @ValueMapValue, especially in the context of retrieving properties from the ValueMap, the process is automatic, resulting in less code to write and increased clarity.

 

Details here: https://techrevelhub.wordpress.com/2023/10/11/sling-model-annotations-best-practices/


Aanchal Sikka

Thanks for the article. I have got to know about multiple scenarios.