Cant we use ValueMapValue with @Model(adaptables=Resource.class) ? | Community
Skip to main content
JakeCham
Level 6
December 28, 2023
Solved

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

  • December 28, 2023
  • 1 reply
  • 1311 views

Hi Team

 

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

Should it be with 

@Model(adaptables = SlingHttpServletRequest.class

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by aanchal-sikka

@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/

1 reply

aanchal-sikka
Community Advisor
aanchal-sikkaCommunity AdvisorAccepted solution
Community Advisor
December 29, 2023

@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
JakeCham
JakeChamAuthor
Level 6
December 30, 2023

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>
}

aanchal-sikka
Community Advisor
Community Advisor
December 31, 2023

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