Return in Sightly | Community
Skip to main content
rk39193348
Level 4
February 7, 2017
Solved

Return in Sightly

  • February 7, 2017
  • 8 replies
  • 3845 views

Hi,

I have a basic example in sightly which is as below: In the logs the value of string 'value' gets printed but in the page it is not getting printed. Please let me know how to achieve this

<div data-sly-use.component="com.adobe.cq.Component">
<p> from java resource: ${component.value}</p>
</div>

In my java class Component.java

package com.adobe.cq;
import com.adobe.cq.sightly.WCMUse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.sling.api.resource.Resource;

public class Component extends WCMUse
{
private String value;
private static final Logger LOGGER = LoggerFactory.getLogger(Component.class);

@Override
  public void activate() throws Exception
   {
  Resource resource = getResource();
  value = resource.getPath();
  LOGGER.info(" value****"+value);
  }
public String getvalue()
{
 return value;
  }
}

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 Gdubz-57m2mu

EDIT: Worth noting that I think the only thing semantically wrong with your code is that your method name is "getvalue()" instead of "getValue()". Case sensitivity is very important! THAT SAID, you really SHOULD use WCMUsePojo instead of WCMUse.

If you're determined to use Java instead of what's available through JavaScript, this should work for you:

Java Class:

package com.your.pkg.name; import com.adobe.cq.sightly.WCMUsePojo; import org.apache.sling.api.resource.Resource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * Created by Greg Wells on 2/7/17. */ public class TestComponent extends WCMUsePojo { private static final Logger LOG = LoggerFactory.getLogger(TestComponent.class); private String value; @Override public void activate() throws Exception { final Resource resource = getResource(); value = resource.getPath(); LOG.info("Value: {}", value); } public String getValue() { return value; } }

HTL Component:

<!--/* Test Component @author Greg Wells */--> <div data-sly-use.testComp="com.your.pkg.name.TestComponent"> <p data-sly-test.value="${ testComp.value }"> <strong>Value from Java:</strong> ${ value } </p> </div>

Let me know if that doesn't work for you.

8 replies

Feike_Visser1
Adobe Employee
Adobe Employee
February 7, 2017

Component is already a pre-defined object, see here: https://docs.adobe.com/docs/en/htl/docs/global-objects.html

Can try this:

<div data-sly-use.c="com.adobe.cq.Component">
<p> from java resource: ${c.value}</p>
</div>

rk39193348
Level 4
February 7, 2017

Even after using 'c' it is coming blank. Please help

Gdubz-57m2mu
Gdubz-57m2muAccepted solution
Level 5
February 7, 2017

EDIT: Worth noting that I think the only thing semantically wrong with your code is that your method name is "getvalue()" instead of "getValue()". Case sensitivity is very important! THAT SAID, you really SHOULD use WCMUsePojo instead of WCMUse.

If you're determined to use Java instead of what's available through JavaScript, this should work for you:

Java Class:

package com.your.pkg.name; import com.adobe.cq.sightly.WCMUsePojo; import org.apache.sling.api.resource.Resource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * Created by Greg Wells on 2/7/17. */ public class TestComponent extends WCMUsePojo { private static final Logger LOG = LoggerFactory.getLogger(TestComponent.class); private String value; @Override public void activate() throws Exception { final Resource resource = getResource(); value = resource.getPath(); LOG.info("Value: {}", value); } public String getValue() { return value; } }

HTL Component:

<!--/* Test Component @author Greg Wells */--> <div data-sly-use.testComp="com.your.pkg.name.TestComponent"> <p data-sly-test.value="${ testComp.value }"> <strong>Value from Java:</strong> ${ value } </p> </div>

Let me know if that doesn't work for you.

Feike_Visser1
Adobe Employee
Adobe Employee
February 7, 2017

@greg, great catch!

In this case there is no real need for Java, you can have this too in your component:

${resource.path}

Gdubz-57m2mu
Level 5
February 7, 2017

Feike Visser wrote...

@greg, great catch!

In this case there is no real need for Java, you can have this too in your component:

${resource.path}

 

Thanks, and I completely agree, a lot of the common functionality is available without writing your own custom classes.

What I posted should work if he or she is really determined to use Java instead. Like if they intend to take this and extend it further, and were merely using the resource.getPath(); as a simple example to pose the question with.

Prince_Shivhare
Community Advisor
Community Advisor
February 7, 2017

Hi,

please check this helpx articles also.

https://helpx.adobe.com/experience-manager/using/creating-htl-component.html

also if you are using AEM 6.2 and maven artchetype 10 project then see this :

https://helpx.adobe.com/experience-manager/using/first_htl_WCMUsePojo.html

- Prince 

rk39193348
Level 4
February 9, 2017

Thanks for help!

Prince_Shivhare
Community Advisor
Community Advisor
February 9, 2017

Hi,

Please close this thread by marking Answer.

- Prince