HTL use-api parameters are always null in Sling Model Class. | Community
Skip to main content
Giancarlo_at_KD
Level 2
December 18, 2019
Solved

HTL use-api parameters are always null in Sling Model Class.

  • December 18, 2019
  • 1 reply
  • 1679 views

I am trying a HTL/Sling Model example with HTL Parameters and Sling Model Injection. However, the parameters passed via HTL are always null. Does anyone have a solution?

 

Java --> Inject example: com.testing.models.SimpleHelloWorld

@Model(adaptables = Resource.class)
public class SimpleHelloWorldModel {

@SlingObject
private Resource currentResource;
@SlingObject
private ResourceResolver resourceResolver;

@Inject
@Optional
protected String testProp;

private String localProp = "test value";

@PostConstruct
protected void init() {
this.localProp = testProp;
}

public String getTestProp() {return testProp;}

public String getLocalProp() {return this.localProp;}

 The idea is to receive a value from HTL (testProp), see if the init() method gets correctly invoked due to the @PostConstruct annotation and set a local property.

Here is the very simple HTL snippet:

<sly data-sly-use.hw="${ 'com.testing.models.SimpleHelloWorldModel' @ testProp='from HTL' }" />
<div class="add-message">
<h3> class="we-ProductsGrid-item-title h4">Testing HTL/Sling Model</h3>
<h3>Testprop: ${hw.testProp}</h3>
<h3>LocalProp: ${hw.localProp}</h3>
</div>

The init() method gets invoked.
However, every property I pass via HTL is null. The @7392697 annotation sets blank.

 

Does anyone know if HTL has been updated to pass parameters to a Sling Model Class that can be read via @586265? 

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 ArpitVarshney

Hi @giancarlo_at_kd 

 

Please update your annotation to below and try:

@Model(adaptables = {Resource.class, SlingHttpServletRequest.class})

 

Regards,

Arpit 

 

 

1 reply

ArpitVarshney
Community Advisor
ArpitVarshneyCommunity AdvisorAccepted solution
Community Advisor
December 18, 2019

Hi @giancarlo_at_kd 

 

Please update your annotation to below and try:

@Model(adaptables = {Resource.class, SlingHttpServletRequest.class})

 

Regards,

Arpit 

 

 

Giancarlo_at_KD
Level 2
December 18, 2019
Thanks, Arpit, this did the trick!