Expand my Community achievements bar.

SOLVED

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

Avatar

Level 3

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

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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.

 

View solution in original post

3 Replies

Avatar

Community Advisor

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.

Avatar

Level 3

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

Avatar

Correct answer by
Community Advisor

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.