Article 4: Design mode and struts introduction articles

zhaozj2021-02-16  49

Serious warning: People only care about the program, please skip

Prior statement: This article is all introduced, and there is no programming at all. All of this article is not your own work. There is no way to modify, just deleting some content. Why add this content? I can understand what? Waiting for the problem, I don't know, but I have to declare that I don't feel nonsense.

Design mode (Design Patten)

Simply put, Design Patten is a commonly used solution. During our development, you will often encounter some of the same or similar problems. Every time we will find a new solution, in order to save time improvement, we provide some to solve these common problems, proven to be feasible. The solution constitutes a unified repository.

A Design Patten describes a proven program. These programs are very common, and it is the most commonly defined pattern with a complete definition. These modes can be reused, and there is good scalability.

MVC design mode

Code separation, display and logical decoupling

Verification process

Process control

Update the status of the application

The MVC helps solve certain problems encountered by the single module method by dividing the problem into three categories.

Model, View, Controller (Controller)

MODEL (model)

The model contains the core of the application, which encapsulates the application's data structure and transaction logic, which reflects the status of the application. Sometimes it only contains status information because it does not understand the information of the window or controller (it does not know anything at any time).

JavaBean and EJB are ideal for this role because it can handle most of the transaction logic and data structures. It can interact with databases or file systems, and bear responsibility for maintaining application data.

View (view)

The view implements the appearance of the module, which is an external manifestation of the application. It can access the model of the model, but does not understand the model, and it does not understand the situation of the controller. When the model changes, the view will be notified, it can access the data of the model, but these data cannot be changed.

Controller

The controller reacts to the user's input and links the model and view. The servlet can accept the client's HTTP request and create the required JavaBean or EJB as needed, and then notify the window to the window.

MVC Model 2 mode for web applications

Features of the web application:

Customized connection of clients and servers

Since the HTTP itself lacks state information, the client must retrore the server to find changes caused by the input, in which case the controller does not notify the application.

Implementing the techniques used in views and implementation of models or controllers.

You can also use Java code to generate all HTMLs, but this will produce more questions.

For web applications, you need to modify the standard MVC form. The figure below shows the MVC web rewrite version, typically also known as MVC Model 2 or MVC 2.

A implementation of Struts - - - MVC 2

It is the implementation of the open source of MVC Model 2

It is a group of collaborative classes, servlets, and JSP tags that make up a reusable design.

Is a framework, using Struts, I decide the framework of your Application.

The rich tag library and utility classes independent of the framework can be used as used

Is a subproject in the Apache group

Struts overview

CLIENT BROWSER Creates an event from each HTTP request from the client browser. The Web container will respond with an HTTP Request.

Controller

The controller receives a request from the browser and decides where to send this request. For Struts, the controller is a command design pattern implemented by a servlet. Struts-config.xml file configuration controller.

Business logic

Business logic updates the status of the model and helps control the process of the application. For Struts, this is done by the Action class "thin" packaging as the actual business logic.

MODEL (model) status

The model represents the status of the application. Business object updates the status of the application. The ActionForm bean represents the status of the model in the session stage or the request grade, not in the persistence. JSP files read information from Actionform Bean using JSP tags.

View (view)

The view is a JSP file. There is no process logic, no business logic, and there is no model information - only the tag. The tag is one of the factors that make Struts different from other frameworks (such as Velocity).

Components used in the Struts framework

ACTIONSERVLET

Controller

ActionClass

Contains transaction logic

Actionform

Display module data

ActionMapping

Help the controller to map the request to the action

ActionForward

Objects used to indicate operational transfer

Actionerror

Used to store and recycle errors

Struts tag library

Can reduce the development of development display hierarchy

ActionServlet class

The controller component is implemented by org.apache.struts.Action.ActionServlet class, this class is an extension of the javax.servlet.http.httpservlet class, which is the core of this frame.

The controller maps the event (event usually HTTP POST) a servlet. With configuration files, you don't have to hardcode these values.

ActionServlet (Command) creates and uses Action, ActionForm, and ActionForward. When you read the struts-config.xml file, configure the Commuts-Config.xml file. When you create a web project, you need to extend Action and ActionForm to resolve specific issues.

Basic features of Struts Controller

1. Intercept user HTTP request

2. Map this request to the corresponding Action class, if this is the first request received by this class, the initialization instance is slowed down.

3. Create or discover an ActionForm Bean instance (see if the configuration file is defined), and then transplant the request process to bean.

4. Call the perform () method of the Action instance and pass the Actioform bean, Action Mapping object, the request, and the response object to it

5. Perform Returns an actionForWord object, this object is connected to the corresponding JSP page

ActionServlet configuration

We need to declare an actionServlet in web.xml, and configure it to load when starting

In most cases, the standard servlet can meet the needs of users.

Action

org.apache.struts.Action.ActionServlet

config

/web-inf/struts-config.xml

debug

2

detail

2

2

Action class

All Action classes expand the org.apache.struts.Action.Action class and override the Perform () method defined in the class.

The Action class is a packaging of business logic, and the use is to convert HTTPSERVLETREQUEST to business logic.

When the event progresses to this step, the input form data (or HTML form data) has been extracted from the request stream and transferred to the actionform class. The ActionForm class is passed to the Actino class, which is all automated.

Be careful when expanding the Action class. The Action class should control the process of the application without controlling the logic of the application. We can provide greater flexibility and reusability by placing business logic in separate packages or EJBs.

The Action class must program as "thread security" mode, because the controller will share the plurality of simultaneous requests to share the same instance, according to the design of the Action class, you need to pay attention to the following points.

You cannot use instances or static variables to store status information for specific requests, which share global resources across the request in the same operation.

If you want to access resources (such as JavaBean and Session Variables) need to be protected when parallel access, then access to synchronize

Action configuration

We need to configure an action information in struts-config.xml

Example

Type = "org.apache.struts.Webapp.example.cautionsavection"

Name = "CAUTIONSAVEFORM"

Scope = "session"

INPUT = "/ s63.jsp">

Actionform class

Actionform class expands org.apache.struts.Action.Actionform class, the BEAN created by program developers can contain additional properties.

The framework assumes that the user creates an actionform bean for each form in the application to maintain the session state of the web application.

If you use a dynamic ActionForm class, you only need to make the appropriate configuration in Struts-Config.xml, the framework can automatically generate an actionform bean

A typical actionFrom bean only has a method of setting with a read method (Getxxx) without transaction logic. Only simple input check logic, the purpose of use is to store the latest data entered in the relevant form so that the same web page can be regenerated while providing a set of error messages so that the user can modify the incorrect input data. The Struts framework will do the following for ActionForm

Check if the userActionform exists; if there is no existence, it will create an instance of the class.

The status of the ActionForm will be set using the corresponding domain in HTTPSERVLETREQUEST. There is not much annoying request.getParameter () call.

The STRUTS frame will update its status before passing the ActionForm to the service packaging action.

Before passing it to the Action class, Struts also performs form authentication () method for ActionForm, but does not advocate this approach.

ActionForm can be maintained at the session level.

ActionForm configuration

Struts-config.xml file controls the mapping relationship between the HTML form request and the ActionForm.

Multiple requests can be mapped to the ActionForm.

ActionForm can map across multiple pages to perform operations such as the wizard.

Examples are as follows (non-dynamic ActionForm configuration):

Need to develop org.apache.struts.Webapp.Example.logonform.java files.

Only the domain of the private correspondence page form and its setxxx () and getxxxxxx () methods are required in LogonForm.java.

Examples are as follows (Dynamic ActionForm configuration):

If you need to utilize the data check function of the frame, type = "org.apache.struts.validator.dynavalidatorform"

Otherwise Type = "org.apache.struts.action.DynaActionform"

ActionMApping class

Struts-config.xml configuration information is converted to a set of actionMApping, while the latter is placed in the ActionMAppings container.

The ActionMApping object helps flow control inside the framework, which can map a specific request URI to a specific Action class and associate an Action class with an ActionForm bean.

ActionServlet (Command) passes ActionMApping to the Action class via Perform (). This allows the Action to access information for controlling the flow.

Action will use an actionMApping's FindForward () method, this method returns an ActionForward that specifies the name so that Action has completed local forwarding.

ActionError class

ActionError is a Struts to keep an error list, encapsulate a single error message.

The ActionError class is not independently erroneous, which is always stored in the ActionerRors object, and View can use tags to access these classes. As follows:

Struts tag library

The Struts tag library used by JSP view components consists of four types of tags.

Bean tag: Manage Bean in JSP page

Logical tag: Control flow in the JSP page

HTML tag: Used to generate an HTML tag, display data in the form, program the URL using the session ID

Template tag: Use dynamic template to construct a page of normal format

(About their detailed introduction)

转载请注明原文地址:https://www.9cbs.com/read-20954.html

New Post(0)