ExamGecko
Question list
Search
Search

Related questions











Question 29 - AD0-E134 discussion

Report
Export

A snippet throws an exception at runtime:

@Model(adaptables = {Resource.class}) public class MyCustomModel {

(SSIingObject

private Resource resource;

What should the developer add to fix it?

A.
defaultlnjectionStrategy = DefaultlnjectionStrategy property to @Model Class annotation
Answers
A.
defaultlnjectionStrategy = DefaultlnjectionStrategy property to @Model Class annotation
B.
(©Optional annotation to page field
Answers
B.
(©Optional annotation to page field
C.
throws Exception at the end of the init method declaration
Answers
C.
throws Exception at the end of the init method declaration
D.
SlingHttpServletRequest.class to adaptables property of ©Model Class annotation
Answers
D.
SlingHttpServletRequest.class to adaptables property of ©Model Class annotation
Suggested answer: A

Explanation:

The developer should add the defaultInjectionStrategy = DefaultInjectionStrategy property to the @Model Class annotation to fix the snippet. The defaultInjectionStrategy property defines how the Sling Model handles missing or null values for the injected fields. By default, the Sling Model uses the REQUIRED injection strategy, which means that all fields must have a non-null value or else an exception is thrown. By setting the defaultInjectionStrategy property to OPTIONAL, the Sling Model allows null values for the injected fields and does not throw an exception. This way, if the page field is null because the resource is not a page, the Sling Model can still work without errors. Reference:

https://sling.apache.org/documentation/bundles/models.html

https://sling.apache.org/documentation/bundles/models.html#optional-injection

asked 02/10/2024
Misael Mosco Jiménez
40 questions
User
Your answer:
0 comments
Sorted by

Leave a comment first