Struts2 architecture and Basic Process

Source: Internet
Author: User

Struts2 architecture and Basic Process

1. Basic Struts2 Process

Generally, the Struts2 framework consists of three parts: core controller FilterDispatcher, business controller, and user-implemented business logic component. In these three parts, the Struts2 framework provides the core controller,

The user needs to implement the business controller and business logic components.

1. FilterDispatcher)

FilterDispatcher is the core controller of the Struts2 framework. This controller runs as a Filter in the web framework and is responsible for intercepting all user requests. When user requests arrive, the Filter will

Dynamically filter user requests. If a user request ends with an action, the request is transferred to the struts2 framework for processing.

When Struts2 obtains *. after an action request *. the * part of the action determines which business logic component to call, such as user. action request, Struts2 will call the business logic of Action as user

To process this request, the Struts2 Action is defined in the struts. xml file.

2. Business Controller

The business controller is actually an instance of the User-implemented Action class. In this class, it inherits the ActionSupport class and generally needs to override its execute method. This method returns a string based on

Different strings are returned, and each string corresponds to a corresponding return view.

After developing the Action class, we need to configure this Action in the struts. xml file, which needs to be defined in the following three parts:

(1) configure the Action name value, which corresponds to the Url requested by the client

(2) configure the implementation class of the Action component, that is, the class attribute.

(3) configure the relationship between the logical view and physical resources contained in the Action. That is, the property of the result tag.

3. Struts2 model component

The business controller of Struts2 does not process users' requests. Generally, users' requests are processed by model components. The business controller is only responsible for intermediate scheduling.

For example, in normal projects, the action layer calls the service layer method to process user requests. The Dao layer may also be hidden under the srrvice layer.

4. Basic Struts2 Process

The general execution process of Struts2 is as follows:

(1) The user initiates a request (. action indicates an Action request)

(2) FilterDispatcher of Struts2 intercepts a request initiated by the user and determines whether the request is an Action request. If the request is an Action request, it is sent to the corresponding Action for processing.

An auxiliary object is required during the process: ActionMapper)

(3) After being handed over to the corresponding Action for processing, according to the configuration information of this Action in struts. xml, the system first intercepts all the interceptors of this Action and then executes the requested Action object.

(4) After the Action is executed, a result is returned (represented by a string). The core controller finds the true path from the configuration file based on the returned result, then forward the request to the corresponding

View to the client.

Ii. basic configuration of Struts2

Configure three files: web. xml struts. xml struts. properties

1. web. xml

Any MVC framework needs to be integrated with web applications. Therefore, you have to use the web. xml file. Only the Servlet configured in web. xml will be loaded by the application.

Generally, all MVC frameworks need to load a core controller in web. xml. For the Struts2 framework, FilterDispatcher needs to be loaded, and web applications only need to load

FilterDispatcher and FilterDispatcher automatically load the Struts2 framework.

The code snippet for configuring FilterDispatcher in web. xml is as follows:

 
 
  
  
   
Struts2
  
  
  
   
Org. apache. struts2.dispatcher. FilterDispatcher
  
 
 
 
  
   
Struts2
  
  
   
/*
  
 

Of course, some initialization parameters or constants (such as struts2 encoding) can be specified when this Filter is configured. I usually do not use this parameter in this field, so I did not write it, but I need to know that this field is okay.

Add initialization parameters.

2. Configure struts. xml

The core configuration file of the Struts2 framework is struts. xml, which is mainly used to manage the business controller of the struts2 framework, that is, the management Action.

By default, the Struts2 framework automatically loads struts in the/WEB-INF/classes path. xml file (so we need to add struts. put xml in the src directory of the project ).

In most applications, as the application scale increases, the number of actions in the system also increases, resulting in bloated struts and xml files, therefore, we need to configure the Action category to different xml files during development.

File, and then include other configuration files in the struts. xml file, for example, . This improves the readability of struts. xml files.

Look at the structure of a struts. xml file, which is a demo file:

 
 
 
 
  
  
  
  
  
  
  
  
   
   
    
    
     
     Parameter Value
    
   
   
   
    
    
    
     
     Parameter Value
    
    
    
     
     
      
      Parameter Value
     
    
   
   
   
    
    Parameter Value
   
   
   
    
    Parameter Value
   
   
   
    
    
     Ing Resources
     
     Parameter Value
    
   
   
   
    
    
     
Exception Handling Resource
     
     Parameter Value
    
   
   
   Parameter Value
   
   
    
Ing Resources
    
    Parameter Value
   
   
   
    
    Parameter Value
   
   
   
    
Exception Handling Resource
    
    Parameter Value
   
  
 

The above configuration file contains a lot of content, so we can learn about struts. A complete structure of the xml file, but the meaning of each configuration element may not be fully understood. Here there are not many details involved.

Later, the article will gradually explore the significance of each configuration element.

3. Configure struts. properties

The Struts2 framework consists of two core configuration files. The struts. xml file is mainly used to manage the Action ing in the application and the Result definition contained in the Action. In addition, the Struts2 framework also includes

A configuration file containing struts. properties defines a large number of properties of the Struts2 framework. developers can change these attributes to meet application requirements.

The struts. properties file contains a large number of key-value pairs. Each key is a Struts2 attribute, and each value is a Struts2 attribute value.

Struts. properties files are typically placed under the WEB-INF/classes directory.

Configurable attributes and descriptions are as follows:

Struts. configuration: This attribute specifies the configuration file manager for loading the Struts 2 configuration file. The default value of this attribute is org. apache. Struts2.config. DefaultConfiguration,

This is the default configuration file manager for Struts 2. If you need to implement your own Configuration Manager, developers can implement a class that implements the Configuration interface,

You can load the Struts 2 configuration file by yourself.

Struts. locale: Specifies the default Locale of the Web application.

Struts. i18n. encoding: Specifies the default deployment set of the Web application. This attribute is very useful for processing Chinese Request Parameters. For obtaining Chinese request parameter values, set this attribute value to GBK or

GB2312. If this parameter is set to GBK, The setCharacterEncoding method of HttpServletRequest is called.

Struts. objectFactory: Specifies the default ObjectFactoryBean of Struts 2. The default value of this attribute is spring.

Struts. objectFactory. spring. autoWire: Specifies the automatic assembly mode of the Spring framework. The default value of this attribute is name, which means automatic assembly based on the Bean name attribute by default.

Struts. objectFactory. spring. useClassCache: This attribute specifies whether the Bean instance is cached when the Spring framework is integrated. Only the attribute values true and false are allowed,

The default value is true. We do not recommend that you modify this attribute.

Struts. objectTypeDeterminer: This attribute specifies the type detection mechanism of Struts 2. Generally, the attribute values tiger and notiger are supported.

Struts. multipart. parser: This attribute specifies the framework for processing multipart/form-data MIME type (File Upload) requests. This attribute supports attribute values such as cos, pell, and jakarta,

That is, it corresponds to the cos File Upload framework, pell upload, and common-fileupload File Upload framework. The default value of this attribute is jakarta. Note that if you need to use cos or pell files

Upload method, copy the corresponding JAR file to the Web application. For example, to use the cos upload method, you need to download the JAR file of the cos framework and put the file in the WEB-INF/lib path.

Struts. multipart. saveDir: This attribute specifies the temporary storage path of the uploaded file. The default value of this attribute is javax. servlet. context. tempdir.

Struts. multipart. maxSize: This attribute specifies the maximum number of bytes allowed for the entire request content during Struts 2 file upload.

Struts. custom. properties: This attribute specifies that the Struts 2 Application loads the custom property file. The property specified by this custom property file does not overwrite the struts. properties file.

. If you need to load multiple custom property files, the names of multiple custom property files are separated by commas.

Struts. mapper. class: Specifies to map HTTP requests to the er of the specified Action. Struts 2 provides the default ing.

Tool: org. apache. struts2.dispatcher. mapper. DefaultActionMapper. By default, the er maps the request prefix to the Action name attribute.

Struts. action. extension: This attribute specifies the request suffix to be processed by Struts 2. The default value of this attribute is action, that is, all requests matching *. action are processed by Struts 2.

If you need to specify multiple request suffixes, multiple suffixes are separated by commas.

Struts. serve. static: whether the attribute is set to provide static content services through the JAR file. This attribute only supports true and false attribute values. The default attribute value of this attribute is true.

Struts. serve. static. browserCache: This attribute sets whether the browser caches static content. When the application is in the development stage, you can set

This attribute is false.

Struts. enable. DynamicMethodInvocation: This attribute sets Struts 2 to support dynamic method calls. The default value of this attribute is true. If you need to disable dynamic method calls, you can set this

The property is false.

Struts. enable. SlashesInActionNames: This attribute sets whether Struts 2 allows diagonal lines in the Action name. The default value of this attribute is false. If the developer wants to allow

You can set this attribute to true by using a diagonal line.

Struts. tag. altSyntax: This attribute specifies whether expression syntax can be used in Struts 2 labels. Because expression syntax is usually used in labels, the attribute should be set to true.

The default value is true.

Struts. devMode: This attribute sets whether the Struts 2 application uses the development mode. If this attribute is set to true, more and more friendly error messages can be displayed when an application error occurs. This property only accepts true

And flase. The default value of this attribute is false. Generally, an application sets this attribute to true during development. when the product is released, this attribute is set to false.

Struts. i18n. reload: This attribute sets whether the system reloads the resource file every time an HTTP request arrives. The default value of this attribute is false. Setting this attribute to true in the development phase is more helpful for enabling

Release, but set this attribute to false during the product release phase. It is prompted that the property is set to true in the development phase, and the international resource file can be reloaded at each request, so that developers can see

Real-time development effect. This attribute should be set to false in the product release stage to provide response performance. re-loading resource files is required for each request, which greatly reduces application performance.

Struts. ui. theme: This attribute specifies the default view topic of the view tag. The default value of this attribute is xhtml.

Struts. ui. templateDir: This attribute specifies the location of the template file required by the view topic. The default value of this attribute is template, that is, the template file under the template path is loaded by default.

Struts. ui. templateSuffix: This attribute specifies the suffix of the template file. The default attribute value of this attribute is ftl. This attribute also allows ftl, vm, or jsp to correspond to FreeMarker, Velocity, and

JSP template.

Struts. configuration. xml. reload: This attribute sets whether the system automatically reloads the file after the struts. xml file is changed. The default value of this attribute is false.

Struts. velocity. configfile: Specifies the location of the Velocity. properties file required by the velocity framework. The default value of this attribute is velocity. properties.

Struts. velocity. contexts: This attribute specifies the Context position of the Velocity framework. If the framework has multiple contexts, multiple contexts are separated by commas.

Struts. velocity. toolboxlocation: This attribute specifies the location of the toolbox of the Velocity framework.

Struts. url. http. port: This attribute specifies the listening port of the Web application. This attribute is usually not large for users. It is provided only when Struts 2 needs to generate a URL (such as a Url tag ).

The default port of the Web application.

Struts. url. https. port: This attribute is similar to the struts. url. http. port attribute. The difference is that this attribute specifies the encrypted service port of the Web application.

Struts. url. includeParams: This attribute specifies whether the request parameters are included when Struts 2 generates a URL. This property accepts the values of none, get, and all, which correspond to the non-contained and only GET types.

Request Parameters and all request parameters.

Struts. custom. i18n. resources: This attribute specifies the international resource file required by the Struts 2 application. If there are multiple international resource files, the names of these resource files are separated by commas (,)

.

Struts. dispatcher. parametersWorkaround: for some Java EE servers, HttpServletRequest does not support calling the getParameterMap () method. You can set this attribute value.

To true. The default value of this attribute is false. This attribute should be set to true for WebLogic, Orion, and Solaris servers.

Struts. freemarker. manager. classname: This attribute specifies the FreeMarker manager used by Struts 2. Default value of this attribute

Value: org. apache. struts2.views. freemarker. FreemarkerManager. This is the built-in FreeMarker manager of Struts 2.

Struts. freemarker. wrapper. altMap: This attribute can only be set to true or false. The default value is true. You do not need to modify the attribute value.

Struts. xslt. nocache: This attribute specifies whether the XSLT Result is cached in the style sheet. This attribute is usually set to true when the application is in the development stage. When the application is in the product use stage, this attribute

Is usually set to false.

Struts. configuration. files: This attribute specifies the default configuration file loaded by the Struts 2 framework. If you need to specify multiple configuration files loaded by default, the file names of multiple configuration files are English

Separated by commas. The default value of this property is struts-default.xml, struts-plugin.xml, struts. xml, and the reader should understand why the Struts2 framework defaults

Loads the struts. xml file.

Sometimes developers do not like the additional struts. properties file. Struts2 allows you to use the struts. xml file to manage these attributes. In the struts. xml file, you can configure them by configuring the same constant element.

These attributes.

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.