Struts 2 configuration XML action details

When learning struts, we must master the working principle of struts 2. Only when we specify the implementation process of the internal architecture of the struts 2 framework and configure the whole struts framework can we make a good logical configuration. Next, I will briefly explain the working principle of struts 2 Framework:

1、 How struts 2 works

1、 The client initializes a request to a servlet container (such as Tomcat); 2. The request passes through a series of filters (among these filters, there is an optional filter called actioncontextcleanup, which is very helpful for the integration of struts 2 and other frameworks, such as SiteMesh plugin); 3. Then the filterdispatcher is called, and the filterdispatcher asks actionmapper to decide whether an action needs to be called for the request; 4. If actionmapper decides to call an a Action, the filterdispatcher hands over the processing of the request to the actionproxy; 5. Actionproxy queries the configuration file of the framework through the configuration manager to find the action class to be called; 6. Actionproxy creates an instance of actioninvocation. 7. The actioninvocation instance is called using the naming pattern. Before and after calling the action, It involves the call of relevant interceptors. 8. Once the action is executed, actioninvocation is responsible for finding the corresponding return result according to the configuration in struts.xml. The return result is usually (but not always, maybe another action chain) a JSP or FreeMarker template that needs to be represented. In the process of representation, you can use the tags inherited from the struts 2 framework. Actionmapper needs to be involved in this process.

2、 General steps for deploying a struts 2 project

1) Create a web project in MyEclipse and import the jar package required by struts 2 into the project, as shown in the following figure

2) Web. In project XML to configure the framework of struts 2, in web XML needs to be included in < Filter > < / Filter >, and < filter name > and < filter mapin can be included under this node

3) Write the action class 4) create the struts to configure the action in the action class XML. Note that the location of the configuration file should be in the root directory of SRC. 5) create a JSP file for testing

3、 Struts Configuration of XML

Let's take a look at the basic configuration of struts

As can be seen from the above code, the data contained in XML is under < struts > < / struts > under the root node. When writing in the actual project, the header information needs to be changed, so we can get it in the jar package in the imported jar

In struts-core-2.2 1.1. Struts 2.0 under jar package 0.dtd.

4、 Struts Configuration of package in XML

Looking at the previous code, you can clearly see the package properties

The attributes are:

1. Name: logical package name 2 Namespace: namespace, used to distinguish the same actions under struts (note that it is a logical path) 3. Extensions: inherit a configuration file 4. Abstract

In the specific use process, we should pay attention to the naming of the namespace. We should add the path of the corresponding namespace in the corresponding process, for example:

This code indicates that the action corresponding to login in the DB namespace will be called

It should also be noted that:

Under the < result > tab is the physical path (absolute path), which refers to the actual location in the project. The code is as follows

6、 Struts Configuration of result in XML

For the configuration of action, I'm just a brief summary. I'll write a blog specifically to explain the dynamic configuration of action

The attributes are:

1. Name: mapping name of action class 2 Class: full path of action class 3 Method: the execute () method is used by default. If you want to customize the trigger method, you need to use method customization

7、 Struts Notes during XML configuration

In the actual development project, we may need to configure many actions. Sometimes, in order to facilitate our classification, we generally need to create struts user under different packages XML file, but in the end, all our configurations will be imported into our struts XML file. Tags used and JSP programming < include >

struts. xml

struts-user. xml

1、 Creation method of action

1) Implement the action interface 2) inherit the actionsupport class and override its methods 3) it can be implemented without inheriting any class. The key is in struts XML

2、 Call of action dynamic method

In an actual project, there may be multiple methods in an action class, and the execution effects of different methods are different. If the general configuration is followed, it will virtually increase the burden on programmers

The first method: in struts The method attribute is used to specify the method name in the action attribute tag in XML (not recommended)

The second method: specify the method you want to call in the JSP page

Dynamic configuration in client request: mapping name! Method name action

In this way, you can dynamically call the myfun method in the action without configuring the method in the action tag

The third method: use wildcards for configuration (recommended: applicable when there are many response methods in a class)

When configuring < action >, wildcards can be used in name, class and method, which is another form of dynamic method call

Note: using wildcards is equivalent to a placeholder operation, where {1} represents the first wildcard. For the above example, when the JSP page is loginuser When an action is called, it is actually the login () method in the starting action class

Through wildcard implementation, different classes respond to methods in different classes:

3、 Configuration of dynamic results of action

Meaning: during the actual operation, the action class may jump to different pages due to complex business conditions. In order to save the result configuration, we generally use the dynamic result configuration. In fact, it is very similar to our global forward configuration in servlet.

UserAction. Java

Struts2. xml

4、 Receive user data (recommended interface to implement modelDriven)

In this blog, I just introduce the common and efficient methods in practical projects. The reason why I recommend the modelDriven interface method is that it can well separate the display interface from the business logic (decoupling).

Implement modelDriven interface

Step: u implement user login function u create user Java class Ø declare user login information Ø create parameterless structure u create action class Ø implement com opensymphony. xwork2. ModelDriven interface Ø declares user class object and instantiates Ø to implement getmodel () method, returns user class object u, creates JSP page Ø form element, and sets name attribute with "attribute"

Note: 1) first of all, the action should implement the modelDriven interface. By default, it implements the getmodel () method. 2) it should instantiate the user object in the action itself, unlike the previous method implemented by the struts 2 framework. 3) what is more prominent is that in the JSP page, the name attribute of the table element can be used directly by name

UserAction. java

login. jsp

5、 Access servlet API in action

There are two ways to access the servlet API in a real project that accesses the servlet API

1) Decoupling method (realize three interface programs: requestaware, applicationaware and sessionaware)

Note: in the non decoupling way, the struts 2 framework has encapsulated the corresponding four application scopes into the map set, so the three map sets defined at the beginning can be equivalent to operating the four scope objects

2) Non decoupling method (implementing two interface programs, servletreuqestaware and servletcontextaware): non decoupling means directly operating servlet API objects

Note: in the implementation interface, only the acquisition of request and application is implemented

In the actual project development, the required method to obtain the session is through httpsession session = request getsession();

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>