Expand my Community achievements bar.

Why isn't my value being @Injected?

Avatar

Level 3

I have this property called "executionID" set on my node.

 

In Helloworld.html I have this line:

<pre data-sly-use.hello="test.core.models.TestModel">

I then read "hello.value"

 

My TestModel looks like this:

 

package test.core.models; //imports... @Model(adaptables = Resource.class) public class TestModel { @Inject  @Default(values="0000000000000") private String executionID; public String value; private final Logger logger = LoggerFactory.getLogger(getClass()); @PostConstruct protected void init(){ try{ logger.info("executionID is " + executionID); // more code.... 

 

At this point the executionID is always the default, "0000000000000".

How can I inject my value into my class?

 

If I remove the @Default annotation and it's value, I get this:
 

 

org.apache.sling.api.scripting.ScriptEvaluationException: org.apache.sling.scripting.sightly.SightlyException: Identifier test.core.models.TestModel cannot be correctly instantiated by the Use API at org.apache.sling.scripting.core.impl.DefaultSlingScript.call(DefaultSlingScript.java:416) ... Caused by: org.apache.sling.scripting.sightly.SightlyException: Identifier test.core.models.TestModel cannot be correctly instantiated by the Use API at org.apache.sling.scripting.sightly.impl.engine.extension.use.UseRuntimeExtension.call(UseRuntimeExtension.java:89) at org.apache.sling.scripting.sightly.impl.engine.runtime.RenderContextImpl.call(RenderContextImpl.java:66) at org.apache.sling.scripting.sightly.apps.test.components.content.helloworld.SightlyJava_helloworld.render(SightlyJava_helloworld.java:53) at org.apache.sling.scripting.sightly.impl.engine.runtime.RenderUnit.render(RenderUnit.java:54) at org.apache.sling.scripting.sightly.impl.engine.SightlyScriptEngine.evaluateScript(SightlyScriptEngine.java:92) at org.apache.sling.scripting.sightly.impl.engine.SightlyScriptEngine.eval(SightlyScriptEngine.java:78) at org.apache.sling.scripting.core.impl.DefaultSlingScript.call(DefaultSlingScript.java:388) ... 204 more Caused by: org.apache.sling.models.factory.MissingElementsException: Could not inject all required fields into class test.core.models.TestModel Could not inject private java.lang.String test.core.models.TestModel.executionID caused by No injector returned a non-null value! at org.apache.sling.models.impl.ModelAdapterFactory.createObject(ModelAdapterFactory.java:534) at org.apache.sling.models.impl.ModelAdapterFactory.internalCreateModel(ModelAdapterFactory.java:306) at org.apache.sling.models.impl.ModelAdapterFactory.createModel(ModelAdapterFactory.java:200) at org.apache.sling.scripting.sightly.models.impl.SlingModelsUseProvider.provide(SlingModelsUseProvider.java:132) at org.apache.sling.scripting.sightly.impl.engine.extension.use.UseRuntimeExtension.call(UseRuntimeExtension.java:84) ... 210 more
9 Replies

Avatar

Level 10

There is a working package too. Compare that with your code.

Avatar

Level 3

I reviewed that article and the working package, but it's not apparent where the problem is.

Avatar

Level 7

You can refer this link to Use sling model with Sightly

Create a method in sling model.

public String getExecutionID(){

return executionID;

}

And in your html file use it as hello.executionID

Let me know if you still face any issue.

regards,

Ankur Ahlawat

Avatar

Level 3

I'm not trying to use the variable `executionID` in my html, I'm trying to set the value in java.

 

I'd like to include the property for `executionID` on a node, then @Inject it in a java class. The current problem is that the value always appears as the default. See my code above.

Avatar

Level 10

I will be looking at your sample today - its on my TODO list.

Avatar

Level 10

Here is another AEM community article that uses HTL and Sling Models -- see: 

https://helpx.adobe.com/experience-manager/using/domparser.html

This injects values too and displays the data in the front end. 

Avatar

Employee

You need to add @Optional in your case I think, perhaps the first time the property executionID is not present.