In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "what is the Struts2 processing flow in Java". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Take a look at the process flow of Struts-2:
1) Browser generates a request and submits the framework for processing: decide which interceptors, action classes, results, and so on to use based on the configuration.
2) the request goes through a series of interceptors, which are processed differently according to the level of the request. This is very similar to Struts-1 's RequestProcessor class.
3) call Action: generate a new action instance and call the business logic method.
4) call produces result: match result class and call to generate instance.
5) the request is returned again through a series of interceptors: the process can also be configured to reduce the number of interceptors
6) return the request to the user: return servlet from control to generate Html.
What is obvious here is that there is no scope encapsulation for FormBean, and data can be obtained directly from Action. Here is a web.xml file for the Strut-2 configuration:
Controller org.apache.struts.action2.dispatcher.FilterDispatcher cotroller / *
Notice that the old servlet has become filter,ActionServlet and FilterDispatcher,*.do has become / *. The filter configuration defines a class with a name (for association) and filter. Filter mapping asks the URI to call the filter if it matches the successful request. By default, the extension is ".action". This is defined by the "struts.action.extension" attribute in the default.properties file.
Default.properties is a property definition file that sets different property values by including a file named "struts.properties" in the project classpath path. The default configuration file for Struts-2 is called struts.xml. Because 1 and 2 have action extensions of .do and .action, respectively, it is convenient to coexist. Let's take another look at an action code for Struts-2:
Public class MyAction {public String execute () throws Exception {/ / do the work return "success";}}
The obvious difference is that you no longer have to inherit any classes and interfaces, only a String is returned with no parameters. Virtually any parameterless method in Struts-2 that returns String can be configured to call action. Where do all the parameters come from? The answer is Inversion of Control technology (inversion of control). The author tries to explain it in the most popular way, let's first try to get the Action to get the reuqest object, so that we can extract any parameters submitted by the page. So we set request as a member variable, and then we need a set method on it. Since most action needs to do this, we implement this set method as an interface.
Public interface ServletRequestAware {public void setServletRequest (HttpServletRequest request);} public class MyAction implements ServletRequestAware {private HttpServletRequest request; public void setServletRequest (HttpServletRequest request) {this.request = request;} public String execute () throws Exception {/ / do the work directly using the request return Action.SUCCESS;}}
So who calls this set method? That is to say, who will control the behavior of this action? in the past, we all wrote an action.setServletRequest (…) in the appropriate place. That is, the control is on the programmer's side. However, the idea of control inversion is that where to call and leave it to the running container to decide, as long as you use the Java reflection mechanism to get the Method object and then call its invoke method to pass in parameters, so control is transferred from the programmer to the container. Programmers can reduce a lot of tedious work and pay more attention to business logic. Request can be injected into action in this way, as can any other object. In order to ensure the thread safety of the member variables of action, the action of Struts-2 is not singleton, and each new request generates a new action instance.
So some people will ask, who is going to do the injection of this object? The answer is the interceptor. What is an interceptor? The author will try to explain the concept of interceptor as popular as possible. If you want to understand interceptors, you must first understand the Proxy pattern in the GOF23 design pattern.
If the An object calls f (), it wants the proxy to do it for B, then B should get the reference of An object, and then call the f () method of An object through the An object reference in B's f (), and finally achieve the purpose that the f () of An is called. Does anyone find this troublesome? why does it have to be encapsulated in the f () method of B that can be done as long as A.F ()? What are the benefits?
1) here we have only one A, and when we have many A, we only need to monitor the f () method of one object B to control all the called f () methods globally.
2) in addition, since agent B can get a reference to An object, B can decide what pre-work can be done before actually calling the f () method of An object, and what post-work can be done before returning.
At this point, do you see the concept of interceptor? It intercepts the request for the next call to the f () method, and then handles it uniformly (each can be handled differently, and the An object can be identified by parsing), and then released after processing. Is this like cutting across the flowing river, searching all the water molecules that want to pass through, and then releasing it? This is the idea of AOP (Aspect of Programming aspect-oriented programming).
Anyway,Struts-2 only uses AOP and IoC technology to reduce the coupling relationship between action and the framework, and tries to reuse action to a certain extent. Driven by this technology, Struts-2 's action becomes a simple POJO (Plain Old Java Object) used by the framework. In fact, the ideas of AOP and IoC have spread all over every new framework, they are not very new technologies, and they all use the things that JDK can do for a long time. They represent an idea of more interface-oriented programming, improving reuse, and increasing expansibility.
This is the end of the content of "what is the Struts2 processing flow in Java". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.