Hi Team
Cant we use ValueMapValue with @Model(adaptables = Resource.class) ?
Should it be with
@Model(adaptables = SlingHttpServletRequest.class
Solved! Go to Solution.
Views
Replies
Total Likes
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/
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/
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>
}
Hello @JakeCham
Thanks for sharing the relevant code snippets. Here are the improvements, please try once
@ValueMapValue
@Named("title")
private String authoredVal;
@ValueMapValue
private String messageText;
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 ?
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/
Thanks for the article. I have got to know about multiple scenarios.
Views
Replies
Total Likes