The MVC Pattern

I l @ ve RuBoard

Until now, you've been using what the programming hotshots call "model 1" designs. In a model 1 site, the JSP provides both the display and the flow control of the site, and it hooks up to the business logic as well.

Model 1 sites have a couple of problems, especially when you start dealing with large, complex applications. For one thing, the JSP starts to get pretty unwieldy because you have to place more flow control into it.

In addition, you end up with a lot of cases in which you begin to process a JSP, realize that you need to move on to the next step, and have to do a redirect in the middle of the file.

You also find yourself with hard-coded URLs throughout your JSP and have a huge task on your hands every time you want to update a filename or move a directory.

A solution exists in the form of "model 2" platforms. Model 2 is also known as MVC (Model-View-Controller), a design pattern that originated with the SmallTalk language.

Here's a simple example that highlights the difference in approaches. Let's say that you want to let a user click a button on a page that will take them to a signup form, where they then can fill in their information. Then when they submit the page, it writes it to the database and takes them back to the main page.

In a model 1 approach, the link would be an HREF that takes the user to a different page that has the form on it. The user fills in the form and hits Apply, and the submit goes to yet another page that processes the data, redisplays the form (with errors highlighted, if there's a problem), and otherwise writes the data to the database and goes to the main page via an explicit redirect.

In the MVC approach, the URL specified in the HREF on the main page doesn't actually refer to a hard-coded page. Instead, it is a functional reference that tells the MVC controller which actions to take when it is requested . In this case, the controller determines that it's he signup form that needs to be displayed.

When the user submits the form, the MVC controller first sends it off to be validated . If the validation fails, the controller returns to the same form with the errors displayed. If the validation succeeds, the controller passes it on for processing and then returns the user to the main page again.

The difference is that, in a MVC design, all the control rests in the hands of the controller (appropriately, don't you think?). The JSP pages have no knowledge of where actions lead.

I l @ ve RuBoard


MySQL and JSP Web Applications. Data-Driven Programming Using Tomcat and MySQL
MySQL and JSP Web Applications: Data-Driven Programming Using Tomcat and MySQL
ISBN: 0672323095
EAN: 2147483647
Year: 2002
Pages: 203
Authors: James Turner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net