`
trix
  • 浏览: 82366 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论
阅读更多

Exploring the Struts architecture

Talking the talk

This chapter explores the Struts framework in depth 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 Struts 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 especially 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 make informed decisions, professionals need to be aware of both a tool’s capabilities and its limitations.

Why we need Struts

Today’s 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 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 handling databases—JDBC and Enterprise JavaBeans (EJBs), for example. But what do we use to put these components together? We have the plumbing and the drywall … what else do we need?

One step back, three steps forward

In the late 1970s, when graphical user interfaces (GUIs) were being invented, software architects saw applications as having three major parts: the part that manages data, the part that creates 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 , the presentation system is called the View , and the interaction system is the Controller . Many modern development environments, including Java’s Swing, use this Model/View/Controller (MVC) architecture (see figure 2.1) as the foundation of their own frameworks.

 

Figure 2.1 The Model/View/Controller architecture

Java web developers already have capable tools, such as JDBC and JSP, for consulting

the Model and creating the View, but where’s the Controller for our web applications?

Enter Struts

The centerpiece of Struts is an MVC-style Controller. The Struts Controller bridges the gap between Model and View. The framework also includes other missing 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.

Struts controller components

The Struts controller is a set of programmable 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 how 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 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. 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 architectural overview, let’s go ahead and introduce the major Struts components now.

 

Figure 2.2 Major Struts components

Hyperlinks

To 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 special 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 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:

<forward

name="welcome"

path="/pages/index.jsp"/>

This element would create an ActionForm JavaBean with its name property 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 destination of 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 single page or Java class.

For more about ActionForwards, see chapter 6.

HTML forms

The web protocols, HTTP and HTML, provide a mechanism for submitting data from a form but leave receiving the data as an exercise for the developer. The Struts framework provides an ActionForm class, which is designed to handle 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 matches 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 an HTML form element of the same name, as shown here:

public final class LogonForm extends 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 everyone does not have to sift through an HTTP request.

The ActionForm classes are created using normal Java classes. The Struts configuration refers to the ActionForm classes through a set of descriptors: the <form-beans> and <form-bean> elements. The <form-bean> elements are descriptors that the framework uses to identify and instantiate the ActionForm objects, as shown here:

<form-bean

name="articleForm"

type="org.apache.artimus.struts.Form"/>

The Struts configuration lists the ActionForm beans it uses and gives the Action- Form classes a logical name to use within the application. For more about ActionForms, see chapter 5.

Custom actions

An HTML form uses an action parameter to tell the browser where to send the form’s 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 data it needs directly from the ActionForm bean.   ere’s 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) form;

// ...

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 request and response object. In practice, an Action can do anything a Java Servlet can do. For more about Action objects, see chapter 8.

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 localizing your application from the controller layer.

ActionMappings

In 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 defined in the XML configuration file:

<action-mappings>

<action

path="/logonSubmit"

type="app.LogonAction"

name="logonForm"

scope="request"

validate="true"

input="/pages/logon.jsp"/>

</action-mappings>

This also allows the same Action object to be used by different mappings. For example, one mapping may require validation; another may not.

For more about ActionMappings, see chapter 7.

ActionServlet

The Struts ActionServlet works quietly behind the scenes, binding the other components together. Although it can be subclassed, most Struts 1.0 developers treat the ActionServlet as a blackbox: they configure it and leave it alone. For more about configuring Struts, see chapter 4.

In Struts 1.1, the ActionServlet is easier to extend. Chapter 9 covers the new extension points and configuration options for the Struts 1.1 ActionServlet.

Localization

Web applications also interact with users through prompts and messages. The Struts components have localization features built in so that applications can be written for an international audience. We refer to the localization features throughout the book. A general overview is provided in chapter 13.

Developing a web application with Struts

To build a web application with Struts, developers will define the hyperlinks they need as ActionForwards, the HTML forms they need as ActionForms, and whatever custom server-side actions they need as Action classes.

Developers who need to access EJBs or JDBC databases can do so by way of the Action object. This way, the presentation page does not need to interact with the Model layer.

The Struts Action object will collect whatever data a View may need and then forward it to the presentation page. Struts provides a JSP tag library for use with JSP pages that simplifies writing HTML forms and accessing other data that an Action may forward. Other presentation devices, such as Velocity templates, can also access the Struts framework to create dynamic web pages. This process is shown in figure 2.3.

 

Figure 2.3 Delivering data back to the view

For more about using various data systems with Struts, see chapter 14. See chapters 10 and 11 to learn more about creating presentation pages with Struts.

Before moving deeper into the Struts architecture, let’s take a look at the issues faced by a web application framework that the architecture must address.

分享到:
评论

相关推荐

    struts1和struts2的区别

    struts1和struts2的区别struts1和struts2的区别struts1和struts2的区别struts1和struts2的区别struts1和struts2的区别struts1和struts2的区别struts1和struts2的区别struts1和struts2的区别struts1和struts2的区别...

    Struts2视频教程

    Struts2是一套非常优秀的Java WEB应用框架,实现优雅、功能强大、使用简洁。目前已有大量的企业项目采用Struts2来作为Web框架进行开发,掌握Struts2是每个进行Web开发的Java程序员的一项必备技能。 本视频对Struts2...

    最新版本的Struts2+Spring4+Hibernate4框架整合

    项目原型:Struts2.3.16 + Spring4.1.1 + Hibernate4.3.6 二、 项目目的: 整合使用最新版本的三大框架(即Struts2、Spring4和Hibernate4),搭建项目架构原型。 项目架构原型:Struts2.3.16 + Spring4.1.1 + ...

    struts2-core.jar

    struts2-core-2.0.1.jar, struts2-core-2.0.11.1.jar, struts2-core-2.0.11.2.jar, struts2-core-2.0.11.jar, struts2-core-2.0.12.jar, struts2-core-2.0.14.jar, struts2-core-2.0.5.jar, struts2-core-2.0.6.jar,...

    搭建struts2框架

    struts2框架的详细搭建 &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" ...

    Struts2开发常用jar包

    包含struts2-core-2.5.10.1.jar,struts2-jfreechart-plugin-2.5.10.1.jar,struts2-json-plugin-2.5.10.1.jar,struts2-junit-plugin-2.5.10.1.jar,struts2-bean-validation-plugin-2.5.10.1.jar,struts2-cdi-...

    struts框架jar包

    logging-1.0.4.jar commons-validator-1.3.1.jar jstl-1.0.2.jar oro-2.0.8.jar standard-1.0.2.jar struts-core-1.3.8.jar struts-el-1.3.8.jar struts-extras-1.3.8.jar struts-faces-...

    struts2实例 学生信息管理系统

    struts2实现的学生信息管理系统 &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" ...

    Struts2_s2-016&017&ognl2.6.11_patch漏洞补丁

    struts.xml文件中新增以下内容: &lt;!-- 为修复struts2 s2-016、s2-017漏洞,重写DefaultActionMapper --&gt; &lt;bean type="org.apache.struts2.dispatcher.mapper.ActionMapper" name="myDefaultActionMapper" class=...

    Struts2漏洞检查工具2019版 V2.3.exe

    Struts2漏洞检查工具2019版 警告: 本工具为漏洞自查工具,请勿非法攻击他人网站! ==漏洞编号==============影响版本=========================官方公告==========================================影响范围====...

    Struts2最新漏洞升级2.3.32版本

    1、升级所需要的jar(见附件): freemarker-2.3.22.jar ognl-3.0.19.jar struts2-convention-plugin-2.3.32.jar struts2-core-2.3.32.jar struts2-spring-plugin-2.3.32.jar ...&lt;struts&gt;“加在这里”&lt;/struts&gt;

    struts1.0和struts2

    struts1和struts2的区别其实并不是太大,两者的区别: Action 类: ◆Struts1要求Action类继承一个抽象基类。Struts1的一个普遍问题是使用抽象类编程而不是接口。 ◆Struts 2 Action类可以实现一个Action接口,也...

    struts-config.xml struts标准配置文件 struts-config

    struts-config.xml struts标准配置文件 struts-config

    Struts简介 什么是Struts Struts基本运作流程

    Struts简介 什么是Struts Struts基本运作流程 ActionMapping类 Action类 ActionForm类 ActionError与ActionMessage 协同开发 模块化程序 Struts异常处理 Struts国际化支持 PlugIn接口 等等

    struts2 总结工程大全

    struts2 总结工程大全struts2 总结工程大全struts2 总结工程大全struts2 总结工程大全struts2 总结工程大全struts2 总结工程大全struts2 总结工程大全struts2 总结工程大全struts2 总结工程大全struts2 总结工程大全...

    struts2环境搭建+struts2 新闻发布系统+struts2 留言板

    struts2环境搭建+struts2 新闻发布系统+struts2 留言板 struts2环境搭建:基础框架搭建,简单易上手,适合新手,比你看书强多了,实践源于理论而高于理论,贵在实践 朋友。 struts2 新闻发布系统:struts2+jsp 功能不...

    在Eclipse中配置Struts2项目(html)手把手教会你 +struts2 标签库介绍(html) + STRUTS2学习文档.pdf + Struts2―表单验证validate(html) + struts2和struts的比较 + struts教程(html)

    在Eclipse中配置Struts2项目(html)手把手教会你 如何在Eclipse中配置Struts2。 struts2 标签库介绍(html)对Struts2的...struts2和struts的比较 让你更清楚的知道struts2和struts的不同之处。 struts教程(html)

    论坛系统项目(Struts 2+Hibernate+Spring实现)

    论坛系统项目(Struts 2+Hibernate+Spring实现)论坛系统项目(Struts 2+Hibernate+Spring实现)论坛系统项目(Struts 2+Hibernate+Spring实现)论坛系统项目(Struts 2+Hibernate+Spring实现)论坛系统项目(Struts...

    struts1 和 struts2所需jar包

    struts1 和 struts2所需jar包。主要包含以下内容: struts-1.3.10-all.zip struts-1.3.10-apps.zip struts-1.3.10-lib.zip struts-1.3.10-src.zip struts-2.3.4.1-all.zip struts.rar

    Struts2实战.pdf

    《Struts 2实战》结合实例介绍了Struts 2框架,主要内容包括Action、Result、Interceptor等框架组件,基于注解的配置选项等新特征,Struts 2插件 FreeMarker,如何从Struts 1和WebWork 2迁移到Struts 2,Ajax标签、...

Global site tag (gtag.js) - Google Analytics