The Blossom module makes the Spring Framework available for Magnolia. Blossom enables you to create editing components that display a high level of dynamic behavior. These components can be used by editors to create truly interactive web pages. For example, the Spring Framework application stack makes it easier to connect to business systems, so that they fetch the information that you want to present in your pages. As Blossom is built on the Spring Web MVC, familiarity with this framework will ensure a smooth experience working with the module.
Key Blossom functionality
- Annotation based API that builds on the Spring Web MVC. The Blossom API automatically detects annotated classes and registers them for use in Magnolia. To do this, simply add @Template to your controllers and they are ready to be used as building blocks by editors.
- Exposes controllers as templates and components, allowing you to use the controller for building a model. Useful if you need to call a web service for information that you need to present or if you need to read information from a database.
- Template based. Having templates (pages, areas and components) backed by an MVC framework has the benefit of providing natural web development business logic.
- Enable the re-use of previously created controllers.
- Allows you to create dialogs with code rather than configuration. This has many benefits:
- you can populate the dialog at runtime with options detected at runtime:
- you can obtain dialogs from the repository and place into your source control (CVS, SVN, GIT).
- Components can be executed in front of Magnolia. This means that it can choose to do a redirect and skip page rendering. This is very useful, for instance, if you have a form that on post should either present an error message or do a redirect.
Blossom module versions and releases, compatibility with Magnolia
Please see the separate Blossom module release notes subpage.
- Blossom requires Magnolia 5.6 or later.
- Needs at least Java 8.
- Supports Spring Framework 3.2 or later.
Maven is the easiest way to install the module. Add the following to your bundle:
<dependency> <groupId>info.magnolia.blossom</groupId> <artifactId>magnolia-module-blossom</artifactId> <version>3.2.1</version> </dependency>
Pre-built jars are also available for download. See Installing a module for help.
Blossom is an annotation based API that's used in combination with Spring MVC. This snippet gives an overview of how the API is used.
Running the sample project
This sample is a complete web application based on Magnolia 4.5 and requires nothing more than a Maven installation.
- Clone the git repository, switch to the right branch, build the project and then run
- When maven has downloaded the necessary artifacts and started the application point your browser to localhost:8080.
- Magnolia will now be in install mode and display a list of modules for install.
Complete the installation wizard and then log in using
superuserfor both username and password.
The sample web app uses JSP EL 2.2 bundled with Servlet API 3.0, use Tomcat 7+ or another servlet container of the same standard compliance level.
Creating your own project
We recommend using the archetypes to create your own project. For more details see this Getting started with Blossom.
What's new in Blossom 3
Major update for Magnolia 5 style dialogs, compatible with Magnolia 5.1 and later. In version 5 Magnolia has a brand new user interface and with it comes a new API for dialogs. This version of Blossom is an update for this API and also contains a few other improvements.
Fluent builder style API for dialogs
Building dialogs is now done using a fluent builder style API with a very compact syntax. The builders allow for controlling every detail of how a field should appear and behave. Where in the old API you had to resort to configure the controls using key and value pairs the new API has chainable setters for every property.
Meta-annotations for component availability
Components can be categorised using custom meta-annotations, these can then be specified when setting the available components on areas. All components having this component category will be available in the area.
Support for autowiring in RenderingModels
AbstractAutowiredRenderingModel is an abstract class for RenderingModels that need be to be autowired using Spring.
Support for Spring MVC 3.1 MVC handler methods
In Spring MVC 3.1 the implementation of annotated MVC was reimplemented adding new handler adapters and handler mappings, starting with Blossom 3.0.2 handlers detected using the new implementation are discovered.
Post processing callbacks prior to registration of templates, areas, components and dialogs
Blossom 3.0.2 introduced a new annotation
@PreRegister to be used on methods that should be called prior to registration with Magnolia.
Auto generation of components in areas
The process of auto generation gives areas the opportunity to pre-populate areas with components to save editors some work.
What's new in Blossom 2
Major update for Magnolia 4.5 and its new rendering engine, page editor and templating concepts. The term paragraph is replaced with component. Components and templates have been streamlined and are internally identical, the only real difference is that they have different id formats which in turn controls where they're applicable.
New id conventions for templates
In Magnolia 4.5 template IDs are in the format of
<moduleName>:<path>. (See the Templating documentation.) As a consequence the Blossom convention was updated. You now need to specify the ID explicitly on your controller:
For help on how to migrate your templates to the new naming convention, see Migrating content when upgrading to Blossom 2 .
With Magnolia 4.5 areas are explicit entities rather than implicit as node collections. They're declared as nested classes (public static) directly within the page templates they belong to.
This is the reference documentation for Blossom. The sample code in this section uses Spring 2.5+ annotated controllers.
To make a controller function as a template, simply add the
@Template annotation. For example:
This controller will be exposed in Magnolia, showing up as 'Section Template'. Note that the 'id' follows a convention used in Magnolia: module name, followed by a path. For templates that you want to use for pages, the path needs to start with
Controlling where a template can be used
Where a page template is available for editors to use can be customized in two ways. Either using the site module and configure in the site definition or by using the
@Available annotation. If the @Available is used it will override any setting in the site definition. Use it on a method in your controller that returns boolean. It can receive as arguments an argument of type
Content, this is the page in the website for which to determine if the template is available. It can also accept an argument of type
TemplateDefinition this is the internal definition used by Magnolia:
For more details see the documentation on the site definition.
Areas are defined using nested classes (public static class) within a template or within an other area. Areas are standard Spring Web MVC controllers annotated with
@Area. In addition Areas have their own dialog.
Controlling which components can be added to an area
To specify which components can be used in an area you can either list the ids of the components using the
@AvailableComponents annotation, list their classes using the
@AvailableComponentClasses annotation, or use meta-annotations to categorise your components. It's possible to use all three options on the same area. Meta-annotations are annotations that you create yourself and use on your components. They need to annotated with
This annotation is a component category annotation.
Any controller annotated with it will be included in an area that specifies it.
A common requirement is to have every page on a site or a whole section to have a consistent base look, sharing header, footer and banners. Area inheritance is a feature that makes an area include components from its parent pages. To use this feature use the
@Inherited annotation on an area.
By default the annotation is set to filtered mode, where it will only include components explicitly marked to be inherited. This is done using a property named inheritable. To flag it the component needs to have a checkbox added to its dialog.
Filtering mode can be turned off causing all components to be inherited.
The annotation also allows you to control how properties are inherited. These are properties on the area itself. A property set on the area in the parent page is then available when the area is rendered in the child page.
Maximum number of components in area
Sometimes its helpful to limit the number of components an editor can add into an area. On the area annotation you can specify the limit your design requires.
Reusing areas between templates through class inheritance
It's common that many templates in a project have the same set of areas by taking advantage of the class hierarchy you only need to define them once.
To reduce the effort needed by editors to create pages it can be useful to pre-populate pages by automatically adding (generating) components in areas. To use this feature add a method on your area and annotate it with
@AutoGenerator. It will receive the Node for the area content.
Components are controllers annotated with
@Template and having an id in the format
This controller will be visible as the Text and Image component. The description set using
@TemplateDescription is shown when you choose a component to add in the page editor.
Accessing content in controllers
In controllers you can have the content object passed directly to your method. In templates this is the content object for the page, in components it's the content object for the component within the page. If you declare two arguments of type
Node the first one will be the content of the page and the second will be the content of the component. Other arguments supported are
To do this, configure the
BlossomHandlerMethodArgumentResolver on your
Accessing Magnolia context objects in controllers
In addition to the content you can access a range of objects about the current rendering context by adding them as arguments to your
@RequestMapping annotated methods.
info.magnolia.rendering.template.TemplateDefinitionThe definition of the template as generated by Blossom based on the controllers annotations.
info.magnolia.rendering.template.AreaDefinitionWhen rendering an area this is the definition of the area generated by Blossom.
info.magnolia.cms.core.AggregationStateAggregation of state about an operation in the CMS
info.magnolia.context.ContextContext regarding an operation in the CMS and methods for accessing the JCR.
Contextfor web requests.
info.magnolia.cms.security.UserThe current user.
info.magnolia.cms.security.MgnlUserThe current user as a instance of the default
info.magnolia.cms.core.ChannelThe current channel being rendered for in a multi-channel setup.
info.magnolia.module.site.theme.ThemeThe current theme.
info.magnolia.module.site.SiteThe current site.
On templates, areas and components you can declare parameters that can then be accessed in the view using the
To access the example parameter in a freemarker view use:
View rendering with Blossom is performed by Spring. Blossom comes with support for using the rendering chain in Magnolia both for JSPs and Freemarker. It works by having view resolvers that return views that call into Magnolia. As a result you'll have access to all the objects that Magnolia provides to template scripts.
Blossom also provides a convenient way of returning redirects to content. By adding the
UuidRedirectViewResolver you can return the
uuid to redirect to directly from your controller. This example returns a redirect to a page:
This example returns a redirect for a resource in the documents repository:
Redirecting to the page currently being rendered is a very common case and Blossom has a shortcut syntax that does exactly this without requiring the uuid.
It's also possible to redirect to the current content being rendered when executing further down the content hierarchy.
If you prefer using Thymeleaf there is a project on GitHub by Thomas Kratz that adds support for it to Blossom. It is fully functional and is in production use.
For more details see the magnolia-thymeleaf-renderer project on GitHub.
Substituting Blossom view resolvers with custom resolvers
If you substitute or combine the Blossom view resolvers with your own implementations, be aware that the GZip filter included in Magnolia wraps the response object and gives it some non-standard semantics. It will tell you that the response is not committed even though it has been written to. It will also allow you to do redirects even though the response has been written to.
Be aware that if you write controllers that exploit the GZip filter, you are creating a dependency and you will need to have the GZip filter active. Certain framework code, such as the JSP view rendering in Spring and WebFlow will test if the response is committed to detect if redirects are possible and sometimes to choose between doing a forward or an include.
Blossom allows you to create dialogs using code. One of the benefits of this is that when the controller fetches data from a web service you can populate your dialog with options retrieved from that web service. See also Dialogs.
For example, if the controller displays a list of the best selling books in a category of books you could call the web service to find out what categories are available and use them to populate a select control. Another benefit is that you get your dialogs out of the repository and into version control. You get to version your dialogs together with your controllers.
You can easily change your dialog code in your IDE, compile and hot swap the new code into an instance of Magnolia running in debug mode. This makes developing dialogs extremely fast.
Fluent builder style API
Dialogs are described using a model of definition classes. These define the structure of the dialog, its tabs and its fields. They describe both what the dialogs and its forms should look like and how they should behave. Blossom uses a fluent builder style API to construct these definitions. The API consists of config classes that produce builders, the builders produce definitions.
The central config class is
UiConfig which is used to create tabs and all different kinds of fields provided by Magnolia.
UiConfig to all methods that create dialogs or populate dialogs. In addition it will also provide a
DialogBuilder, depending on the use case. These represent the tab or dialog that's being populated.
Note that is also possible to extend the dialog creation mechanism in Blossom to use custom config classes or replace
TabBuilder with classes of your own for adding your custom or third-party fields. The article Extending the Blossom TabBuilder on the wiki explains how this is done in detail.
Dialog factories create dialogs. Although the dialogs are automatically exposed in Magnolia, you need to configure them as beans in Spring. The easiest way to do this is to use Spring 2.5+ component scan, however manual configuration in XML will work. For example:
This registers a dialog in Magnolia with the name
front-page-dialog. It has two tabs,
Margins. Methods annotated with
@TabFactory in superclasses will be called. If there are many dialogs that look the same you can create an abstract base class for them.
The order that the dialogs appear in has been set using the
@TabOrder annotation. It sorts them based on their names. If you would rather sort them by their names you can change it with a property in your module descriptor.
Templates create their own dialog
A template always has a dialog so you don't have to create a dialog factory and link them with a name. Instead your controller will act as its own dialog factory. You can use the same annotations inside your controller that you can for dialog factories. (See above.) If you want your template to use a dialog that is created by a dialog factory or configured in the repository, it's possible to override this behavior and specify which dialog should be used:
Templates can contain dialog factories
Because templates are often designed for use with few dialogs, Blossom allows you to create these within your template class for ease of location. The concept is similar to dialog factories but here everything is done in a single method.
Dialogs and the class hierarchy
It's not uncommon to have several dialogs that are very similar. For instance you might want to have the same basic sets of tabs in a number of dialogs. Blossom makes this possible by allowing you to define tab factories in super classes. Methods are invoked in superclass-first order, methods within the same class have no defined order of invocation.
For instance a tab for meta data in an abstract base class extended by several templates.
Post create callbacks for dialogs
Tab factories will always add a tab and then call your method to have it populated. If you wish to only add the tab in certain scenarios you can instead use a post create callback. You can then determine the scenario, for instance testing if the user has a certain role or if the page is in a certain part of the website tree. They can also be useful when you need to do changes to the dialog after all tab factories have been added. Methods are invoked in superclass-first order and always after tab factories in the same class, post create methods within the same class have no defined order of invocation.
In this example a field is added if a criteria is met.
Custom dialog actions
Blossom will add the standard save and cancel actions to the dialog before it is shown to the user unless any have been configured.
This example shows how to configure a custom save action using a post create callback.
Validating dialog input
To ensure that the content is correctly structured you can add validators to the fields. In the example below the name field can not be empty and the email has to be valid.
Using DAM fields
The DAM module has a config class of its own,
DamConfig, Blossom also provides this automatically when added as an argument.
Post processing callbacks prior to registration of templates, areas, components and dialogs
Before Blossom registers templates, areas, components and dialogs with Magnolia at startup, effectively making them available for use, it will inspect its class for methods annotated with
@PreRegister and invoke those methods. This can be used to customize the definition Blossom built based on the annotations present on the class.
Pre-execution of components
Pre-execution allows you to write components that can take care of the entire page rendering. This is necessary if you want to do redirects. Another scenario is if you want to have your controller rendering a form into the page on GET requests and write an XML document or a PDF on POST requests.
Note that this feature is enabled by default from version 1.1 and disabled by default in 0.5. To enable it you need to change a flag in the repository. Navigate to
config:/server/filters/cms/blossom in AdminCentral and change the property
enabled from '
false to '
In order for pre-execution to work you need to use the
BlossomHandlerMapping. See section on this page on Handler Mapping.
Pre-execution is performed by a filter that will intercept requests and look for a request parameter that contains the UUID of the component that is to be pre-executed. The request parameter is named
_pecid. Blossom provides a taglib to make it easier to include this request parameter.
The blossom taglib has two tags,
<blossom:pecid-input /> that will output
<input type="hidden" name="_pecid" value="<component uuid>" />. This tag is also available under the alias
<blossom:pecidInput />, this is necessary for use in Freemarker templates.
It is possible to use
<blossom:pecid var="pecid" /> to set the UUID of the component as a variable, in this example named
var attribute is optional. If it's not specified the component UUID is written directly into the page.
When you have multiple forms on the same page it can be helpful to distinguish which one is supposed to handle a POST request by testing in a controller if it is being pre-executed.
At the end of this article you'll find a full example of how to use pre-execution.
Spring uses HandlerMappings to map a request to a handler (usually a controller). Blossom interrogates the HandlerMappings to find out how you have mapped your handlers. Your HandlerMapping needs to be a class inherited from
AbstractUrlHandlerMapping. This is usually the case, both Spring 2.5+ annotated controllers and
BeanNameUrlHandlerMapping inherit from
If you want support for pre-execution (see below) you will need to have
BlossomHandlerMapping delegate to your
HandlerMapping. This is necessary because this is where pre-execution is taken care of.
Note, you have to explicitly declare your HandlerMapping in XML. Although Spring will supply you with defaults even if you do not use XML, it is not possible for Blossom ot get a reference to the defaults and therefore will not take part in the rendering.
@I18nBasename you can set the name of the resource bundle you want to use. This can be used on templates, areas and components and in dialog factories. Labels and descriptions set for instance using the
@TabFactory annotations will then be localized using the specified resource bundle.
You can also leave labels and titles empty to have Magnolia resolve them automatically by generating a key based on the dialog's name, the tab's name, and the field's name. However by default Blossom will use the value given in @TabFactory both for name and label of the tab. In order to leave the label empty so automatic i18n takes effect you need to set a property in your module descriptor.
Spring i18n interoperability
Blossom provides you with a
LocaleResolver to enable Spring to pick up the locale that Magnolia has selected for the current request:
To bridge Magnolia i18n into Spring, use the following
If the content of a tab field should be translatable then it can be made i18n aware by using the i18n() method.
Sites and the template prototype
In the site definition you can define a template prototype which is a template that other templates inherit from. Things configured on the prototype such as areas are copied onto other templates.
To use this feature with Blossom you need to use a special view renderer. Blossom provides a FactoryBean for creating this renderer, SiteAwareFreemarkerTemplateViewRendererFactoryBean.
For more details see the documentation on the site definition.
Because Magnolia handles multipart requests, Blossom provides a
MultipartResolver implementation. Add this snippet to your beans XML. In the example above this would be
Virtual URI Mappings
Any bean implementing VirtualUriMapping that is configured with Spring is automatically detected and exposed in Magnolia. This means that you don not need to configure them in the repository as you normally would.
In addition, Blossom supports writing virtual URI mappings by annotating a class with
@VirtualURIMapper. Instances of this class, when configured as a bean in Spring, will be scanned for methods that can be used for URI mapping. Any method that return
MappingResult and accepts as arguments a String that is the incoming URI or a
HttpServletRequest is used.
The returned string from a Virtual URI mapping is the new URI that Magnolia will use to look up the page in the repository that is to be rendered. The returned URI can also be prefixed with "redirect:", "permanent:" or "forward:" to trigger either a temporary redirect, a permanent redirect or a forward respectively. For redirects the URI can be absolute or relative within the web application (the context path is added automatically).
For more details the reference documentation on.
Dependencies on Magnolia components
Magnolia is not based on Spring, but rather uses a container internally that manages components. It is possible to pull these components in as beans in the
ApplicationContext. Here they are made available for declaring dependencies on them and as candidates for autowiring. This is an example that exposes the
ModuleManager as a bean with id 'moduleManager':
Configuring beans in the repository
Besides for storing content for web pages, the JCR repository is also widely used for configuration. One benefit of this is that configuration is easy to change while the application is running.
To read this configuration Magnolia uses a mechanism called Node2Bean to transform the configuration into plain Java beans. The process is based on reflection. Blossom provides an extension to this mechanism which, during initialization of the bean, applies post processing on it using the Spring
This makes it possible to have dependencies in the bean that should be autowired by the
ApplicationContext. Note that the bean will also receive all the standard life cycle callbacks that a bean managed by Spring can use. These include:
- and many more.
It can also use
@PostConstructand it will be subject for having AOP aspects applied.
The Node2Bean mechanism by default calls a method with the signature
public void init() on the beans it creates if it exists. The extension provided by Blossom does not support this. Instead use
With Blossom it is possible to use this mechanism directly in Spring beans xml files. The bean is proxied and is transparently replaced if changes are made in the repository.
This is an example for a book store web site that has a discount service configured in the repository:
This example is identical to the one above except that the configuration is observed for any changes being made. When it changes the bean is reloaded making it easy to change the discounts while the system is running:
By default the proxy is created using JDK dynamic proxies, this proxies on an interface level. If you require proxying on the class you can specify
proxy-target-class="true" and the proxy will instead be a cglib class proxy. This is analogous to the way Spring AOP uses proxies.
Autowiring RenderingModel classes
In order to use this you will need to have annotation config enabled in your root web application context. Add the following to your
Interactions with <mvc:annotation-driven/>
The <mvc:annotation-driven/> tag is essentially a macro. It adds a number of beans, such as message converters, handler adapters and so on. They're all configured with reasonable defaults that suits the majority of users. That is, until you actually need something that differs from the defaults. Then you need to remove it and explicitly configure instead.
In order for Blossom to work it customizes some of the same beans that <mvc:annotation-driven/> adds. Therefor some things will break when you add <mvc:annotation-driven/>. One thing that does break is pre-execution. In order for it to work all handler adapters need to be configured within a
A common reason for adding <mvc:annotation-driven/> is bean validation of submitted forms since it adds configuration for this by default. When using Blossom you need to set up configuration for bean validation explicitly. If you've used the archetypes to create your module this was done for you. Otherwise refer to this snippet that shows how its done:
Additional DispatcherServlets and AJAX calls
Controllers used for rendering content are not accessible for requests coming in to the servlet container. Requests that come in are handled my Magnolia and mapped to content that is only later rendered by these controllers. The BlossomDispatcherServlet that manages these controllers are started in your module class and the servlet container has no knowledge of this servlet. Since these controllers are non meaningful without content there's no reason to have them directly accessible.
Controllers that should be accessible directly and are not used to render content need to be managed by a different DispatcherServlet.
There's two ways to add a DispatcherServlet in a Magnolia project. You either simply add one to web.xml or you add to your module descriptor.
Adding it to the module descriptor is the better choice because then its started after your module has started and it will become a child of the root web application context and can access all beans within it. You also won't need to update web.xml which makes module install easier and upgrades of Magnolia easier.
The module descriptor is your /src/main/resources/META-INF/magnolia/<module name>.xml file.
Here's an example of what that looks like:
Magnolia installs the servlet when the module is installed so after you've added it you need to reinstall your module. When it's installed you can see it in the configuration app at /server/filters/servlets/dispatcher.
In the example above the DispatcherServlet will get all requests with a path starting with /ajax/ and it uses a configuration file/src/main/resources/ajax-servlet.xml.
You'll want to make sure that ajax-servlet.xml has only the controllers that should be accessible from the outside and not those used for rendering content. Do this by organizing them in different packages and use component scan to find the right ones.
Here's what a simple ajax-servlet.xml would look like:
It will component scan and find the controller below which becomes accessible at /ajax/hello , note that the /ajax part is not specified in the controller.
This is a simple controller that displays a form:
The JSP for this component uses the blossom taglib to add the request parameter.
Using Spring WebFlow inside Magnolia
It is possible to include a webflow as a component and embed it into a page. The example below is a controller that exposes a booking flow:
Note that you need to turn off the
redirectOnPause behavior since it's not usable when the flow is embedded in a page. Also, you might want to customize the handling of
Blog - Tobias Mattsson