Thứ Tư, 21 tháng 10, 2009

Introduction to Struts


Goals After completing this chapter, the student will be able to
  • understand the MVC architecture.
  • set up an application using Struts.
  • use the Struts bean and html tags.
  • process user input from HTML forms through Struts.
Prerequisites The student will need to have an understanding of JSP, JavaBeans, and custom tags.
Objectives This chapter is presented to provide the student with an understanding of the Struts framework.

The Model-View-Controller Architecture

"Model-View-Controller" is a way to build applications that promotes complete separation between business logic and presentation. It is not specific to web applications, or Java, or J2EE (it predates all of these by many years), but it can be applied to building J2EE web applications.

The "view" is the user interface, the screens that the end user of the application actually sees and interacts with. In a J2EE web application, views are JSP files. For collecting user input, you will have a JSP that generates an HTML page that contains one or more HTML forms. For displaying output (like a report), you will have a JSP generates an HTML page that probably contains one or more HTML tables. Each of these is a view: a way for the end user to interact with the system, putting data in, and getting data out.

When the user clicks 'Submit' in an HTML form, the request (complete with all the form information) is sent to a "controller". In a J2EE web application, controllers are JavaBeans. The controller's job is to take the data entered by the user (in the HTML form that the JSP generated) and pass it to the "model", which is a separate Java class that contains actual business logic. The model does whatever it does (for instance, store the user's data in a database), then returns some result back to the controller (perhaps a new ID value from the database, or perhaps just a result code saying "OK, I'm done"). The controller takes this value and figures out what the user needs to see next, and presents the user with a new view (for instance, a new JSP file that displays a confirmation that the data they entered was successfully saved).

This all sounds like a lot of work, and it is. But there is a point to architecting applications this way: flexibility. The beauty of model-view-controller separation is that new views and controllers can be created independently of the model. The model -- again, this is pure business logic -- knows nothing of HTML forms or JSP pages. The model defines a set of business functions that only ever get called by controllers, and the controllers act as proxies between the end user (interacting with the view) and the business logic (encapsulated in the model). This means that you can add a new view and its associated controller, and your model doesn't know or care that there are now two different ways for human beings to interact with the application.

For instance, in an application with complicated data entry screens, you could add a JSP that generated a quick-edit form with default values instead of a longer, standard form. Both JSPs (the short form and the long form) could use the same controller; default values could simply be stored in the HTML

as hidden input values, and the controller would never know the difference. Or you could create a completely new interface -- a desktop application in addition to a web application. The desktop application would have views implemented in some interface-building tool, and have associated controllers for each screen. But these controllers would call the business functions in the model, just like the controllers in the web application. This is called "code reuse", specifically "business logic reuse", and it's not just a myth: it really can happen, and the Model-View-Controller architecture is one way to make it happen.


What is Struts?

Struts is a framework that promotes the use of the Model-View-Controller architecture for designing large scale applications. The framework includes a set of custom tag libaries and their associated Java classes, along with various utility classes. The most powerful aspect of the Struts framework is its support for creating and processing web-based forms. We will see how this works later in this chapter.


Struts Tags

Common Attributes

Almost all tags provided by the Struts framework use the following attributes:

Attribute Used for
id the name of a bean for temporary use by the tag
name the name of a pre-existing bean for use with the tag
property the property of the bean named in the name attribute for use with the tag
scope the scope to search for the bean named in the name attribute

Referencing Properties

Bean properties can be referenced in three different ways: simple, nested, or indexed. Shown here are examples for referencing the properties each way:

Reference Method Example
simple
nested
indexed

flavorful mix of methods

Creating Beans

Beans are created by Java code or tags.

Here is an example of bean creation with Java code:

// Creating a Plumber bean in the request scope
Plumber aPlumber = new Plumber();
request.setAttribute("plumber", aPlumber);

Beans can be created with the tag:




creating/using a bean in session scope of type java.lang.String

Most useful is the creation of beans with Struts tags:






Other Bean Tags

The Struts framework provides other tags for dealing with issues concerning copying cookies, request headers, JSP implicity defined objects, request parameters, web application resources, Struts configuration objects, and including the dynamic response data from an action. These tags are not discussed here, but it is important to be aware of their existence.










Bean Output

The and tags from the Struts framework will write bean and aplication resources properties into the current HttpResponse object.


This tag allows locale specific messages to be displayed by looking up the message in the application resources .properties file.








This tag writes the string equivalent of the specified bean or bean property to the current HttpResponse object.




Creating HTML Forms

Quite often information needs to be collected from a user and processed. Without the ability to collect user input, a web application would be useless. In order to get the users information, an html form is used. User input can come from several widgets, such as text fields, text boxes, check boxes, pop-up menus, and radio buttons. The data corresponding to the user input is stored in an ActionForm class. A configuration file called struts-config.xml is used to define exactly how the user input are processed.

The following diagram roughly depicts the use of Struts for using forms.


The Struts html tags are used to generate the widgets in the html that will be used in gathering the users data. There are also tags to create a form element, html body elements, links, images, and other common html elements as well as displaying errors. Below are the tags provided by html section of the Struts framework and a short description of each.

Note: Most of the tags for use with forms are able to take the name and property attributes representing the initial state of the widgets and for capturing the state that the widgets were in when a form submission occurred.


Generates an tag. This tag should be used inside a element.
Generates an tag and causes the Action servlet not to invoke its validate() method. This tag should be used inside a element.

Generates an .

Generates an . "Checkedness" depends upon whether the property array specified contains a corresponding value as the one specified for the multibox.


Generates a tag. This tag should be used inside of a tag.



Wheat Wood Clay
Stone Sheep
Generates html to display any errors that may have occurred during invocation of the validate() method.

Generates
.
There is a hidden element here which is invisible. :-)
Generates . Generates .

A link to an external site
Generates an html link.
Generates for use in collecting information that should not be shown on-screen.
Credit Debit

Generates a radio button ().

Generates .





Generates


Generates to submit form data entered by the user.

Name
Email Address
Generates .

Generates

Không có nhận xét nào: