Skip to main content
Level 2
Aug 12, 2024 7:48 PM UTC
Solved

Compilation errors when trying to use sling model in my html

I have created a new component and when i try to use the sling model using data-sly-use
i get the below error , is there any reason for this error?

org.apache.sling.scripting.sightly.SightlyException: Compilation errors in org/apache/sling/scripting/sightly/apps/myproject/components/content/components/demo__component/demo__002d__component__002e__html.java:
Line 39, column 1932 : com.myproject.core.slingmodels.components.democomponent.DemoComponentModel cannot be resolved to a type

 This is my sling model

package com.myproject.core.slingmodels.components.democomponent;

import com.myproject.core.slingmodels.HeaderModel;
import lombok.Getter;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.ChildResource;


@Getter
@Model(adaptables = Resource.class,
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class DemoComponentModel {

@ChildResource
private HeaderModel Heading;

}
narendiran_ravi
narendiran_raviAccepted solution
Level 6
Aug 13, 2024 6:19 AM UTC

Can you check if the package info file has been added -  this will scan the models 

package-info.java

@Version("1.0")
package com.your.sling.models.yourpackage;
import org.osgi.annotation.versioning.Version;

 

5 replies

MukeshYadav_
Community Advisor
Community Advisor
Aug 12, 2024 8:40 PM UTC
DPrakashRaj
Community Advisor
Community Advisor
Aug 12, 2024 9:00 PM UTC

You need to check more entry in logs. It’s actually failing during compilation so it seems it has something to do with your code abd log will give you more insights on this.
What about this class ? Have you defined it properly?

  • HeaderModel
Translate
sherinregi-1
Community Advisor
Community Advisor
Aug 13, 2024 3:26 AM UTC

Hi @zendark 

Can you please check if you are adding the sling models package in pom as well

<Sling-Model-Packages>com.myproject.core.slingmodels</Sling-Model-Packages>
Translate
narendiran_ravi
narendiran_raviAccepted solution
Level 6
Aug 13, 2024 6:19 AM UTC

Can you check if the package info file has been added -  this will scan the models 

package-info.java

@Version("1.0")
package com.your.sling.models.yourpackage;
import org.osgi.annotation.versioning.Version;

 

Translate
ZendarkAuthor
Level 2
Aug 14, 2024 4:50 AM UTC

By adddind this file in my sling model folder , it works thank you

Translate