Struts2 and struts1

Source: Internet
Author: User
1. Basic Process of struts 2

The struts 2 framework consists of three parts: the core controller filterdispatcher, the Business controller, and the user-implemented business logic component. In these three parts, the struts 2 Framework provides the core controller filterdispatcher, and you need to implement the business controller and business logic components. The struts 2 framework can be roughly divided into three parts: core controller filterdispatcher, business controller action, and enterprise business logic component implemented by users. The core controller filterdispatcher is the basis of the struts 2 Framework and includes the internal control process and processing mechanism. The business controller action and business logic components must be implemented by the user. When developing action and business logic components, you also need to write related configuration files for use by the core controller filterdispatcher. The workflow of struts 2 is simpler than that of struts 1, which is basically the same as that of the webwork framework. Therefore, Struts 2 is an upgraded version of webwork. The basic process is as follows:

1. The client browser sends an HTTP request. 2. According to the Web. xml configuration, this request is received by filterdispatcher. 3. According to struts. xml configuration, find the action class and method to be called, and inject the value to aciton through IOC. 4. Action calls the business logic component to process the business logic. This step contains form verification. 5. After the action is executed, find the corresponding returned result based on the configuration in struts. xml and go to the corresponding page. 6. Return an HTTP Response to the client browser.

 

2. core controller: filterdispatcher

Filterdispatcher is the core controller of the struts 2 framework. This controller runs as a filter in a web application. It intercepts all user requests. When a user request arrives, the filter filters user requests. If a user request ends with an action, the request is transferred to the struts 2 Framework for processing.

 

Struts 2 Framework obtained *. after the action is requested *. the previous part of the action request determines which business logic component to call, for example, for login. action request. Struts 2 calls the action named login to process the request.

 

The actions in struts 2 are defined in struts. in the XML file, when an action is defined in the file, the name and class attributes of the action are defined. The name attribute determines which user request the action processes, the class attribute determines the implementation class of this action.

 

Struts 2 is an action instance used to process user requests. It is not a user-implemented business controller, but an action proxy-because the user-implemented business controller is not coupled with the servlet API, obviously, user requests cannot be processed.

The struts 2 Framework provides a series of interceptors that parse the Request Parameters in the httpservletrequest request, pass them into the action, and call back the action's execute method to process user requests.

 

Obviously, the above processing process is a typical AOP (Aspect-Oriented Programming) processing method. Figure 3.19 shows the processing model.

 

 

 

The user-implemented action class is only the proxy target of the Action proxy of struts 2. The user-implemented business controller (Action) includes the processing of user requests. The user's request data is contained in the httpservletrequest object, and the user's action class does not need to access the httpservletrequest object. The interceptor parses the request data in httpservletrequest and sends it to the action instance of the business logic component.

3. Business Controller

The business Controller component is an instance of the User-implemented action class. The action class usually contains an execute method, which returns a string-the string is a logical view name, after the service controller processes user requests, the execute method returns different strings based on different processing results-each string corresponds to a view name.

 

After developing the business controller required by the system, the programmer also needs to configure the action of struts 2, which is defined in the following three parts:

 

-URL of the action.

 

-Implementation class corresponding to the action component.

 

-Mappings between logical views and physical resources contained in actions.

 

Each action must process a user request, and the user request always contains a specified URL. After the filter dispatcher intercepts the user request, it processes the Forwarding Based on the correspondence between the request URL and the Action URL.

4. Struts 2 Model Components

In fact, the model component is out of the scope of the MVC framework. The struts2 framework usually does not provide much help for the implementation of model components.

 

Text Box: Figure 3.20 model component in the model component Java EE application called by the Controller, usually the business logic component of the system. The business logic components hidden in the system may also contain components such as Dao and domain objects.

 

Generally, the business controller in the MVC Framework calls the model component method to process user requests. That is to say, the business logic controller does not actually process user requests, and the model component is responsible for processing user requests. The business controller is only the scheduler responsible for scheduling, which is also called action as the controller.

 

 

 

Figure 3.20 shows the processing process.

 

The prompt is displayed in Figure 3.20. Action is used to call the business logic component. When the controller needs to obtain the business logic component instance, it usually does not directly obtain the business logic component instance, but obtains the business logic component instance through the factory mode; you can also use other IOC containers (such as spring containers) to manage instances of business logic components.

5. View components of struts 2

Struts 2 has changed the current situation that struts 1 can only use JSP as the view technology. Struts 2 allows other template technologies such as freemarker and velocity as the view technology.

 

When the controller of struts 2 returns the logical view name, the logical view is not associated with any view technology, but only returns a string that serves as the logical view name.

 

When we are in struts. when configuring an action in an XML file, you must not only specify the name and class attributes of the action, but also specify a series of result sub-elements for the action element, each result sub-element defines a ing between a logical view and a physical view. The preceding applications use JSP technology as the view. Therefore, the type attribute is not specified when the result sub-element is configured. jsp is used as the view resource by default.

 

If you need to use other view technologies in struts 2, you can specify the corresponding type attribute When configuring the result sub-element. For example, if you want to use freemarker, set the result value to the type attribute of freemarker. If you want to use the velocity template technology as a view resource, set the result value to the type attribute of velocity ......

6. Struts 2 running process

After the above introduction, we found that the running process of the struts2 framework is very similar to that of the webwork framework.

 

Tip: on the official site of struts 2, we can find the following statement: essential, Struts 2.0 is the technical equivalent of webwork 2.3. Aside fromthe package and property renaming, It isn' t much different than, say, migratingfrom webwork 2.1 to 2.2 -- meaning: the struts 2.0 technology is equivalent to the webwork 2.3 framework, in addition to the package and attribute names. Migrating from webwork 2.2 to struts 2 is no more complex than migrating from webwork 2.1 to webwork 2.2.

 

Here we can see that struts 2 is actually an upgraded version of webwork 2.2, which is not hard to understand: Why is webwork so similar to struts 2!

 

Struts struts is just an MVC framework for rapid development of Java Web applications. Struts focuses on C (Controller), including actionservlet/requestprocessor and our custom action. It also provides V (View) with a series of custom tags ). However, Struts involves almost no M (model), so struts can adopt any form of commercial logic implemented in Java. Spring spring is a light-weight container. Its core is beanfactory, which is used to construct the M (model) we need ). On this basis,

Spring provides the implementation of AOP (Aspect-orientedprogramming, hierarchical programming) and uses it to provide services such as transactions and security in non-management environments; extended applicationcontext of Bean factory makes it easier for us to implement J2EE applications; Dao/ORM makes it easier for us to develop databases; web MVC and spring web provide Java Web application frameworks or integrate with other popular web frameworks. That is to say, the two can be used together to complement their own characteristics. Hibernate is a free open-source Java package that makes it compatible with relational databases.

It is very easy to deal with, just as your database contains common Java objects used every day, without having to consider how to extract them from a mysterious database table (or put them back into a database table ). It frees you from allowing you to focus on the objects and functions of your applications without worrying about how to save them or how to find them later. A Technical Specification of EJB: in terms of technology, EJB is not a "product". EJB is a standard that describes the solutions to build application components: Scalable Distributed (distributed) transaction Processing (transactional) Data Storage (persistent) Security (secure) Ant -. ant Introduction

Ant is a make-like tool used to compile, run, and test Java programs. In the process of building, packaging, and publishing, almost everything can be handled by ANT tasks. II. to install and configure ant, you can download the pre-compiled ant from the jakata website and decompress it to any directory. Set your own environment variable, ant_home, to the decompressed directory, and set the java_home environment variable, point to the JDK root directory. 3. When ant is used to manage the project, it automatically loads a file called build. XML project configuration file. If you want to give the configuration file another name, you can use the buildfile tag to run ant, as shown below: ant-buildfile project_configuration.xml main features: use XML file as the configuration file, that is, build. XML, which can be seamlessly integrated with JUnit for system testing, compilation, and publishing. Powerful and detailed report functions, flexible control, and custom task combinations.

 

6. Implement MVC in struts2

Struts uses JSP as the MVC view, and the action class specified by actionservlet is used as the controller, that is, C in MVC. It is responsible for the interaction between views and models. Each entry point of the controller is set by a configuration file named struts-config.xml. This file maps requests from the view to a specific Java class for corresponding processing, and the Controller also specifies the location of the next view. The model in struts mainly refers to JavaBean, which represents the model and encapsulates data and business logic.

7. struts1 process:

The Controller initializes the configuration file and initializes the corresponding moduleconfig object for different struts modules. The Controller receives the HTTP request and finds the action subclass corresponding to the request from actionconfig. If there is no corresponding action, the Controller directly forwards the request to the JSP or static page, otherwise, the Controller distributes requests to specific action classes for processing.

Before the Controller calls the execute method of a specific action, the actionform object fills itself with parameters in the HTTP request. You can also call the validate method in the actionform class to check the validity of the request parameters, and return an actionerrors object containing all error information.

Execute the specific execute method, which is responsible for executing the corresponding business logic. After execution, an actionforward object is returned, and the Controller uses this actionforward object for forwarding. You can also encapsulate the business logic to be processed by the action in the JavaBean. If there is an EJB in the system, you can call the EJB through the JavaBean interface to complete business processing. If no EJB exists, then you can directly connect to the database in JavaBean for database-related operations.

 

Struts2 and struts1

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.