What Is Struts?


According to its creators , Struts is a "Web Application Framework." So, what does that mean? Let's look first at the Framework piece.

Frameworks

What's a framework?

The dictionary defines a framework as "A structure for supporting or enclosing something else, especially a skeletal support used as the basis for something being constructed ." This perfectly describes Struts: a collection of Java code designed to help you build solid applications while saving time. It provides the basic skeleton and plumbing; you focus on the layout and look of each room.

Interestingly, the dictionary offers an alternative definition: "A set of assumptions, concepts, values, and practices that constitutes a way of viewing reality." This describes Struts as well ”it's a way of looking at things. Struts saves you time by enabling you to view complex applications as a series of basic components: Views, Action classes, and Model components .

So, how does a framework save you time?

By using a framework, you don't have to spend time building your entire application. You can focus on coding the business logic and the presentation layer of the application, not the overhead pieces.

Using a framework also helps you encode best practices. The framework developers put a lot of thought into the best approaches to application building, so why rediscover them yourself?

Other benefits of using a framework are that it allows your code (at least in the case of Struts) to be highly platform independent. For example, the same Struts code that works under Tomcat on an old Windows NT machine should run using WebLogic Server on Linux or Solaris in production. In many cases, this can be accomplished without even recompiling. The same Web application (or .war file) simply can be copied from one host to another.

Another extremely important benefit ”especially if you're relatively new to Web development ”is that Struts gives you a place to start. Any developer will tell you that it's easier to take a basic application and modify it than it is to build something from scratch. This basic feature of Struts can save you days or weeks of planning and development.

Web Applications

Struts is for building Web applications.

By saying this, a couple implications are made. The first is that Struts is generally for building applications in which the client software is a browser.

The switch to building applications in which the client software is a browser is one of the greatest impacts of Internet technologies since 1995. Initially, this type of applications was primarily used on the Internet, and system users were generally using browsers. Toward the end of 1999 or so, it switched. Now all companies seem to want to build every application as browser-based.

This is due to the extremely compelling economics of the approach. It's cheaper for companies to deploy new applications to their internal users' desktops if the client software is a browser. If it's a client/server or other Win32-based client application written in Visual Basic or PowerBuilder, installing or upgrading the application requires software to be installed on the client machine's desktop. To install a new application if it runs in a browser simply requires sending the user a URL to point to.

Upgrades to Web-based applications are even easier to install. The end user does nothing at all! The next time the user comes to the site, he simply sees the latest version of the application!

So, whether your application is internal to your own organization or for use over the Internet, Struts can save you significant time building it when the client application is a browser.

The second implication of saying Struts is for building Web applications is a bit more formal. It's that Struts builds applications that are compliant Web applications, based on the Java Servlet specification (version 2.3 at the time of this writing). The Java Servlet specification is part of the larger J2EE (Java 2 Platform, Enterprise Edition) specification.

Being a compliant Web application (or Webapp, for short) implies, among other things, that Struts applications have

  • A standard directory structure

  • Certain standard configuration files ( web.xml and so on)

  • Dynamic functionality deployed as Java classes and .jsp pages

  • A standard Web Archive ( .war file) format for deployment

Although it's not required reading, it would be very useful to you as a Struts developer if you're familiar with (or at least have handy a copy of) the Java Servlet specification version 2.3 (or 2.2, depending on the application server you're using). This document will help you understand some of the formal Web application pieces of the framework. This is reviewed in more detail in the next chapter, so don't worry about reading the entire spec right now!

If you're unsure what a web.xml file is or why the directories always need to be named WEB-INF , details like this come from the Web application spec inside the Java Servlet specification.

Components of the Struts Framework

The Struts framework is based on two primary components: a Model-View-Controller architecture that makes it easy to build flexible applications and a set of JSP custom tags for building JSP pages.

Even though they're covered in more detail in the next chapter, let's take a quick look at these components right now.

Struts Model-View-Controller Architecture

Struts Model-View-Controller architecture simplifies building Web applications by providing a model into which you plug components. Take, for example, a simple application for updating a user's address information. In this case, the Model-View-Controller architecture might break the application into the following components:

Model:

Address.java ” A programming model of the user's address. Address.java would provide a simple way of setting and getting components of the user's address, and reading it from or storing it to a more permanent storage location (such as a database).

View:

AddressView.jsp ” A view to be used to display the user's information. AddressView.jsp would contain very little in the way of conditional logic; it simply takes the values in Address.java and displays them.

Controller:

AddressAction.java ” A controller to assist in validating the user's entries and choosing the right view to display the results. If errors are made in entering the data, AddressAction.java makes the decision to display an error page or send the user back to the original entry page.

Although this breakdown is simplified for illustration, you get the idea. The Model provides an internal representation of the data. The View doesn't make decisions, it simply displays data. The Controller determines what processing to perform and what steps to take next.

The Struts Tag Libraries

The Struts Tag libraries are used for creating View components.

One of the challenges of JSP development has traditionally been that programmers have a tendency to frequently use Java scriptlets in their pages. This makes the pages more complicated for an HTML designer to understand and maintain.

The Struts tag libraries provide a set of JSP custom tags that are generally understandable by both JSP developers and page designers. The custom tags have names like <html:text> and <logic:iterate> . You'll learn more about what these tags do in Chapters 12 through 16, which cover the tag libraries in detail, but for now understand that the Struts tag libraries provide a powerful set of functionality in a format designed to simplify developing pages (or Views , in Struts parlance).



Struts Kick Start
Struts Kick Start
ISBN: 0672324725
EAN: 2147483647
Year: 2002
Pages: 177

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