An in-depth study of Struts2 (i)---what is Struts2? What is its working principle?

Source: Internet
Author: User

This article is absolutely original, welcome to reprint, but reprint please remember to indicate the origin of the article: http://blog.csdn.net/izard999/article/details/39891281


have been working on Android for nearly 4 years, recently changed to a unit to do technical director, get outsourced code found is Struts2 do, 5 years ago on the Struts2 or have some insights, but because years do not, leading to or some small forgotten, the previous record of some notes and experience, Add some new things from the official document and see some version updates, and decided to go into it again. I also hope that this series of articles can bring substantial help to the friends you use.

Okay, here we are.

Why to study in depth, why to learn the principle? Maito, but most people still do not want to sharpen the knife, but first the mountain firewood, first over the addiction, and so cut down a few down, and then down the hill to sharpen, this will be very delayed. Why I will first learn the underlying principles of struts2, rather than directly to learn how to use STRUTS2 to complete a function it? surface looks, directly learn how to complete the function with STRUTS2, see the effect is very cool things! But why do you do this? Why does the program code write this way? ? And once the Struts2 itself has bugs, how to solve it (5 years ago I play 2.1.6 when the bug is a heap)? The result is self-evident, confusedly knows nothing! I am a person who likes to ask why, so I have to understand why, in order to extrapolate, independent of the program, I don't panic if I make a mistake. If you wait until confused, know to learn the underlying principle, then to learn the underlying principle, it is not the same as cutting the knife after cutting the same, more delay time? Why many people turn over a lot of books and learn for months Struts2 haven't learned? Is the study of the wrong order caused!

What is STRUTS2?

Struts2 is a web-layer framework designed according to the MVC pattern, in fact, Struts2 is a large filter, we can in the Web.xml file in the description of a feature of all requests to the filter processing, this filter is referred to a configuration file ( Usually/web-inf/classes/struts.xml each request to a different action to handle!


Struts2 's core Filterdispatcher, starting with version 2.1.3, was replaced by Strutsprepareandexecutefilter. Why did the official upgrade?

First look at the official Struts2 working schematic diagram:


I still have some opinions about struts, this is my Struts2.3.16.3 version of the map, and I saw the same 5 years ago, I would like to see your filter version of the change, this map should also change it?

Struts2 working principle (based on the understanding of the above picture): When a httpservletrequest request comes, go through the Actioncontextcleanup filter, clear the action environment, and then pass the other filters, For example, Sitemesh and user-defined filters, and so on, finally reach Struts2 's core filter strutsprepareandexecutefilter, at this point, Strutsprepareandexecutefilter will look for actionmapping to see if this action path exists, and if so, the agent that generates the action in reference to the Struts.xml file, and executes the action, STRUTS2 Many functions are done by interceptors, so this action agent actually calls the action to process the request of the method will go through the Interceptor 1,2,3, before the actual call action method, the action method ends will return a view, The view will also refer to the actionmapping and refer to the template (Jsp,freemarker), at which point the interceptor will intercept again (stating that the STRUTS2 interceptor will intercept when entering the action and returning the result, which is better than filter!), Finally produce httpservletresponse, the corresponding result!

We also see that the old version above, all the other filters we need to put in before the Struts2 filter.  But now I have a need: I want to do something in my filter that requires a STRUTS2 environment (Actioncontext), but it needs to be done before Struts2 's action executes. At this time, the old version of the filter can only be two eyes looking at the sky.

For example, like a patient to do surgery, surgery needs to be an anesthetic (custom filter) and surgery (STRUTS2 filter), how do you want to play anesthesia? Don't let the patient stand.   So first have to prepare a bed (Actioncontext), after the anesthetic, reoperation. That's what strutsprepareandexecutefilter really means. Can be divided into strutspreparefilter,strutsexecutefilter, if there is no such demand as before, we generally use strutsprepareandexecutefilter on it.


Later, please pay attention to the next article: Strutsprepareandexecutefilter Source code interpretation

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.