from :http://www.cnblogs.com/fmricky/archive/2010/05/20/1740479.html

struts.xml It's the most utilized file in our development , It's also Struts2 The most important configuration file in .

Let's introduce some of them struts.xml Tags commonly used in

1、<include>

utilize include label , You can put a struts.xml The profile is split into multiple profiles , And then in struts.xml Use in <include> Tag introduces other configuration files .

Like an online shopping program , You can configure users 、 Commodity allocation 、 Order configurations are placed in 3 Configuration files user.xml、goods.xml and order.xml in , And then in struts.xml Lieutenant general 3 A configuration file is introduced :

struts.xml:

?
1
2
3
4
5
6
7
8
9
10
<? xml version="1.0" encoding="UTF-8"?>
<! DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
 
< struts >
    < include file="user.xml"/>
    < include file="goods.xml"/>
    < include file="order.xml"/>
</ struts >

user.xml:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<? xml version="1.0" encoding="UTF-8"?>
<! DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
 
< struts >
    < package name="wwfy" extends="struts-default">
        < action name="login" class="wwfy.user.LoginAction">
            <!-- Omit Action Other configuration -->
        </ action >
        < action name="logout" class="wwfy.user.LogoutAction">
            <!-- Omit Action Other configuration -->
        </ action >
    </ package >
</ struts >

2、<constant>

I mentioned before struts.properties In the introduction of configuration file , We've mentioned all of them in the past struts.properties Properties defined in the file , Can be configured in struts.xml In file . And in the struts.xml in , It's through <constant> Tag to configure :

?
1
2
3
4
5
6
7
8
9
10
11
12
<? xml version="1.0" encoding="UTF-8"?>
<! DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
 
< struts >
    <!-- Set the development mode -->
    < constant name="struts.devMode" value="true"/>
    <!-- Set the encoding form to GB2312-->
    < constant name="struts.i18n.encoding" value="GB2312"/>
    <!-- Omit other configuration information -->
</ struts >

3、<package>

1、 Package properties Introduction

stay Struts2 The framework is managed through packages action、result、interceptor、interceptor-stack And so on . The package properties are as follows :

attribute

Is it necessary to

describe

name yes Package name , Apply the label of this package as other packages
extends no Set this package to inherit other packages
namespace no Set the namespace of the package
abstact no Set to abstract package

2、extends Detailed explanation of attributes

  • When a package is configured extends Property inherits another package , The package will inherit all the configurations in the parent package , Include action、result、interceptor etc. .
  • Because the packet information is obtained in the order of configuration files , So the parent package must be defined before the child package .
  • Usually we configure struts.xml When , They all inherit a name called “struts-default.xml” My bag , This is a struts2 The package built into .

3、namespace Detailed explanation of

namespace Mainly for large projects Action Management of , More important is to solve Action The problem of duplicate names , Because not in the same namespace Action You can use the same Action Named .

1) If you use a namespace URL Will change

For example, we have a configuration file

?
1
2
3
4
5
< package name="wwfy" extends="struts-default">
    < action name="login" class="wwfy.action.LoginAction">
        < result >/success.jsp</ result >
    </ action >
</ package >

In this configuration Action Of URL by http://localhost:8080/login.action

If a namespace is specified for this package

?
1
2
3
4
5
< package name="wwfy" extends="struts-default" namespace="/user">
    < action name="login" class="wwfy.action.LoginAction">
        < result >/success.jsp</ result >
    </ action >
</ package >

In this configuration Action Of URL by http://localhost:8080/user/login.action

2) Default namespace

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

3) Specify the root namespace

When the namespace is set to “/”, That is, when the package's namespace is specified as the root namespace , At this point, all the root paths of Action Requests will go to this package to find the corresponding resource information .

If the path in the previous example is http://localhost:8080/login.action Then all http://localhost:8080/*.action Will look for resources in the package set as the root namespace .

4、<action> And <result>

1、<action> Property introduction

The attribute name

Whether must

Function description

name yes Requested Action name
class no Action The processing class corresponds to the specific path
method no Appoint Action Method name in
converter no Appoint Action The type converter used

If not specified method By default Action Medium execute Method .

2、<result> Property introduction

The attribute name

Whether must

Function description

name no Corresponding Action Return logical view name , The default is success
type no Return result type , The default is dispatcher

3、 The use of wildcards

With result An increase in ,struts.xml Files will become more and more complex as well . Then you can use wildcards to simplify the configuration :

For example, the following case :

Action by Test.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
public class Test {
    public String test1(){
        return "result1" ;
    }
   
    public String test2(){
        return "result2" ;
    }
   
    public String test3(){
        return "result3" ;
    }
}

struts.xml Is configured as

?
1
2
3
4
5
< package name="wwfy" extends="struts-default">
    < action name="test*" class="wwfy.action.test{1}">
        < result name="result{1}">/result{1}.jsp</ result >
    </ action >
</ package >

4、 visit Action Another implementation of the method

stay Struts2 If you want to access Action The specified method in , It can also be changed by URL Request to implement , Will be the original “Action name .action” Change it to “Action name ! Method name .action” stay struts.xml You don't need to specify a method name in .

5、<exception-mapping> And <global-exception-mapping>

These two tags are used to configure the corresponding view information when an exception occurs , Only one is Action Scope , One is package wide , When the same type exception is configured in both scopes ,Action The priority of scope is higher than that of package scope . These two tags contain the same attributes :

The attribute name

Whether must

Function description

name no Used to represent the exception configuration information
result yes Specifies the view information to display when an exception occurs , This is the logical view
exception yes Specify the exception type

The sample code for the two tags is :

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<? xml version="1.0" encoding="UTF-8"?>
<! DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
 
< struts >
    < package name="default" extends="struts-default">
        < global-exception-mappings >
            < exception-mapping result=" Logical view " exception=" Exception types "/>
        </ global-exception-mappings >
        < action name="Action name ">
            < exception-mapping result=" Logical view " exception=" Exception types "/>
        </ action >
    </ package >
</ struts >

6、<default-class-ref>

When we're configuring Action When , If not for a certain Action Specify specific class When the value of , The system will automatically reference <default-class-ref> The class specified in the tag . stay Struts2 In the frame , By default class by ActionSupport, The configuration we can use in xwork Under the core package of xwork-default.xml Found in file .

When there is a special need , You can manually specify the default class

?
1
2
3
4
5
6
7
package wwfy.action;
 
public class DefaultClassRef {
    public void execute(){
        System.out.println( " Default class Start execution ……" );
    }
}

stay struts.xml Middle configuration

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<? xml version="1.0" encoding="UTF-8"?>
<! DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
 
< struts >
    < package name="wwfy" extends="struts-default">
        <!-- Specify the default class by Test -->
        < default-class-ref class="wwfy.action.DefaultClassRef"/>
        < action name="test1">
            < result >/index.jsp</ result >
        </ action >
    </ package >
</ struts >

7、<default-action-ref>

If you're asking for an undefined Action Resource time , The system will throw 404 error . Such mistakes are inevitable , But such a page is not friendly . We can use <default-action-ref> To specify a default Action, If the system does not find the specified Action, Will be specified to call the default Action.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<? xml version="1.0" encoding="UTF-8"?>
<! DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
 
< struts >
    < package name="wwfy" extends="struts-default">
       
        < default-action-ref name="acctionError"></ default-action-ref >
        < action name="acctionError">
            < result >/jsp/actionError.jsp</ result >
        </ action >
    </ package >
</ struts >

8、<default-interceptor-ref>

This tag is used to set up all the Action The default interceptor information to be applied . In fact, our package is inherited struts-default After the bag , It uses Struts Default Settings . We can do it in struts-default.xml Find the configuration in :

?
1
< default-interceptor-ref name="defaultStack"/>

In the actual development process , If we have special requirements, we can change the default interceptor configuration . Once the configuration was changed ,“defaultStack” Will no longer be quoted , You need to manually add .

9、<interceptors>

Through this tag, you can send Struts2 The interceptor or stack is registered in the framework , It is usually used for the registration of custom interceptors or interceptor stacks . How to use this label is as follows :

?
1
2
3
4
5
6
< interceptors >
    < interceptor name=" Interceptor name " class=" Interceptor class "/>
    < interceptor-stack name=" Interceptor stack name ">
        < interceptor-ref name=" Interceptor name ">
    </ interceptor-stack >
</ interceptors >

10、<interceptor-ref>

The tag can be used to identify the Action Add interceptor function . When for some Action After adding interceptor function alone ,<default-interceptor-ref> The interceptor specified in will no longer be used for this Action Work .

11、<global-results>

This tag is used to set the global result set within the package scope . In more than one Action Return to the same logical view , Can pass <global-results> Tags uniformly configure the logical views corresponding to these physical views .

?
1
2
3
4
5
6
7
8
9
10
11
12
<? xml version="1.0" encoding="UTF-8"?>
<! DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
 
< struts >
    < package name="wwfy" extends="struts-default">
        < global-results >
            < result name="test">/index.jsp</ result >
        </ global-results >
    </ package >
</ struts >

[ turn ] struts.xml Configuration details of more related articles

  1. Struts2 note ——struts.xml Configuration details

    visit HelloWorld Settings for applied paths * stay struts1 in , adopt <action path=“/primer/helloWorldAction.action”> Node path Property specifies ...

  2. struts.xml Configuration details Inside information Do not reprint Thank You for Your Cooperation

    1.<include> utilize include label , You can put a struts.xml The profile is split into multiple profiles , And then in struts.xml Use in <include> Tag introduces other configuration files ...

  3. struts.xml Configuration details

    struts.xml It's the most utilized file in our development , It's also Struts2 The most important configuration file in . Let's introduce some of them struts.xml Tags commonly used in 1.<include> utilize includ ...

  4. struts.xml Configuration details

    struts.xml It's the most utilized file in our development , It's also Struts2 The most important configuration file in . Let's introduce some of them struts.xml Tags commonly used in 1.<include> utilize includ ...

  5. struts2 Of struts.xml Configuration details

    In the application struts When the framework is being developed , The essential step is to struts.xml To configure , The more you know about this file , It will be easier for us to develop an application . Let's take a look struts.xml The content of , What's the use of each one of them . ...

  6. java web.xml Configuration details ( turn )

    Source :java web.xml Configuration details 1. General configuration : At every station WEB-INF There's one down there web.xml Settings file for , It provides the configuration settings of our platform . web.xml Definition : . The name and description of the platform . For environmental parameters ...

  7. web.xml Configuration details listener

    web.xml Configuration details listener Definition <listener> <listener-class>nc.xyzq.listener.WebServicePublishLis ...

  8. Spring introduction web.xml Configuration details

    Spring introduction web.xml Configuration details https://www.cnblogs.com/cczz_11/p/4363314.html https://blog.csdn.net/hellolove ...

  9. tomcat in server.xml Configuration details ( Reprint )( One )

    Reprinted from :https://www.cnblogs.com/starhu/p/5599773.html tomcat in server.xml Configuration details Tomcat Server The structure diagram of is as follows :( This document describes such as ...

Random recommendation

  1. YbSoftwareFactory Code generation plug-ins 【 Twenty-four 】:MVC Dynamic custom routing is implemented in

    In the last introduction   Document circulation system   The implementation of the , This article introduces MVC The implementation of dynamic custom routing under . In a typical CMS In the system , You usually need to specify a friend chain address for a column , By specifying the friend chain address , The address in this column is more humanized . Convenient memory , It's also used in search citation ...

  2. Example :Basic Lens sample

    This example demonstrates how to extend a Camera Lens. 1. UI The interface is a MediaViewer <controls:MediaViewer x:Name="MediaViewer&qu ...

  3. graph theory --- spfa + Chain forward star ( The template questions ) dlut 1218 : Kiki and transformers

    1218: Kiki and transformers Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 130  Solved: 37[Submit][Status][Web Boa ...

  4. Linux How to check which processes are occupied by CPU Most memory resources

    linux Get the occupation under CPU The most resourceful 10 A process , You can use the following command combination : ps aux|head -1;ps aux|grep -v PID|sort -rn -k +3|head linux Get the occupation under ...

  5. (step8.2.2)hdu 2509(Be the Winner—— Simple game )

    The main idea of the topic : Enter an integer n, It means the number of matches ( The original question actually refers to the number of apples , But to be as consistent as possible with the template , So it is understood here as the number of matches .... In fact, it doesn't matter to understand why , It is important to , Just understand ....). In the next line , Yes ...

  6. hdu 3247 AC Automatically + Pressure dp+bfs Handle

    Resource Archiver Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Ot ...

  7. Oracle Program backup

    -- Use root User backup su - root mkdir -p /oracle/data_dump/backup_soft nohup tar -cvf /oracle/data_dump/backu ...

  8. Beginners Python—— File operation II

    Preface : Why do I need a second file operation ? Because the knowledge in the first chapter is not enough to support the basic needs . Let's analyze them one by one . One .Python The characteristics of file operation Let's start with an analogy , As the ancestor of high-level programming language ,C How language operates on files ? character ( strand ...

  9. How to make mysql Sort by two or more fields

    I'm going to design a supply and demand information form , It contains the serial number (id)( Auto increment ), Release date (time), Last update (last_time). Because in order to avoid people repeatedly publishing information to occupy the forefront, so set up last_time This ...

  10. React-Native Advanced _3. Touch highlight TouchableHighlight

    On Android ListView   Click on One of the sub views , There will be highlights , The effect is ReactNative Pass through TouchableHighlight Realization , The specific use is as follows 4. Touch highlight Touchabl ...