Concept, architecture, and working principle of Struts

Source: Internet
Author: User

 Basic Concepts

Struts is an Open Source project of the Jakarta Project Team of the Apache Foundation. It adopts the MVC mode and can help java developers develop Web applications using J2EE. Like other java architectures, Struts is also an object-oriented design that gives full play to the capabilities of the MVC mode "separated display logic and business logic. The core of the Structs framework is an elastic control layer, based on standard technologies such as Java Servlets, JavaBeans, ResourceBundles and XML, and some class libraries of Jakarta Commons. Struts consists of a group of collaborative classes (components), Serlvet, and jsp tag lib. Web applications based on the struts architecture basically comply with the design standards of JSP Model2, and can be said to be a change type of the traditional MVC design pattern.

Struts has its own Controller and integrates other technologies to implement Model and View ). At the model layer, Struts can easily be combined with data access technologies, such as JDBC/EJB and other third-party class libraries, such as Hibernate/iBATIS, or Object Relational Bridge ). In the view layer, Struts can work with JSP, including JSTL and JSF, as well as Velocity templates, XSLT, and other presentation layer technologies.
Struts provides support for every professional Web application to help you create an extended development environment for your application.

  Struts architecture and working principle

MVC, short for Model-View-Controller, is a common design pattern. MVC weakens the coupling between the business logic interface and the data interface, and makes the view layer more changeable. The working principle of MVC is shown in 1:



Figure 1


Struts is an implementation of MVC. It uses Servlet and JSP tags (belonging to the J2EE specification) as part of the implementation. Struts inherits various MVC features and makes corresponding changes and extensions based on J2EE features. The architecture and working principle of Struts are shown in Figure 2:



Figure 2
As shown in figure 2, the architecture of Struts includes three parts: Model, View, and Controller.

Let's take a look at the struts architecture (Model 2) and working principles from the MVC perspective:

1) Model)

In the Struts architecture, the model is divided into two parts: the internal state of the system and the operations that can change the state (transaction logic ). The internal status is usually represented by a group of ActinForm beans. Depending on the design or application complexity, these beans can be self-contained and continuous, or only obtain data (from a database) as needed ). Large applications typically encapsulate transaction logic (Operations) within a method, which can be called by bean with state information. For example, the shopping cart bean has the information of the purchased items, and the checkOut () method may be used to check the user's credit card and send the order information to the warehouse. In small programs, operations may be embedded in the Action class, which is part of the controller role in the struts framework. This method is suitable when the logic is simple. We recommend that you separate the transaction logic (what to do) from the role played by the Action class (what to do.

2) View)

The view is mainly created by JSP. struts contains the extended custom tag Library (TagLib), which simplifies the process of creating a completely internationalized user interface. Currently, tag libraries include Bean Tags, HTML tags, Logic Tags, Nested Tags, and Template Tags.

3) Controller)

In struts, the basic Controller component is the instance servelt in the ActionServlet class. The actually used servlet is defined by a group of mappings (described by the ActionMapping class) in the configuration file. Operations on business logic are mainly completed by the Action, ActionMapping, and ActionForward components. Action acts as the real implementer of business logic, actionMapping and ActionForward specify the operation direction of different business logic or processes. Struts-config.xml file configuration controller.

 

  Components in Struts Architecture


Figure 3

3 shows the simplest relationship between ActionServlet (Controller), ActionForm (Form State), and Action (Model Wrapper.

The components used in the architecture are as follows:

ActionServlet Controller
ActionClass Contains transaction logic
ActionForm Display Module data
ActionMapping Help controllers map requests to operations
ActionForward Object used to indicate the operation Transfer
ActionError Used to store and recycle errors
Struts tag Library This reduces the development and display layers.

   Struts configuration file: struts-config.xml

Struts configuration file struts-config.xml, which we can find by default in the directory \ WEB-INF \ struts-config.xml. The configuration of the file includes four parts: Global Forwarding, ActionMapping class, ActionForm bean, and JDBC data source.

1) Configure Global Forwarding

Global Forwarding is used to create logical name ing between JSP pages. Forwarding can be obtained through the instance mapped to the call operation, for example:
ActionMappingInstace. findForward ("logicalName ");

Example of Global Forwarding:

<Global-forwards>
<Forward name = "bookCreated" path = "/BookView. jsp"/>
</Global-forwards>

Attribute Description
Name Name of Global Forwarding
Path Relative Path to the target URL

2) Configure ActionMapping

ActionMapping objects help to control the internal process of the framework. They can map request URIs to the Action class and associate the Action class with the ActionForm bean. ActionServlet uses these mappings internally and transfers controls to instances of specific Action classes. All Action classes use the perform () method to implement specific application code and return an ActionForward object, including the target resource name for response forwarding. For example:

<Action-mappings>
<Action path = "/createBook" type = "BookAction" name = "bookForm" scope = "request" input = "/CreateBook. jsp">
</Action>
<Forward name = "failure" path = "/CreateBook. jsp"/>
<Forward name = "cancel" path = "/index. jsp"/>
</Action-mappings>

Attribute Description
Path Relative Path of the Action class
Name Name of the Action bean associated with this operation
Type The full name of the Action class connected to this ing (including the package name)
Scope ActionForm bean scope (request or session)
Prefix Prefix used to match Request Parameters and bean attributes
Suffix Suffix used to match Request Parameters and bean attributes
Attribute Scope name.
ClassName The default class of the fully qualified class of the ActionMapping object is org. apache. struts. action. ActionMapping.
Input Path of the input form, pointing to the control that must be returned when an input error occurs in the bean
Unknown If this parameter is set to true, the operation will be used as the default operation for all URIs with no defined ActionMapping.
Validate If this parameter is set to true, ActionServlet will call the validate () method of ActionForm bean to check the input before calling the perform () method on the Action object.

You can use the <forward> element to define the logical name of a resource, which is the target of the Action class response to be forwarded.

Attribute Description
Id ID
ClassName The fully qualified name of the ActionForward class. The default name is org. apache. struts. action. ActionForward.
Name Logical name used by the operation class to access ActionForward
Path Path of the target resource to be forwarded
Redirect If this parameter is set to true, ActionServlet uses the sendRedirect () method to forward resources.

3) Configuration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.