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

Giancarlo_at_KD

17-12-2019

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 @Optional annotation sets blank.

 

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

Accepted Solutions (1)

Accepted Solutions (1)

ArpitVarshney

MVP

17-12-2019

Hi @Giancarlo_at_KD 

 

Please update your annotation to below and try:

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

 

Regards,

Arpit 

 

 

Answers (0)