Which approach is better? Maybe one of these approaches is outdated?
Actually, ComponentController and ComponentRenderer both serve different purposes.
ComponentController is where you can play around incoming requests, manipulate data or some business logic before feeding it to view to render the final output. Here you need to write view in JSP file. Refer CMSPageUrlResolvingController
, SimpleResponsiveBannerComponentController
, DynamicBannerComponentController
etc.
ComponentRenderer is for View. Which means you are going to write view content on the page context directly inside the renderer. Here you do not require to have JSP to render the view. Refer CMSParagraphComponentRenderer
, CMSLinkComponentRenderer
, ImageMapComponentRenderer
etc
If you just want to populate your component attributes to Model, then you don't require to define the custom controller or renderer, OOTB GenericCMSAddOnComponentController will take care of it.
Please note, If there is a respective custom componentRenderer already defined, then componentContoller won't be invoked, having said that custom componentRenderer and custom componentContoller can't work together.
How custom component controller or renderer gets invoked internally?
DefaultCMSComponentRendererRegistry.java
@Override
public void renderComponent(final PageContext pageContext, final C component)
{
final String typeCode = getTypeService().getComposedTypeForClass(component.getClass()).getCode();
final Map<String, CMSComponentRenderer> renderersMap = getRenderers();
try
{
// To check for any custom renderer present
if (renderersMap != null && renderersMap.containsKey(typeCode))
{
renderersMap.get(typeCode).renderComponent(pageContext, component);
}
else
{
// If no custom renderer, call GenericViewCMSComponentRenderer
getDefaultCmsComponentRenderer().renderComponent(pageContext, component);
}
}
catch (final Exception e)
{
handleException(e, component);
}
}
GenericViewCMSComponentRenderer.java
To invokes the custom component controller or default controller
@Override
public void renderComponent(final PageContext pageContext, final AbstractCMSComponentModel component)
throws ServletException, IOException
{
// ...
// ...
final String typeCode = component.getTypeCode();
String controllerName = typeCode + "Controller";
if (!getBeanFactory().containsBean(controllerName))
{
if (LOG.isDebugEnabled())
{
LOG.debug("No controller defined for ContentElement [" + typeCode + "]. Using default Controller");
}
controllerName = DEFAULT_CONTROLLER;
}
final String includePath = "/view/" + controllerName;
if (LOG.isDebugEnabled())
{
LOG.debug("Rendering CMS Component type [" + typeCode + "] uid [" + component.getUid() + "], include path ["
+ includePath + "]");
}
renderView(pageContext, component, includePath);
// ...
// ...
}