JavaRanch Home    
 
This page:         last edited 01 August 2013         What's Changed?         Edit

Model1, Model2 and MVC   

What's the difference between Model 1 and Model 2 JSP programming?

What's the difference between MVC1 and MVC2?

Does my app need Struts to be MVC?

A little history lesson...

The first Java technology for server-side web development was the servlet. Writing applications with servlets was very similar to writing CGI applications in Perl in that all of the output had to be built up as Strings from within Java code. This was very tedious and error-prone. It also made it very difficult for web designers with no Java experience to alter the look and feel of the pages generated by servlets.

Then came JSP. JSPs, like Microsoft ASPs and like the popular scripting language PHP, treat everything as template text, but allow the insertion of Java code into tags called scriptlets and JSP expressions. This allowed people to work on server-side applications just as they would with the other popular scripting languages but it had a couple of drawbacks. There was no separation of concerns. One script would hold database code, business logic, HTML markup and any javascript code needed for the final page rendering. Code reuse was difficult as was automated testing. This came to be known as "Model 1" JSP programming.

MVC

MVC or the Model View Controller pattern was a common technique for separating the various concerns in GUI code invented by Trygve Reenskaug, working on Smalltalk for Zerox. (reference)

At some point it became clear that this technique could be adapted to Java EE applications to achieve the same level of separation. Doing so involves writing the Model layer as Beans or Plain Old Java Objects (POJOs), using servlets as the Controller, and then, when all the heavy lifting is done, forwarding to a JSP to format and markup the results. Servlet/JSP applications written using and MVC architecture came to be known as Model 2 JSP programming.

Because this pattern existed in a different form before being used in servlet/JSP applications, it was sometimes referred to as "MVC2". This name led to some confusion as it implied that there is an MVC1 for servlet applications, which there is not. It is sufficient just to say MVC.

Frameworks

Initially JSP lacked the means to do simple branching and iteration without resorting to scriptlet code. Having any scriptlet code in a JSP tends to lead to having a little more scriptlet code which leads to having lots of scriptlet code. This meant that many applications which started out with a nice clean MVC architecture, over time, became a mixture of model 1 and model 2 code and were just as hard to maintain as Servlets or JSP alone.

Frameworks like Apache Struts provided a set of pre-written custom tags and a structured set of Java classes to help enforce the MVC policy, and to eliminate a lot of the structural back end work. Over time a lot of framworks showed up, each with various degrees of complexity and different approaches to assisting and enforcing the MVC pattern. One of our staff, Bear Bibeault has written a very small framework named FrontMan which is being used by several other staff members in their own projects.

Depending on your needs, a framework may or may not make your life easier. It is certainly possible to write an entire web application using the MVC pattern without adopting a framework. This has been made easier in JSP 2.0 thanks to the Java Standard Tag Library (JSTL) specification and the new JSP Expression Language (EL).

Learning

We at JavaRanch feel that a newcomer to servlet/JSP programming is better off learning to code MVC applications without a framework, at least until comfortable with the pattern. We have a very simple Demo application in our CodeBarn called Simple MVC (CodeBarnSimpleMvc) and, in our Cattle Drive, there is an MVC assignment

Learning and understanding both the fundamentals of JSP and Servlet programming and the MVC pattern is the best way to ensure that you're equipped with the knowledge necessary to evaluate which, if any, framework is right for your project.



ServletsFaq

JspFaq

TomcatFaq

StrutsFaq


JavaRanchContact us — Copyright © 1998-2014 Paul Wheaton