advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement

Reviews : FreeMarker: Separating the Model and Presentation Layers :

System Flow/Architecture

FreeMarker's strength is derived in separating the model layer from the presentation layer. FreeMarker does this by having a data model to represent the model layer and a template to represent the presentation layer. Marrying the data model and the template produces the desired result. The result could be Java classes, HTML, XML, plain text, RTF, or any output. This paradigm makes it easier to separate the business logic from the presentation cleanly and gives developers and designers the independence they crave. Normally, developers can't start working on the user interface until the designers are done with the look and feel. Then, developers are forced to wade through tons of HTML tags, stylesheets, and DHTML code to insert the business logic to make the page dynamic. Once the business logic is embedded, the desired look and feel is often not achieved due to misaligned CSS tags or incomplete HTML elements. Designers then have to make modifications again to regain the look and feel. This reworking process costs time, money, and makes code maintenance a nightmare. Just imagine having to make modifications to the page!

FreeMarker allows programmers and developers to work independently on the model layer and the view layer without stepping on each other’s toes. A designer can start working on the presentation layer with just the outline of a data model. The designer creates the presentation layer in their tool of choice and references elements in the data model using a simple scripting language. Meanwhile, developers can independently work on the model layer and populate the data model with the actual values that need to be inserted into the presentation layer.

The Data Model

The data model is a collection of data arranged in a tree format. At the very beginning is the root element and from it sprouts the different branches of data. Branches have logical groups of data that are specific to a view. They can be independent or shared, depending on the requirements of the presentation layer. The data in a branch can be anything from simple data types like integer, string, or Boolean to complex data types like maps, lists, and Java beans. FreeMarker converts the DataModel to objects of the type freemarker.template.TemplateModel. It uses an ObjectWrapper (freemarker.template.ObjectWrapper) to wrap the Java type in an appropriate TemplateModel. For instance, strings are wrapped as SimpleScalar, a Java object of type Number maybe wrapped by SimpleNumber and so on. Java Beans and other objects are wrapped using freemarker.ext.beans.BeansWrapper. The wrapping of Java beans is done through Java reflection.

Templates

A template is a file that contains static text interpolated with special FreeMarker tags in them. The tags give the dynamic quality to the template and can be used to insert data dynamically from the model. As discussed earlier, FreeMarker tags are written in the FreeMarker Template Language (FTL). They are straightforward and can be used by any designer with a little experience with scripting languages.

FreeMarker Template Language (FTL):

Interpolation is the process by which part of a string, which is marked with special tags, is processed and substituted with processed output before the whole string is printed to the target output stream. Here's the syntax for string interpolation:
 ${ variable_name }
Everything that needs interpolation must be within the ${ and } tags. The variable_name should be an element from the DataModel.

FTL Directives enable users to process and format data just like you would in a regular programming language. Some of the built-in directives provided by FreeMarker are loop constructs, inclusion of external data, decision points etc.

FTL Directives are similar to HTML tags, in that:

  • They must be in angular brackets <>, with the difference being that the first character after the start angular bracket must be the #. For instance, <#list>.
  • Every tag has a start and end tag. The end tag should be the same as the starting tag.
  • Every tag must be properly nested.
FTL Expressions are a combination of variables from the DataModel, arithmetic operators, logical operators, interpolations, etc. FTL Expressions are used to gather and process data from different elements before they are evaluated. This allows for scripting language-like flexibility within the presentation layer.

After the template has been loaded, it is parsed, merged with the DataModel, and then printed to the desired output stream. Once a template has been parsed, it is cached and subsequent requests for the same template results in the same instance being returned. This results in optimal use of memory and enhances execution speed.

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.

 Intel Go Parallel Portal
 Internet.com eBook Library
 IBM Software Construction Toolbox
 Microsoft RIA Development Center
 Destination .NET
XML error: not well-formed (invalid token) at line 43
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

Remember Figlets? They're Back With Zend
Microsoft Readies an App Store Competitor?
Google: Chrome Browser Will Make Money
Sam Ramji: Microsoft's Man in Open Source
Google to Shake Up Browsers With Own Launch
Mozilla's Ubquity Mashup: For The Masses?
iPhone Users Just Want to Have Fun
Oops! I Fixed the Linux Kernel
Jim Zemlin: The New Center of Linux Gravity
Microsoft's Novell Investment Tops $340M

Writing Functional Code with RDFa
BitLocker Brings Encryption to Windows Server 2008
Network Know-How: Exploring Network Algorithms
Create a Durable and Reliable WCF Service with MSMQ 4.0
The Baker's Dozen: 13 Tips for SQL Server 2008 and SSRS 2008
Book Excerpt: Microsoft Expression Blend Unleashed
Develop a Mobile RSS Feed the Easy Way
State of the Semantic Web: Know Where to Look
A 3D Exploration of the HTML Canvas Element
Setting Up and Running Subversion and Tortoise SVN with Visual Studio and .NET

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Intel PDF: Virtualization Delivers Data Center Efficiency
Intel eBook: Managing the Evolving Data Center
Microsoft Article: BitLocker Brings Encryption to Windows Server 2008
Symantec eBook: The Guide to E-Mail Archiving and Management
Microsoft Article: RODCs Transform Branch Office Security
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
Avaya Article: Advancing the State of the Art in Customer Service
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Avaya Article: Avaya AE Services Provide Rapid Telephony Integration with Facebook
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Seminar: Efficiencies in Hardware/Software Virtualization
HP Webcast: Disaster Recovery Planning
Go Parallel Video: Performance and Threading Tools for Game Developers
HP Video: StorageWorks EVA4400 and Oracle
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
IBM TCO eKIT: Your IT Budget is Under Attack, Get in Control
IBM Energy Efficiency eKIT: Learn How to Reduce Costs
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt and free High-Performance SQL Code eBook
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
Microsoft Article: Silverlight Streaming--Free Video Hosting for All
Featured Algorithm: Intel Threading Building Blocks - parallel_reduce
HP Demo: StorageWorks EVA4400
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES