28 November 2005

Model View Contoller Architecture

Ever wondered how some developers get such lovely URL's, and how they manage to add oodles of content to their web sites in a breeze? How'd they make that web site so quickly and what type of application server uses .do and .view pages anyway? Well, the answer is simple, most of them use a Model-View-Controller (or MVC) pattern. Stay with me and I'll give you a little taste into this new way of developing web applications.

This article discusses the MVC pattern. For this example, we'll be using a J2EE (Java) container (web server) such as Tomcat or JBoss. Instead of writing the system from scratch, we'll be using the popular Struts framework. Views (templates) are rendered either using JSP (JavaServer Pages) technology, JSF (JavaServer Faces) or XSLT.

Most web sites are created using a Model 1 or page-centric approach. This means that the functionality of the web site is based on the page that is requested. This is limiting, having your functionality split over so many pages.

In the MVC approach, you have a single controller that processes all requests. This controller takes the request, runs it through a xml mapping file, and, depending on the configuration of the mapping file, forwards the processing of logic on to a java component (object that you write to do the logic). The component does the work, and stores the "state" or "result" in what's known as a model (a fancy word for some kind of data object stored in the request or session or disk). One the business logic is complete, the component gives processing back to the controller who then decides on a view. The view (template) gets populated by variables in the model, and wham, your client has their web page.

Sun's Java (J2EE) makes a really great platform for this. In a Java-based web application, you can add a web.xml file to the application that instructs to the web server (like JBoss or Tomcat) how to handle requests, etc. In this file, you can map a URL request pattern such as *.do or /home/* to a single servlet (Java web component). This servlet, known as the controller servlet, then processes the request and passes execution on the model (buiness logic) and view (template).

Let's look at some of the adantages of this approach before looking at some pseudo-code:

1. Easy creation of content as there are no actual pages.
2. Complete re-use of components because of great seperation of presentation and logic.
3. Easily convert applications for other browsers / WAP / web services using different views.
4. Use pre-built frameworks (such as struts or JSF) and save thousands of hours coding.
5. Easily manage an application flow from a single XML-based configuration file.
6. The controller handles validation and form data. Your application can simply read out these values.

Struts is a great code-based framework to start your first MVC architecture with. In order to develop a struts application, you need to have a running instance of the Tomcat Web Server and the Struts Framework.

Lastly, the view, is typically a JSP page (JavaServer Pages) page or template with some simple custom tags (little variables) in it. The JSTL (JSP Standard Tag Library) provides a set of html-like tags that can be used to access data objects and embed them in a page.

A fairly new technology, JSF (JavaServer Faces) also provides a "tag language" for accessing and displaying re-usable web components (widgets). This can be used in place of the JSTL library. Some folk also prefer to use XSLT (a SGML-based W3C specification for transofming XML documents into other types; like XHTML; using a special XSL style sheet) to render their views. There is a great article showing the use of Struts with XSLT on JavaWorld. But first, get into Struts with this article.

Hope this introductory article proved useful to you! Enjoy!

Web Discoveries Blog

This is my new weblog where I'm going to mention all those wonderful web site development tips and tricks that I've discovered. Hopefully, I'll make a little money off adsense too.