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"
>
</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{
}
GetFragmentConcessionary (interface)
public interface GetFragmentConcessionary extends Component {
public String getTeste();
}
Solved! Go to Solution.
Views
Replies
Total Likes
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.
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.
Can you share some code on how to do this ? I'm new with Java Sling Model in AEM
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.
Views
Likes
Replies