1、网上文件管理系统论文英文Exploring the Struts architectureThis article covers_ Introducing application frameworks, MVC, and Model 2_ Understanding how Struts works_ Using the Struts control flow_ Exploring the strengths and weaknesses of Struts1 Talking the talkThis article explores the Struts framework in depth
2、 and highlights the benefits Struts can bring to your development efforts. We believe that once you can “talk the talk” of web architecture and design, you will be better equipped to use Struts with your own applications.With a sound overview of the Struts architecture in place, we outline the Strut
3、s control flow and the way it handles the request-response event cycle. A good understanding of this process makes it much easier to create applications that make the best use of the framework.Choosing a web application framework should not be a casual decision. Many people will use this book, and e
4、specially this chapter, as part of evaluating Struts for their project. Accordingly, we conclude this chapter with a candid look at the strengths and weaknesses of the Struts framework and address concerns regarding overall performance. Struts is designed for professional developers. To makeinformed
5、 decisions, professionals need to be aware of both a tools capabilities and its limitations.2 Why we need StrutsTodays web applications are critical components of the corporate mission. As always,development teams need to build applications in record time, but they have to build them right and build
6、 them to last. Java web developers already have utilities for building presentation pages, such as JavaServer Pages and Velocity templates. We also have mechanisms for handlingdatabasesJDBC and Enterprise JavaBeans (EJBs), for example. But what do we use to put these components together? We have the
7、 plumbing and the drywall what else do we need?2.1 One step back, three steps forwardIn the late 1970s, when graphical user interfaces (GUIs) were being invented,software architects saw applications as having three major parts: the part that Why we need Struts 31 manages data, the part that creates
8、screens and reports, and the part that handles interactions between the user and the other subsystems Ooram. In the early 1980s, the ObjectWorks/Smalltalk programming environment introduced this triumvirate as a development framework. In Smalltalk 80 parlance, the data system is dubbed the Model, th
9、e presentation system is called the View, and the interaction system is the Controller. Many modern development environments, including Javas Swing, use this Model/View/Controller (MVC) architecture (see figure 2.1) as the foundation of their own frameworks. Java web developers already have capable
10、tools, such as JDBC and JSP, for consultingthe Model and creating the View, but wheres the Controller for our webapplications?2.2 Enter StrutsThe centerpiece of Struts is an MVC-style Controller. The Struts Controller bridges the gap between Model and View. The framework also includes other missing
11、pieces developers need to write scalable, leading-edge web applications. Struts is a collection of “invisible underpinnings” that help developers turn raw materials like databases and web pages into a coherent application.2.3 Struts controller componentsThe Struts controller is a set of programmable
12、 components that allow developers to define exactly how their application interacts with the user. These components hide nasty, cumbersome implementation details behind logical names. Developers can program these details once, then go back to thinking in terms of what the program does rather than ho
13、w it does it. Users interact with a web application through hyperlinks and HTML forms. The hyperlinks lead to pages that display data and other elements, such as text and images. The forms generally submit data to the application via some type of custom action.As shown in figure 2.2, Struts provides
14、 components that programmers can use to define the hyperlinks, forms, and custom actions that web applications use to interact with the user. We used these components to build a starter application in chapter 1. In chapter 3, we walk through using these components to build another simple application
15、. Then, in chapter 4, we provide a detailed overview of configuring these components. Later chapters provide more detail about putting each component to use within your application. In part 4 we demonstrate using the components in the context of working applications. But, since this chapter is the a
16、rchitectural overview, lets go ahead and introduce the major Struts components now.NOTE The Struts components are configured via XML.HyperlinksTo the application developer, a hyperlink is a path to some resource in the application.This may be a web page or a custom action. It may also include specia
17、l parameters. In Struts, developers can define a hyperlink as an ActionForward. These objects have a logical name and a path property. This lets developers set the path and then refer to the ActionForward by name. ActionForwards are usually defined in an XML configuration file that Struts reads when
18、 the web application loads. Struts uses the XML definitions to create the Struts configuration, which includes a list of ActionForwards. The XML element that would create an ActionForward for a welcome hyperlink might look like this:This element would create an ActionForm JavaBean with its name prop
19、erty set to welcome and its path property set to /pages/index.jsp. JSP pages and other components can then refer to the welcome forward. The Struts framework will look up the welcome ActionForward bean and retrieve the path to complete the hyperlink. This allows developers to change the destinationo
20、f a link without changing all the components that refer to that link. In most web applications, details like this are hardcoded into JSP and Java code, making changes difficult and prone to error. In a Struts application, these details can be changed throughout the application without touching a sin
21、gle page or Java class.For more about ActionForwards, see chapter 6.HTML formsThe web protocols, HTTP and HTML, provide a mechanism for submitting datafrom a form but leave receiving the data as an exercise for the developer. The Struts framework provides an ActionForm class, which is designed to ha
22、ndle input from an HTML form, validate the input, and redisplay the form to the user for correction (when needed), along with any corresponding prompts or messages. ActionForms are just JavaBeans with a couple of standard methods to manage the validation and revision cycle. Struts automatically matc
23、hes the JavaBean properties with the attributes of the HTML controls. The developer defines the Action-Form class. Struts does the rest.This class will automatically populate the username field from a form with anHTML form element of the same name, as shown here:public final class LogonForm extends
24、ActionForm private String username = null;public String getUsername() return (this.username);public void setUsername(String username) this.username = username;Other properties would be added for each field of the form. This lets other components get what they need from a standard JavaBean, so everyo
25、ne does not have to sift through an HTTP request. The ActionForm classes are created using normal Java classes. The Struts configurationrefers to the ActionForm classes through a set of descriptors: the and elements. The elements are descriptors that the framework uses to identify and instantiate th
26、e ActionForm objects, as shown here:The Struts configuration lists the ActionForm beans it uses and gives the Action-Form classes a logical name to use within the application. 1.0 vs 1.1 In Struts 1.1 the ActionForm can also use a Map (java.util.Map) to store the attribute names rather than define i
27、ndividual properties. A new type of JavaBean, the DynaBean, can also be used with Struts 1.1 and later. You can specify the properties for a DynaActionForm by using an XML element. In effect, this does let you define ActionForms in the Struts configuration file.Custom actionsAn HTML form uses an act
28、ion parameter to tell the browser where to send the forms data. The Struts framework supplies a corresponding Action class to receive such data. The framework automatically creates, populates, validates, and finally passes the appropriate ActionForm to the Action object. The Action can then get the
29、data it needs directly from the ActionForm bean. Heres an example:public final class LogonAction extends Action public ActionForward perform(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response)throws IOException, ServletException MyForm myForm = (MyForm) for
30、m;/ .return mapping.findForward(continue);An Action concludes by returning an ActionForward object to the controller. This allows the Action to choose a definition using logical names, like continue or cancel, rather than system paths.To ensure extensibility, the controller also passes the current r
31、equest and response object. In practice, an Action can do anything a Java Servlet can do.In addition to the ActionForward, ActionForm, and Action objects, the Struts controller layer provides several other specialized components, including Action- Mappings and the ActionServlet. Struts also supports
32、 localizing your application from the controller layer.ActionMappingsIn a web application, every resource must be referred to through a Uniform Resource Identifier (URI). This includes HTML pages, JSP pages, and any custom actions. To give the custom Actions a URI, or path, the Struts framework provides an ActionMapping object. Like the ActionForwards and ActionForms, the mappings are usually
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1