Share data between components

Hello, I have doubts using components, specifically sharing objects, strings, etc between components.
Actually I use the session to store the required data from one component and then I extract it in the another component.

Is there another option to use?

Thanks in advance.

1 Like

All executions on the HstComponent instances, hierarchically oraganized in a page, share a single HstRequestContext object, which can be accessed through either RequestContextProvider.get() or HstRequest#getRequestContext().
And, the execution order on HstCompnoent instances in the page is “parent to child” when invoking #doBeforeRender() methods, and the reversed, “child to parent”, when rendering each component using hst templates (e.g, FTL). [1,2]

Therefore, you could try any of the following:

  • You can simply set an attribute through HstRequestContext.setAttribute(...) in a parent component’s #doBeforeRender() and get the attribute in descendants.
  • Or you can. add a utility class with a static method which uses RequestContextProvider.get() to get the current thread’s HstRequestContext to return the sharable attribute if existing, or create one if not set yet.





There is one more option:

  • If one of the components in the page sets some sharable attributes in HstRequestContext in #prepareBeforeRender(...) method, then all components may see the sharable attributes in #doBeforeRender(...) method or thereafter. See [1] for details; #prepareBeforeRender() methods, if existing, are invoked on all components before invoking #doBeforeRender(...).