Details on configuring struts. xml instances

Source: Internet
Author: User
Struts. xml is the most frequently used file in development and the most important configuration file in Struts2. The following describes several labels commonly used in struts. xml: struts. xml is the most frequently used file in development and the most important configuration file in Struts2.

The following describes several labels commonly used in struts. xml.

1,

Using the include tag, you can split a struts. xml configuration file into multiple configuration files, and then use Label to introduce other configuration files.

For example, an online shopping program can put user configuration, item configuration, and order configuration in three configuration files respectively. xml, goods. xml and order. xml, and then in struts. introduce these three configuration files in xml:

Struts. xml:

  
  
   
   
   
  

User. xml:

  
  
   
    
    
   
  
2,

In the introduction to the struts. properties configuration file, we mentioned that all attributes defined in the struts. properties file can be configured in the struts. xml file. In struts. xml Label:

   
   
    
    
    
    
    
   
3, 1. package attributes

In the Struts2 framework, packages are used to manage configuration information such as action, result, interceptor, and interceptor-stack. The package attributes are as follows:

Attribute

Required

Description

Name Yes The package name, which is used as a flag for other packages.
Extends No Set this package to inherit from other packages
Namespace No Set the package namespace
Abstact No Set as abstract package

2. Details About extends attributes
  • When a package inherits from another package by configuring the extends attribute, the package inherits all the configurations of the parent package, including action, result, and interceptor.

  • Because the packet information is obtained in the order of configuration files, the parent package must be defined before the child package.

  • Usually when we configure struts. xml, we inherit a package named "struts-default.xml", which is the built-in package in struts2.

3. details about namespace

Namespace is mainly used to manage actions in large projects. More importantly, it solves the problem of duplicate Action names, because actions not in the same namespace can use the same Action name.

1) if namespace is used, the URL will change

For example, we have a configuration file.

    
     
      /success.jsp
     
    

The URL of the Action in this configuration is localhost: 8080/login. action.

If the namespace is specified for this package

    
     
      /success.jsp
     
    

The URL of the Action in this configuration is localhost: 8080/user/login. action.

2) default namespace

If no namespace is specified for a package in Struts2, the package uses the default namespace. the default namespace is always "".

3) specify the root namespace

When the namespace is set to "/", that is, the package namespace is specified as the root namespace, all Action requests under the root path will go to this package to find the corresponding resource information.

If the ingress path is http: // localhost: 8080/login. for action, all http: // localhost: 8080 /*. action is used to search for resources in the package set as the root namespace.

4. 1. Introduction to attributes

Attribute name

Required?

Function description

Name Yes Name of the requested Action
Class No Specific path of Action processing class
Method No Specifies the method name in the Action.
Converter No Type converter used by the specified Action

If no method is specified, execute method in Action is executed by default.

2, Attribute introduction

Attribute name

Required?

Function description

Name No The logical view name returned for the corresponding Action. The default value is success.
Type No Return result type. the default value is dispatcher.

3. Use of wildcards

As the result increases, the struts. xml file becomes more and more complex. Then you can use wildcards to simplify the configuration:

For example:

The Action is Test. java.

public class Test {public String test1(){return "result1";}public String test2(){return "result2";}public String test3(){return "result3";}}

Struts. xml is configured

      
       
        /result{1}.jsp
       
      
4. another way to access the Action method

In Struts2, if you want to access the specified method in Action, you can also change the URL request to change the original "Action name. action" to "Action name! The method name. action does not need to be specified in struts. xml.

5, And

Both labels are used to configure the view information corresponding to an exception, but one is the Action range and the other is the packet range. when the same type of exception is configured in both ranges, the priority of the Action range is higher than that of the package range. the two tags have the same attributes:

Attribute name

Required?

Function description

Name No Used to indicate the abnormal configuration information
Result Yes View information displayed when an exception occurs. set this parameter to logical view.
Exception Yes Exception type


The sample code for the two labels is:

        
        
         
          
           
          
          
         
        
6,

When we configure an Action, if a specific class value is not specified for an Action, the system will automatically reference The class specified in the label. In the Struts2 framework, the default class is ActionSupport, which can be found in the xwork-default.xml file under the xwork core package.

When you have special requirements, you can manually specify the default class

Package wwfy. action; public class DefaultClassRef {public void execute () {System. out. println ("default class starts execution ...... ");}}

Configure in struts. xml

         
         
          
           
           
           
            
/Index. jsp
           
          
         
7,

If you request an undefined Action resource, the system throws the 404 error. Such errors are inevitable, but such pages are not friendly. We can use To specify a default Action. if the system does not find the specified Action, it will be specified to call this default Action.

          
          
           
            
            
             /jsp/actionError.jsp
            
           
          
8,

This label is used to set the default interceptor information to be applied by all actions in the entire package range. In fact, our package inherits the struts-default package and uses the default settings of Struts. We can find the related configuration in the struts-default.xml:

           

In actual development, we can change the default interceptor configuration if we have special requirements. Once this configuration is changed, defaultStack will no longer be referenced and needs to be manually added.

9,

You can use this tag to register an interceptor or an interceptor stack in the Struts2 framework. generally, it is used to register a custom interceptor or an interceptor stack. The label is used as follows:

            
             
             
              
             
            
10,

You can use this tag to add the interceptor function for the Action in which the object is located. When the interceptor function is added separately for an Action, The interceptor specified in will no longer work for this Action.

11,

This label is used to set global result sets within the package range. When multiple actions return the same logical view Tags are used to configure the logical views corresponding to these physical views.

              
              
               
                
                 
                  /index.jsp
                 
                
               
              

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.