In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces the example analysis of the working principle of Struts2, which is introduced in great detail and has certain reference value. Friends who are interested must finish it!
I. working principle
The processing in the Struts2 framework is roughly divided into the following steps
1 the client initiates a request to the Servlet container (such as Tomcat)
2 the request passes through a series of filters (Filter) (among these filters is an optional filter called ActionContextCleanUp, which is helpful for the integration of Struts2 and other frameworks, such as SiteMesh Plugin).
3 then FilterDispatcher is called, and FilterDispatcher asks ActionMapper to decide whether this request needs to call some Action.
4 if ActionMapper decides that some Action,FilterDispatcher needs to be called, the processing of the request will be handed over to ActionProxy
5 ActionProxy queries the configuration file of the framework through Configuration Manager to find the Action class that needs to be called
6 ActionProxy creates an instance of ActionInvocation.
7 the ActionInvocation instance is called using a naming pattern, which involves the call of the relevant interceptor (Intercepter) before and after the process of calling Action.
8 once the execution of Action is completed, ActionInvocation is responsible for finding the corresponding return result according to the configuration in struts.xml. The return result is usually (but not always, can also be another Action chain) a template of JSP or FreeMarker that needs to be represented. Tags inherited in the Struts2 framework can be used during presentation. ActionMapper needs to be involved in this process
Second work flow
1. The client browser issues a HTTP request.
2. According to the web.xml configuration, the request is received by FilterDispatcher
3. According to the struts.xml configuration, find the Action classes and methods that need to be called, and inject the values into Aciton by IoC.
4. Action calls the business logic component to process the business logic, which includes form validation.
5. After the execution of Action, find the corresponding returned result result according to the configuration in struts.xml, and jump to the corresponding page.
6. Return HTTP response to client browser
III. The difference between interceptors and filters
1. Interceptor is based on java reflection mechanism, while filter is based on function callback.
2. Filters rely on servlet containers, while interceptors do not rely on servlet containers.
3. Interceptors can only work on Action requests, while filters can work on almost all requests.
4. The interceptor can access the objects in the Action context and value stack, but the filter cannot.
5. In the life cycle of Action, interceptors can be called multiple times, while filters can only be called once when the container is initialized.
4. What should use Struts2
Struts2 is a fairly powerful Java Web open source framework, a POJO-based Action MVC Web framework. It is based on the current Webwork and XWork framework, inherits its advantages and makes considerable improvements at the same time.
1. Struts2 is based on MVC architecture, the framework structure is clear, the development process is clear at a glance, developers can control the development process very well.
2. Use OGNL to pass parameters.
OGNL provides a simple way to access data in a variety of scopes in Struts2, and you can easily access data in Request,Attribute,Application,Session,Parameters. It greatly simplifies the amount of code for developers to obtain this data.
3. Powerful interceptor
Struts2 interceptor is an Action-level AOP,Struts2. Many features in the interceptor are implemented through interceptors, such as exception handling, file upload, validation, etc. Interceptor is configurable and reusable, you can put some common functions such as login verification, permission verification and so on in the interceptor to complete some common functions in Java Web projects. In a Web project I implemented, the interceptor of Struts2 is used to complete the permission verification function in the system.
4. Easy to test
The Action of Struts2 is a simple POJO, so it is convenient to write test cases for Struts2 Action, which greatly facilitates the testing of 5Java Web projects.
Easy to extend plug-in mechanism to add extensions in Struts2 is a pleasant and easy thing, just put the required Jar package into the WEB-INF/lib folder, and make some simple settings in struts.xml to achieve the extension.
6. Modular management
Struts2 has turned the module into the basic idea of the architecture, and the application can be modularized in three ways: splitting the configuration information into multiple files, creating self-contained application modules as plug-ins to create new framework features, and organizing new functions that have nothing to do with specific applications into plug-ins to add to multiple applications.
7. Global results and declarative exceptions
Add a global Result to the application and handle exceptions in the configuration file so that when a specified exception occurs during handling, you can jump to a specific page.
His so many advantages are favored by many people to combine with spring and Hibernate to form the popular ssh framework. Of course, each company wants its own framework, which is also the product of ssh variation.
See the netizens' summary of the principle of Struts2, and I also summarize it myself for the follow-up interview. Here are my questions.
1. The customer server initiates a request and points it to the Tomcat container through the HTTP protocol. What did tomcat do when she got the request?
2. Our web.xml configuration
Struts2org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterstruts2/*
We can see org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter in the web configuration file. What is the use of this Filter class?
This / * is to intercept all requests. What does he do when he intercepts requests? / *
3. Our struts.xml configuration
/ Hello.jsp
Who is responsible for finding the Action classes and methods that need to be called based on the struts.xml configuration?
4. Finally, find the corresponding returned result result according to the configuration in struts.xml, and then return the HTTP response to the client browser.
How struts2 works
The client sends a request
After a series of filters
FilterDispatcher uses ActionMapper to determine which Action the REquest needs to call.
FilterDispather to ActionProxy.
Ask struts.xml to find the corresponding Action through ConfigurationManager
ActionProxy creates an instance of ActionInvocation
After calling Action Action, ActionInvocation is responsible for finding the corresponding return result according to the configuration in struts.xml.
Struts interceptor, commonly used interceptor
The interceptor of Struts2 is its core part, which provides a mechanism to define a special module to encapsulate and process requests, which can be performed before and after Action execution.
ModelDriven (model driven) servletConfig (get ServletApo) params (dynamic parameter injection) validation (input verification declarative verification)
Advantages of struts2
The clear hierarchical structure of mvc schema is realized.
Rich tag library to improve the efficiency of development
Provides a rich implementation of interceptor
Facilitate exception handling
The relationship between the various parts of the system can be grasped through the configuration file.
Struts2 core controller
FilterDispatcher
It is responsible for intercepting all user requests, and the Filter filters user requests when they arrive. If the user request ends with action, the request will be transferred to the Struts 2 framework for processing.
Struts2 profile override order
Struts.xml-> struts.properties-> web.xml (specify parameters when configuring the filter. Programmers can write)
The way struts2 modifies constants
Struts.xml:
Struts.properties: struts.action.extension=do
Action
Action is multi-instance, so Scope needs to set parameters in Spring.
Wildcard dynamic invocation
Three ways of Action implementation
Simple POJO
Implement the Action interface (formatted return parameters are provided)
Inherit ActionSupport (implements the Action interface and does some internationalization and verification functions)
Visit ServletAPI
ServletActionContext
Implement the interface ServletRequestAware,ServletResponseAware, and the ServletContextAware interceptor is responsible for ServletConfig
Result view
Chain (forward to action, need to set name if not in the same package) dispatcher (default) redierct (request redirect) stream (file upload and download)
What is the purpose of the default struts-default
The struts-default package is built-in by struts, which defines many interceptors and Result types within struts2, and many of the core functions of Struts2 are implemented through these built-in interceptors. When the package inherits the struts-default package, it can use the functions that struts2 provides for us.
Usually every package should inherit the struts-default package.
How struts2 validates a specified method
The validation function is handled by the validation interceptor. Echo error messages are handled by the workflow interceptor.
After verification fails, the error message echo needs to be set up / regist.jsp result view
Validate all methods in the class, override the validate () method, and addFieldError when an error occurs ("name", "Please enter a user name")
For the specified method: rewrite the validate () method to public void validateDemo1 followed by the name of the specified method (verify the Demo1 method)
Value stack
The life cycle of ValueStack is the life cycle of Action, saved in the request domain
Each action has a corresponding value stack, and the action object is stored at the top of the stack by default.
Using OGNL to access the contents of the value stack does not need to add #, but to access other domains need to add #
What is the difference between ActionContext, ServletContext and pageContext?
ActionContext is the current context of Action. Through ActionContext, you can get references to objects related to Action, such as request, session, ServletContext, etc.
ServletContext is a domain object. There is only one ServletContext in a web application, and the life cycle is accompanied by the whole web application.
PageContext is the most important built-in object in JSP. You can get the application of other domain objects through pageContext. At the same time, it is a domain object, and its scope is only for the current page.
Interceptor
Each interceptor is a java class that implements the Interceptor interface
Init () is called when the interceptor is initialized
Intercept () is called when the interceptor works
Destroy () is called when the interceptor is destroyed
Struts2 has 18 interceptors by default
Custom interceptor
Write a class that inherits MethodFilterInterceptor
Declare interceptor in struts2.xml
The interceptor is configured in struts2.xml. The default interceptor will fail when you specify a custom interceptor, so it will be displayed again.
File upload
File field form form enctype=multipart/form-data submission method is post
Action accepts the file object as the name in the form, and the file type is name+ContentType. The file name is: name of the jsp page file component + FileName.
You can use an array to receive uploads of multiple files
The above is all the contents of the article "example Analysis of how Struts2 works". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.