Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

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

Avatar

Level 2

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 @Deleted Account 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

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @Giancarlo_at_KD 

 

Please update your annotation to below and try:

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

 

Regards,

Arpit 

 

 

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

Hi @Giancarlo_at_KD 

 

Please update your annotation to below and try:

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

 

Regards,

Arpit