Struts. xml summary for beginners

Source: Internet
Author: User

Struts. xml summary for beginners

Struts is a beginner. I have not started yet. I have read a little bit and managed my thoughts. Sorry for the error. An authoritative execution process is attached.

Consider the MVC model,

In struts,

C: FilterDispatcher

V: page

M: action

Recall this process

1. First, after tomcat receives an HttpServletReques, it determines whether to notify struts2 based on web. xml.

Therefore, you must check the web. xml configuration file,

If you want struts to intercept

You need to add a Filter.


If the above Code is added, the url will be intercepted according to the settings

2. After struts2 intercepts the url, it will take the corresponding action according to the struts. xml file. Therefore, it is necessary to understand struts. xml

About struts. xml configuration:

1. First, you have to write an action to let struts2 know how to take the action, so there is a tag.

2. You want to do something before or after the action, such as checking whether you are logged on to the customer or recording the action.

3. After the action and interceptor operations are complete, we have to return data. Therefore Born

Like a controller, struts determines the action taken by the system and the page returned to the user.

3. Understand the labels in struts.


To better manage actions, actions with the same name can exist in different packages.


Name, comparison

Namespace: Optional. The default value is "" (Null String). It can be associated with a url. For example, for Request/test/*. action, you can set namespace to "/test"

Exteds: (optional) inherited package. You can write extends = "struts-default"

Abstract: (optional) indicates that the package must be inherited.

2. After the package is planned, various labels can be put in it, such

Action determines the action to take.


*. Action requested by name and url is the same name,

Class the class to be called by this action

Method: execute the action defined by class. The default value is execute ()

3. Some information must be verified before or after the action is executed, or some subsequent processing is required. The Interceptor is used.



4. After the action is completed, how can I notify struts2 of the changes?


That is the result function.



This process is basically like this. For Beginners, the Process summary can only be understood by themselves to facilitate memory.


Authoritative execution process:


The process of a request in the Struts2 framework is roughly divided into the following steps:

1. The client sends a request;
2. This request goes through a series of filters (these filters have an optional Filter called ActionContextCleanUp, which is very helpful for integrating Struts2 with other frameworks, such as sitemeshzugin)
3. FilterDispatcher is called. FilterDispatcher asks ActionMapper to determine whether to call an Action. FilterDispatcher provides the following functions:

(1) execute Actions
(2) Clear ActionContext
(3) maintain static content
(4) Clear the interceptors of XWork within the request Lifecycle

4. If the ActionMapper decides to call an Action, FilterDispatcher will hand over the request processing to the ActionProxy.
5. ActionProxy queries the framework configuration file through ConfigurationManager and finds the Action class to be called.
6. ActionProxy creates an ActionInvocation instance.
7. The ActionInvocation instance uses the naming mode for calling. Before and after the Action is called, the call of the relevant Interceptor (Intercepter) is involved.
8. Once the Action is executed, ActionInvocation is responsible for finding the corresponding return result based on the configuration in struts. xml. The returned result is usually (but not always, or another Action chain) a template of JSP or FreeMarker to be represented. You can use the tag inherited from the Struts2 framework in the Process of representation. ActionMapper needs to be involved in this process

Interceptor and filter:

1. the interceptor is based on the java reflection mechanism, and the filter is based on function callback.

2. The filter depends on the servlet container, and the interceptor does not rely on the servlet container.
3. the interceptor can only work on Action requests, while the filter can work on almost all requests.
4. the interceptor can access objects in the Action context and value stack, but the filter cannot.
5. In the lifecycle of an Action, the interceptor can be called multiple times, and the filter can only be called once during container initialization.


All objects (actions, Results, Interceptors, etc.) in the above process are created through ObjectFactory.

Struts2's goal is simple-making Web development easier. To achieve this goal, Struts2 provides many new features, such as smart default settings, annotation usage, and application of the "Convention over configuration" principle, all of this greatly reduces the XML configuration. Actions in Struts2 are all POJO, which enhances the testability of actions and reduces the coupling degree within the framework, the input items in the HTML form are converted into appropriate types for action. Developers can also pre-process and post-process requests through the Interceptor (which can be customized or the interceptor provided by Struts2). As a result, the process of requests becomes more modular, this further reduces coupling. Modularization is a general topic-the framework can be expanded through plug-in mechanism; developers can replace the key classes of the framework with custom implementations, in this way, functions not available in the framework can be obtained. tags can be used to render multiple themes (including custom themes). After an Action is executed, there can be multiple result types-including rendering JSP pages, Velocity and Freemarker templates, but not limited to these.

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: 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.