Wednesday, July 23, 2008

Model-View-Controller

The Struts framework was created by Craig R. McClanahan and donated to Apache Software Foundation (ASF) in 2000. A framework provides generic, cooperative components that your application extends to deliver a particular set of functions. So it is also referred as an "upside-down" library. The Struts framework provides three key components for the implementation of the MVC structure.
  • A "request" handler provided by the application developer that is mapped to a standard URI.
  • A "response" handler that transfers control to another resource which completes the response.
  • A tag library that helps developers create interactive form-based applications with server pages.
Others
Cocoon project founded by Stefano Mazzocchi in January 1999 is an open source project the ASF. Cocoon leverages XML, XSLT, and Simple API for XML (SAX) to help create, deploy, and maintain XML server applications.

Velocity is a Jakarta project like Struts. It is a Java-based template engine that can generate SQL, PostScript, and XML form templates.

JavaServer Faces (JSF) project is to provide a standard set of JSP Tags and classes to aid in the management of complicated HTML forms, event handling, and presentation state.

Jakarta Turbine is a servlet-based framework. A large set of components is included with the framework, including those for using relational databases, security, and scheduling, etc.

WebWork is a web application framework that uses the Pull Hierarchical Model View Controller (HMVC) design. With a standard MVC design, changes made to the model are pushed to the view. In the case of WebWork, the views pull the data when they need it.

Spring is a J2EE application framework developed by Rod Johnson. Spring is not just a web framework. It can integrated with other disparate components.

Apache Struts 2 was originally known as WebWork 2. After working independently for several years, the WebWork and Struts communities joined forces to create Struts2.

Controller - MVC
The controller portion of the web tier MVC design generally is a Java Servlet. The Front Controller pattern, which is part of the J2EE Design Patterns, describes how a web tier controller should be implemented. Code that would normally need to put in every JSP page can be put in the controller servlet, which processes all the request.

In the Struts framework, the controller responsibilities are implemented by several different components, one of which is an instance of the org.apache.struts.action.ActionServlet class. The ActionServlet extends the javax.servlet.http.HttpServlet class and its responsible for packaging and routing HTTP traffic to the appropriate handler in the framework.

An org.apache.struts.action.Action class in the Struts framework is an extension of the controller component. It decouples the client request from the business model. This allows for more than a one-to-one mapping between the user request and an Action. The Action class can perform other functions, such as authorization, logging, and session validation, before invoking the business operation.
Note: In Struts 2, POJO Actions - use any class as an Action class - even the interface is optional. We can inject dependencies into Actions using Spring without glue code or red tap.

Model - MVC
In an enterprise application, the model portion of MVC pattern will be Enterprise JavaBeans. The Struts framework does not have a great deal of support in the way of model components. This is better left for EJB, CORBA, or some other type of component framework.

The EJB 2.0 made performance improvements through the use of local interfaces. However, due to the overhead of making remote calls, there still can be a significant performance impact if the web tier attempt to use entity beans directly as the model portion of the application. Therefore, JavaBeans are returned from session beans and used within the web tier. These JavaBeans are referred to as data transfer object (DTO) and are used within the views to build the dynamic contents. In a distributed application, usage of DTOs reduces the network bandwidth and improve response.

In Struts, application data is encapsulated in Java objects. User input is used to populate an action form upon form submission, business methods accept transfer objects as arguments, and custom tags are designed to manipulate JavaBeans. You can keep business logic out of the Action classes to protect against change. The Action is free to put the object in the request or session and then forward it to a JSP, where the data can be extracted and presented to the user.

View - MVC
The views within the web tier MVC pattern typically consist of HTML and JSP pages. The view components employed in a Struts application are:
  • HTML
  • DTO
  • Struts ActionForms
  • JSP
  • Custom tags
  • Java resource bundles
The Struts framework (ActionForm objects) automatically collects the input from the request and passes the data to an Action using a form bean. To keep the presentation layer decoupled from the business layer, DTO was created by using the data from the ActionForm.
Note: In Struts 2, no more ActionForms. POJO forms - use any JavaBean to capture form input or put properties directly on an Action class. Use both binary and String properties!

JSP pages make up the majority of what has to be built for the Struts view components. Combined with custom tag libraries and HTML, JSP makes it easy to provide a set of views for an application.

The Struts framework provides six core tag libraries. We may extend the Struts tags or create our own custom tags. The custom tag libraries that are included with the framework are: HTML, Bean, Logic, Template, Nested, and Tiles tag libraries.

Note: The Struts 2 adds interactivity and flexibility with AJAX tags that look and feel just like standard Struts tags.

No comments: