Most Read This Week
Flex Top Links You Must Click On !
Streamlining Application Development Using Struts in ColdFusion
ColdFusion MX supports Java objects and servlets in a big way
By: Steve Belt
Jan. 8, 2006 03:30 PM
Although earlier versions of ColdFusion supported Java objects and servlets to some degree, ColdFusion MX does so in a big way. This support opens up commercially available libraries to ColdFusion developers that were previously only available to J2EE shops. Moreover, you can now draw from an immense library of free code from within the Java open-source community. This availability greatly enhances the features of your own applications and speeds your ColdFusion-based solutions to market. Use these objects to provide back-end processing or user-interface functionality, or simplify coding CFM pages by supplementing ColdFusion functions and tags with Java TagLibraries.
Of particular note is how ColdFusion MX hosts and runs the Apache Jakarta Project's Struts Action servlet. By implementing Struts, ColdFusion MX offers a Model-2 Model-View-Controller (or MVC) environment for ColdFusion applications. This article is based on an implementation that uses ColdFusion MX Enterprise Edition on a Windows server. ColdFusion MX Enterprise Edition includes an embedded J2EE server based on Macromedia JRun. In addition to hosting ColdFusion pages, Enterprise Edition can host JSPs and servlets. This article is also applicable to users running ColdFusion on third-party application servers, although directory and filenames will vary somewhat.
First I give a brief description of my recent project, which shows how Struts and other Java objects provided a timesaving solution to a ColdFusion project. Next I explain MVC architectures so that you can define the role that Struts plays in your application's architecture. Then you learn how to transition your own development into a Struts framework. Finally I give you a sample application to help you get started using Struts in your own environment. (Code for this article can be downloaded from http://macromedia.com/devnet/coldfusion/articles/struts.html.)
Background: Saving Time with Struts
In the Java arena, we have adopted the Struts framework for our web services. By using Struts to implement a true Model-2 MVC architecture, we can split work among our various groups. Our layout group is fluent in designing dazzling layouts using Macromedia Flash. Our business managers design conceptual business objects and workflow. Our developers use Struts to create in code the objects and flow described by the business managers. While the members of each group have great talents in their assigned areas, they require a cursory understanding of the others' ingredients to a web-based application.
Our Client: One recent client was an internal ColdFusion development shop for a large association of plastic surgeons. Their task was to create web-based services for their membership. The members use these services to promote their private practices to the public. The success of these developers is measured by the number of referrals that are driven by the website to the doctors' private practices. To do this, the association strives to be a clearinghouse from which potential patients can find the best doctor for their needs.
They run their website on Windows servers running ColdFusion MX Enterprise Edition. (They also happen to run CommonSpot, a content management package from PaperThin. They often import ColdFusion pages into CommonSpot for consistency with the rest of the site. However, CommonSpot integration is beyond the scope of this article.)
The Project: We had to create private photo galleries that users could access from a doctor's personal page. Doctors publish their most successful procedures using before-and-after photographs along with a description of the procedure. The reason for this is so that the public will view these images and be encouraged to contact the doctor's office for a consultation.
In addition, the association wanted to offer a general-purpose gallery to the public. By drawing customers to the site, the association would lead patients to a doctor's private page and hopefully generate a referral. This general gallery would be a subset of the best examples found among the members' private galleries.
This project offered us many challenges. First of all, the images were categorized by procedure, each of which had a unique HTML form to describe the patient, treatment, and results of the surgery. This form needed to be converted into a descriptive paragraph suitable for public viewing. Some form of template-technology would be required for that conversion.
Second, the images submitted by the members were typically simple downloads from a digital camera. These images came in various dimensions and resolutions and needed to be resized to standard dimensions before being stored on the servers. Additionally, photos needed to be watermarked with some sort of copyright text so discourage web surfers from appropriating them for their own uses.
Third, we needed to build in all the sorts of features you expect to find in a good online photo-gallery service: grouping, indexing, filtering, and so on.
The ColdFusion and Struts Solution: As with any business, competitive concerns and published timelines required our client to roll out these new services quickly and reliably. The services also needed to match the look and feel of the rest of the website. In addition, the workflow was still being "worked out" as development began.
By using Struts, we enjoyed all the benefits of MVC architecture: Our layout team started designing the pages that reflected the look and feel of the existing ColdFusion MX site while our developers created the Beans that maintained the photo gallery pages. They also started building the generic structures for the procedure forms. We created and modified the process flow through simple modifications to the Struts configuration file, and each team created independent timelines and contributed pieces to the project as they became available.
Java object support proved to be a great timesaver. In our private libraries, we already had a Java object that used the Graphics2D class to quickly complete the image-modification features. We implemented Apache Velocity (http://jakarta.apache.org/velocity/)to create the paragraph templates in record time. From an earlier contract, we already had a skeleton Struts service for creating a website photo gallery. In all, ColdFusion's support for Java and Struts allowed us to reuse code and work in parallel, effectively halving the time to complete the job.
(In all fairness, Macromedia technologies alone could have solved most of our requirements. ColdFusion pages, ColdFusion components, and Macromedia Flash components offer all the functionality we required. However, the ability to reuse open-source code, not to mention mine our private libraries, motivated many of our choices.)
Examining the Struts Framework's Usefulness
The Controller is responsible for directing traffic between and among the Model and View objects. As the View submits information, that information is passed to the Controller, which implements the proper Model object for manipulating or storing the data. The Controller might invoke a new View to provide user feedback on the Model's success or failure to process that information. Likewise, when the View requests information, the request is submitted to the Controller. The Controller calls the necessary Model(s) to get the information and then passes that information to an appropriate View object for display. How‹or perhaps I should say where‹this Controller function is implemented is what differentiates Model-1 and Model-2 implementations of the MVC design.
A full description of Model-1 and Model-2 MVC implementations can be found in Designing Enterprise Applications with the J2EE Platform, Second Edition by Inderjeet Singh, Beth Stearns, Mark Johnson, and the Sun Microsystems Enterprise Team. This book explains that in a Model-1 architecture, application flow is determined by the links embedded in the JSP or CFM pages; whereas in a Model-2 architecture, a servlet (Controller) is placed between the web pages (the View) and the business objects (the Model) to determine program flow.
So now with all that behind us, we can define Struts as follows: Struts is an implementation of the Controller functionality in a Model-2 MVC architecture.
The next logical question to ask is how do you apply these various roles of MVC architecture to the ColdFusion MX environment? And which if any of these architectures is appropriate for your project?
Applications created using JSP or CFM pages have a tendency to embed some of the business logic within the current page. Although it's possible to separate logic from Views, which are later combined using includes, it's not uncommon for developers‹especially during the final hours of development‹to embed small pieces of logic in the View layer. What's more to the point is that the command richness of both JavaServer Pages and ColdFusion MX allows developers to break rules. Because these types of applications are considered non-MVC conforming designs, they do not perform well over heavy loads. Non-MVC applications are perhaps most appropriate for prototypes and small-scale applications.
Reader Feedback: Page 1 of 1
Featured White Papers
Latest Flex Stories & Posts
Subscribe to the World's Most Powerful Newsletters
Developer Links You Must Click On !