Open Source Spring Framework and Magnolia CMS: Creating Complex Java-based Websites

Spring Framework and Magnolia CMS: Creating Complex Java-based Websites

If you’re thinking about building a new Web site or re-launching an old one, it usually makes sense to consider a Content Management System (CMS). Using a CMS offers a number of advantages over hand-crafting individual pages. However, if you’re building a complex Web site, an off-the-shelf CMS often won’t satisfy all your requirements out of the box. You’ll likely need to customize it and because each CMS its own API and architecture, you’ll usually need to invest a fair amount of time learning its innards before you can start adding custom features.

From a development perspective, the ideal solution would be a CMS that can be customized without needing any special training or knowledge. If you’re a Spring developer, this ideal CMS is closer than you may think. Magnolia CMS, an open-source, enterprise-grade CMS written entirely in Java, has recently added Spring support, allowing Spring developers to easily extend the base CMS with custom functionality using their existing knowledge of the Spring Framework.

In this article, I’ll introduce you to Blossom, the Magnolia CMS module for Spring, and illustrate how you can use it to integrate Spring controllers and views into the Magnolia architecture. Let’s get started!

Assumptions and Prerequisites

Before we dive into the code, a few notes and assumptions are worth mentioning. This tutorial assumes that you have a working knowledge of Spring 3.0.x, and that you’re comfortable with deploying Web applications using Spring MVC. It also assumes that you’re familiar with Java Server Pages (JSP) and the process of creating, building and deploying JAR files to an Apache Tomcat server using Apache Maven.

The Blossom module serves as a bridge between Spring and Magnolia CMS, so this tutorial also requires you to have an understanding of the Magnolia CMS architecture, especially for templating and custom modules. To obtain this understanding, you should read the WebReference article Publish Web Content Efficiently with Magnolia CMS, and then briefly review the Magnolia CMS templating tutorial and module design guide, particularly the section on creating custom modules.

In terms of software, this tutorial assumes that you have a properly configured Spring development environment, including the following components:

The code in the following sections also assumes that you have downloaded and installed:

You’ll find detailed instructions on how to perform these tasks in the Magnolia CMS documentation, or in the WebReference beginner’s tutorial Set Up the Magnolia CMS for Web Content Creation in Just a Few Clicks. Note that the Magnolia CMS download includes a bundled version of Apache Tomcat.

With all that out of the way, let’s dive right into the code!

Introducing Blossom, the Spring and Magnolia CMS Bridge

As you may already know, Magnolia CMS uses a template-based architecture that allows users to define page types and subsections through a graphical, dialog-based interface. It then presents a composite WYSIWYG view with built-in editing controls for each subsection, allowing authors to directly modify different sections of the page and see the changes instantly. Figure 1 shows a screenshot that illustrates this in action.

Magnolia WYSIWYG Template Editor

Initial Magnolia Project Directory Structure

shell> mvn tomcat:deploy

You should see something like Figure 4 as Maven builds the project.

Apache Maven JAR Deployment

Magnolia Module Installation

Magnolia Template Auto-detection by Blossom

New Template Definition in Magnolia

New Page Creation in Magnolia

New Paragraph Template Insertion

Web Form Generated by Paragraph Template

Magnolia Form Submission Result

Figure 11: Form Submission Result

Tip: Blossom also supports the use of Spring controllers as page templates. The process is very similar to that for paragraph templates, except that you will annotate the controller with @Template instead of @Paragraph. Look here for an illustrative example.

Conclusion

Of course, this is just the tip of the iceberg. With Spring in play, all manner of interesting things become possible in Magnolia CMS. Still, the examples in this tutorial illustrate how Magnolia’s Blossom module makes it easy to integrate advanced features of the Spring Framework with Magnolia’s existing templating architecture. This allows developers a great deal of creativity and flexibility when building complex Web sites with custom workflows and non-standard use cases. The use of Spring annotations further simplifies the process, making it possible to reuse existing Spring controllers as Magnolia templates and thereby reduce the overall time, cost and risk of development. Try it out yourself, and see how easy it is!

Code Download

  • Magnolia-Spring_src
  • For Further Reading

  • Set Up the Magnolia CMS for Web Content Creation in Just a Few Clicks (from WebReference)
  • Publish Web Content Efficiently with Magnolia CMS (from WebReference)
  • The Magnolia CMS templating tutorial (from documentation.magnolia-cms.com)
  • About the Author

    Vikram Vaswani is a consultant specializing in open-source tools and technologies. He has more than 11 years of experience deploying open-source tools in corporate intranets, high-traffic Web sites, and mission-critical applications. He is a frequent contributor of articles and tutorials to the open source community, and the author of six books on PHP, MySQL and XML technologies.

    Latest Posts

    Related Stories