2024年5月5日发(作者:)

Struts, an open-source MVC implementation

Introduction

Kids in grade school put HTML pages on the Internet. However, there is a

monumental difference between a grade school page and a professionally developed

Web site. The page designer (or HTML developer) must understand colors, the

customer, product flow, page layout, browser compatibility, image creation,

JavaScript, and more. Putting a great looking site together takes a lot of work, and

most Java developers are more interested in creating a great looking object interface

than a user interface. JavaServer Pages (JSP) technology provides the glue between

the page designer and the Java developer.

If you have worked on a large-scale Web application, you understand the

term change. Model-View-Controller (MVC) is a design pattern put together to help

control change. MVC decouples interface from business logic and data. Struts is an

MVC implementation that uses Servlets 2.2 and JSP 1.1 tags, from the J2EE

specifications, as part of the implementation. You may never implement a system

with Struts, but looking at Struts may give you some ideas on your future Servlets

and JSP implementations.

In this article, I will begin with a JSP file that uses elements you may be familiar

with and discuss the pros and cons of such a page. I will then cover Struts and how it

can control change in your Web project and promote specialization. Finally, I will

re-develop the simple JSP file with the page designer and change in mind.

A JSP file is a Java servlet

A JavaServer Page (JSP) file is nothing more than another way to view a servlet.

The concept of a JSP file is to allow us to see a Java servlet as an HTML page. This

view eliminates all of the ugly print() statements that normally show up in Java code.

The JSP file is pre-processed into a .java file, then compiled into a .class. If you are

using Tomcat, you can view your files in the work directory.

Other containers may store the .java and .class files elsewhere; the location is

container specific. Figure 1 demonstrates the JSP file-to-servlet flow.

Figure 1. JSP file-to-servlet flow

(This is significantly different from a Microsoft Active Server Page (ASP). An ASP is

compiled into memory, not into a separate file.)

The simple self-contained JSP file

In a small JSP application, it is common to see the data, business logic, and the user

interface combined into one module of code. In addition, the application generally

contains the logic that controls the flow of the application. Listing 1 and Figure 2

demonstrate a simple JSP file that allows a user to join a mailing list.

Listing 1. -- a simple request and response JSP file

<%@ page language="java" %>

<%@ page import="tion" %>

<%@ page import="gList" %>

<%

String error = "";

String email = ameter("email");

// do we have an email address

if( email!=null ) {

//

if( dEmail(email) ) {

//

try {

il(email);

} catch (Exception e) {

error = "Error adding email address to system. " + e;

}

if( ()==0 ) {

%>

// redirect to

<%

}

} else {

// set error message and redisplay page

error = email + " is not a valid email address, please try again.";