- 浏览: 85289 次
- 性别:
- 来自: 北京
最新评论
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.
相关推荐
Struts2是一款非常流行的Java Web框架,用于构建企业级应用。然而,随着时间的推移,Struts2在安全方面暴露出了一些重要的漏洞,这给使用该框架的系统带来了潜在的安全风险。"Struts2漏洞检查工具Struts2.2019.V2.3...
Struts1.2驱动包是Java Web开发中一个重要的组件,它是Apache Struts框架的特定版本,用于支持基于Model-View-Controller (MVC)设计模式的应用程序开发。Struts1.2因其稳定性和广泛的功能集而在过去备受推崇,尤其在...
### Struts2核心知识点解析 #### 一、Struts2框架概述 - **定义与特点**:Struts2是一款基于MVC(Model-View-Controller)设计模式的Java Web应用程序框架,它继承了Struts1的优点,同时在设计上更加灵活、易用,...
Struts 2是一款基于Java的开源MVC框架,它在Web应用开发中广泛使用,但同时也因其复杂的架构和历史遗留问题,成为了网络安全的焦点。这个标题提到的是一个全面的Struts 2漏洞检测工具,旨在帮助开发者和安全专家识别...
该工具的打开路径为:\Struts2VulsTools-2.3.20190927\Test\bin\Release\Text.exe 2019-09-25: 优化部分EXP在部分情况下被WAF拦截的问题,提高检测成功率,优化自定义上传路径exp,文件所在目录不存在时自动创建...
Struts是一个开源的Java Web应用程序框架,主要用于构建MVC(Model-View-Controller)模式的Web应用。在Java EE世界中,Struts扮演着至关重要的角色,它简化了开发过程,提高了代码的可维护性和可扩展性。SSH框架是...
项目原型:Struts2.3.16 + Spring4.1.1 + Hibernate4.3.6 二、 项目目的: 整合使用最新版本的三大框架(即Struts2、Spring4和Hibernate4),搭建项目架构原型。 项目架构原型:Struts2.3.16 + Spring4.1.1 + ...
Struts2是一个强大的Java EE应用程序框架,主要用于构建企业级的Web应用。它的核心是MVC(Model-View-Controller)设计模式,可以帮助开发者组织代码,提高开发效率,并且提供了丰富的特性来支持表单验证、国际化、...
Struts2 项目开发 Struts2 是一个基于 Java Web 的框架,广泛应用于 Web 应用程序的开发。下面将从 Struts2 项目开发的角度,详细介绍 Struts2 框架的应用、开发流程、技术架构、实践经验等方面的知识点。 项目...
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是一个强大的Java web应用程序开发框架,它基于Model-View-Controller(MVC)设计模式,旨在简化创建用户交互式、数据驱动的web应用的过程。这个“Struts2接口文档”是开发者的重要参考资料,提供了关于...
struts2框架的详细搭建 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" ...
Struts2-showcase是一个用于演示和学习Apache Struts2框架功能的开源项目。这个压缩包“struts2-showcase.rar”包含了完整的源代码,旨在帮助开发者深入理解Struts2框架的工作原理及其各种特性。以下是对Struts2和...
struts2 chm 程序包 org.apache.struts2 接口概要 接口 说明 StrutsStatics Constants used by Struts. 类概要 类 说明 RequestUtils Request handling utility class. ServletActionContext Web-specific ...
Struts 2整合Struts 1,允许开发者利用Struts 1已有的投资,同时享受Struts 2带来的优势,如增强的类型安全和更强大的拦截器机制。 在《Struts 2权威指南--基于WebWork核心的MVC开发》这本书中,作者李纲深入浅出地...
Struts2是一个基于MVC(Model-View-Controller)设计模式的Java Web应用程序框架,它在Web开发领域中被广泛使用,提供了强大的控制层来处理请求、数据绑定、验证和结果展示。Struts2.2.3是Struts2的一个版本,这个...
北京时间8月22日13时,Apache官方发布通告公布了Struts2中一个远程代码执行漏洞(cve-2018-11776)。该漏洞可能在两种情况下被触发,第一,当没有为底层xml配置中定义的结果设置namespace 值,并且其上层动作集配置...
包含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-...
Struts2是一个基于MVC(Model-View-Controller)设计模式的开源Java Web框架,它在Web应用开发中被广泛使用。Struts2的核心是Action类,它负责处理用户请求,与模型进行交互,并将结果返回给视图。在Struts2的版本...
Struts2是一个非常著名的Java Web开发框架,由Apache软件基金会维护。它基于MVC(Model-View-Controller)设计模式,极大地简化了构建基于Java EE的Web应用程序的过程。本资源包含"struts2所有jar包程序文件",是...