How to implements a class in my current class and access his method ? | Adobe Higher Education
Skip to main content
Level 3
August 23, 2023
해결됨

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

  • August 23, 2023
  • 1 답변
  • 809 조회

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();
}

 

이 주제는 답변이 닫혔습니다.
최고의 답변: 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 답변

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.

NathanVieira작성자
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
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.