hi folks,
Can you annotate a class extended from WcmUsePojo with @component
and call a service using @reference?
I.e. using felix scr annotations.
I tried it and got errors messages about ScriptEvaluationExceptions.
The service works fine with bundlecontext.
thanks
Fiona
Solved! Go to Solution.
Views
Replies
Total Likes
@fionas76543059 Usage of annotation where extending WCMUsePojo class not a right approach. Please refer below code snippet you may understand using WCMUsePojo
Java
import javax.jcr.Node;
import org.demo.core.models.TextComponentModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.adobe.cq.sightly.WCMUsePojo;
public class TextComponent extends WCMUsePojo {
private TextComponentModel model;
@Override
public void activate() throws Exception {
try {
log.info("Text Component 2.0 backend logic starts");
/**
* Initializing the model object to set the values
*/
model = new TextComponentModel();
/**
* Getting the current node from the resource object which is available in Use
* API
*/
Node node = getResource().adaptTo(Node.class);
/**
* Check if the node has title property
*/
if(node.hasProperty("title")) {
/**
* Reading the title property from the string
*/
String title = node.getProperty("title").getString();
/**
* Setting the value entered by the user in the model object
*/
model.setTitle(title);
}
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
/**
* This method is wrapper to return the model object
*
* @return {@link TextComponentModel}
*/
public TextComponent2Model getModel() {
return model;
}
}
HTL
Text Component 2.0 - Showcases Java Use API <div data-sly-use.text="org.demo.core.cqcomponents.TextComponent"> <h1>Title: ${text.model.title}</h1> </div>
Hi @fionas76543059 ,
What is the use of extending a class from WCMUsePojo?, the purpose of extending WCMUsePojo -
Your class treated as a regular POJOs that extend WCMUsePojo (Javadoc) implement the Use interface and are initialized with the scripting bindings, providing convenience methods for accessing commonly used objects (request, resource, properties, page etc).
IMHO, there is no sense to extend WCMUsePojo in your component/service component.
Hope that helps!
Regards,
Santosh
hi Santosh,
Are you saying, if you want to use annotations, don't use classes extended from wcmUsePojo :
Convert the Class to be a @Model class instead. ?
thanks
Fiona
@fionas76543059 Usage of annotation where extending WCMUsePojo class not a right approach. Please refer below code snippet you may understand using WCMUsePojo
Java
import javax.jcr.Node;
import org.demo.core.models.TextComponentModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.adobe.cq.sightly.WCMUsePojo;
public class TextComponent extends WCMUsePojo {
private TextComponentModel model;
@Override
public void activate() throws Exception {
try {
log.info("Text Component 2.0 backend logic starts");
/**
* Initializing the model object to set the values
*/
model = new TextComponentModel();
/**
* Getting the current node from the resource object which is available in Use
* API
*/
Node node = getResource().adaptTo(Node.class);
/**
* Check if the node has title property
*/
if(node.hasProperty("title")) {
/**
* Reading the title property from the string
*/
String title = node.getProperty("title").getString();
/**
* Setting the value entered by the user in the model object
*/
model.setTitle(title);
}
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
/**
* This method is wrapper to return the model object
*
* @return {@link TextComponentModel}
*/
public TextComponent2Model getModel() {
return model;
}
}
HTL
Text Component 2.0 - Showcases Java Use API <div data-sly-use.text="org.demo.core.cqcomponents.TextComponent"> <h1>Title: ${text.model.title}</h1> </div>
thanks Santosh,
Is TextComponentModel annotated with @Model so that I can use annotations in it?
thanks
Fiona
Try this
MyConfigService service = getSlingScriptHelper().getService(MyConfigService.class);
thanks Brian, I'll try that.
Views
Likes
Replies