How to implements a class in my current class and access his method ? | Community
Skip to main content
Level 3
August 23, 2023
Solved

How to implements a class in my current class and access his method ?

  • August 23, 2023
  • 1 reply
  • 809 views

I want to return the value of variable teste2 inside my current class but I can't return correctly this value what I need to do? 

Here below there's the code to test 

 

HTL 

<sly
data-sly-use.cameraModel="com.mysite.core.models.CameraCardsList"
data-sly-test="${cameraModel.listItems.size>0}"
data-sly-use.fragmentTemplate="mysite/components/block_cameraGroup/templates.html"
>

    <h4 class="cmp-block_cameraGroup--title"access model getFragmentConcessionary = <sly data-sly-  use.model="com.ccrcanaisdigitais.core.models.GetFragmentConcessionary">${model.getTeste}</sly>
access model Cameras = ${cameraModel.getTeste}</h4>


</sly> 

Model CameraCardsList (class)

public class CameraCardsListImpl extends AbstractComponentImpl implements CameraCardsList, GetFragmentConcessionary {
private GetFragmentConcessionary delegate = new GetFragmentConcessionaryImpl();
@9944223
public String getTeste() {
return delegate.getTeste();
}
}

model GetFragmentConcessionary (class)
public class GetFragmentConcessionaryImpl implements GetFragmentConcessionary{
     public String teste2 = "outConstructor";
    @PostConstruct protected void init() throws RepositoryException {
       teste2 = "inConstructor";
      }
    @9944223 public String getTeste() {
       return teste2;
     }
}

 

CameraCardsList (interface)

public interface CameraCardsList extends Component{

    public String getTeste();

}

GetFragmentConcessionary (interface)

public interface GetFragmentConcessionary extends Component {
    public String getTeste();
}

 

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 Sady_Rifat

Hello @nathanvieira ,
Check this example,
NavigationMenu.java

@Model(adaptables = Resource.class) public class NavigationMenu { @Inject @Optional private String linkText; @Inject @Optional private String linkURL; @PostConstruct protected void init() { // Your Code } }

MyModel.java

@Model(adaptables = Resource.class) public class MyModel { @Inject @Optional private List<Resource> menues; private List<NavigationMenu> menusList = new ArrayList<>(); @PostConstruct protected void init() { //Create your valid resource, in this example resource my resource is from multifield child item. for (Resource resource : menues) { NavigationMenu menu = resource.adaptTo(NavigationMenu.class); menusList.add(menu); } } }

Hope this example helps you to understand how you can create a model object by adapting.

 

1 reply

Sady_Rifat
Community Advisor
Community Advisor
August 24, 2023

Hello @nathanvieira ,

The structure you followed will not work. When you write this line.

private GetFragmentConcessionary delegate = new GetFragmentConcessionaryImpl();

it will initialize the Model class with a new object. So the values will not be there. It will return null every time.
Solution,

One thing you can do, since you need two model classes I assumed your component structure is also like this. For that, you need to adaptTo the GetFragmentConcessionary instead of creating an object by new. This adaptTo can be by request or resource based on your Model implementation.

Level 3
August 24, 2023

Can you share some code on how to do this ? I'm new with Java Sling Model in AEM

Sady_Rifat
Community Advisor
Sady_RifatCommunity AdvisorAccepted solution
Community Advisor
August 28, 2023

Hello @nathanvieira ,
Check this example,
NavigationMenu.java

@Model(adaptables = Resource.class) public class NavigationMenu { @Inject @Optional private String linkText; @Inject @Optional private String linkURL; @PostConstruct protected void init() { // Your Code } }

MyModel.java

@Model(adaptables = Resource.class) public class MyModel { @Inject @Optional private List<Resource> menues; private List<NavigationMenu> menusList = new ArrayList<>(); @PostConstruct protected void init() { //Create your valid resource, in this example resource my resource is from multifield child item. for (Resource resource : menues) { NavigationMenu menu = resource.adaptTo(NavigationMenu.class); menusList.add(menu); } } }

Hope this example helps you to understand how you can create a model object by adapting.