


Solved! Go to Solution.
Views
Replies
Total Likes
@Model(adaptables=Resource.class)
public class MyModel {
@Inject
private String propertyName;
}
In this case, a property named "propertyName" will be looked up from the Resource (after first adapting it to a ValueMap
) and it is injected.
Gets a property from a ValueMap by name; If @Via is not set, it will automatically take resource if the adaptable is the SlingHttpServletRequest. If name is not set the name is derived from the method/field name.
@Model(adaptables = SlingHttpServletRequest.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL) public class ExampleComponent { // @Inject @Source("valuemap") @Named("jcr:title") @ValueMapValue(name = "jcr:title") private String titleText; // @Inject @Source("valuemap") @ValueMapValue private String titleDescription;
It means if we use @@ValueMapValue it will be done automatically an we have to write less code.
Refer here-https://sling.apache.org/documentation/bundles/models.html
Please refer below example to get property value when Model is created using SlingHttpServletRequest as an Adaptable and we have to add @Via to get property from current resource.
@Model(adaptables=SlingHttpServletRequest.class) public interface MyModel { //will return request.getResource().getValueMap().get("propertyName", String.class) @Inject @Via("resource") String getPropertyName(); }
whereas if you use @ValueMapValue it will be done automatically and you have to write less code. Refer this snippet from Sling Document :
@ValueMapValue : Injects a ValueMap value. If via is not set, it will automatically take resource if the adaptable is the SlingHttpServletRequest. If name is not set the name is derived from the method/field name.
Link : https://sling.apache.org/documentation/bundles/models.html
@Model(adaptables=Resource.class)
public class MyModel {
@Inject
private String propertyName;
}
In this case, a property named "propertyName" will be looked up from the Resource (after first adapting it to a ValueMap
) and it is injected.
Gets a property from a ValueMap by name; If @Via is not set, it will automatically take resource if the adaptable is the SlingHttpServletRequest. If name is not set the name is derived from the method/field name.
@Model(adaptables = SlingHttpServletRequest.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL) public class ExampleComponent { // @Inject @Source("valuemap") @Named("jcr:title") @ValueMapValue(name = "jcr:title") private String titleText; // @Inject @Source("valuemap") @ValueMapValue private String titleDescription;
It means if we use @@ValueMapValue it will be done automatically an we have to write less code.
Refer here-https://sling.apache.org/documentation/bundles/models.html
@Inject: This annotation is used to inject a property, resource, request, OSGi service.
This is a generic annotation
@ValueMapValue: Injects a ValueMap value. If via is not set, it will automatically take resource if the adaptable is the SlingHttpServletRequest. If name is not set the name is derived from the method/field name.