HomeCore Learning Journey › Lab 5 — Model the benefits table

Lab 5 — Model the benefits table

Purpose: Back the plan-card with a Sling Model: typed fields, derived values, child-resource benefits, and a JSON export.

Who this page is for

AudienceWhy it matters to you
All engineersPairs with Session 5

Goal

Replace Lab 4's direct properties.* access with a proper model, add derived logic, and export JSON.

Steps

  1. In the core module, create PlanCardModel as in Session 5 (adaptable: request; OPTIONAL injection),

plus a nested/companion Benefit model class (label, covered) adapted from child resources of benefits.

  1. Add derived getters: getFormattedPremium() and getCoveredCount() (count of benefits with covered=true).
  2. Switch plancard.html to data-sly-use.model="com.academy.phi.models.PlanCardModel" and render only getters —

no properties.* left in the script.

  1. Deploy; confirm the page renders identically, plus show "X of Y benefits covered" from the derived getter.
  2. Add @Exporter(name = "jackson", extensions = "json") and request

<component-path>.model.json — your model is now a headless API.

  1. Unit test: with wcm.io AEM Mocks, load a JSON fixture of the component content and assert

getCoveredCount() and empty-content behaviour.

Checkpoint

VerifyExpectation
Empty component instanceRenders placeholder, no NPE — OPTIONAL + defaults doing their job
error.log during renderClean — injection failures would log here
.model.json outputTitle, premium, benefits array present; internal fields absent
Unit testGreen without a running AEM — models are just classes

Stretch

Inject an @OSGiService (write a trivial PremiumFormatter OSGi service) to experience service injection.

Quick navigation