In-depth explanation of struts2--struts.xml configuration (i.)

Source: Internet
Author: User
Tags constant extend

configuration of the bean
STRUTS2 is an extensible framework in which core components are configurable and assembled by struts2 their own dependency injection containers.
In the Struts-default.xml file, you define the configurable components of the STRUTS2 framework, such as:

<bean type= "Com.opensymphony.xwork2.ActionProxyFactory" name= "Xwork" class= " Com.opensymphony.xwork2.DefaultActionProxyFactory "/>
<bean type=" Com.opensymphony.xwork2.ActionProxyFactory "name=" struts "class=" Org.apache.struts2.impl.StrutsActionProxyFactory "/>
<bean class=" Com.opensymphony.xwork2.ObjectFactory "static=" true "/>

You can configure a bean for two purposes:
1. The framework's IOC container creates an instance of the bean and then injects the instance into the frame's internal object
2. Inject value into bean by static method of Bean
In the first usage, the bean is injected into the framework to collaborate internally with the inner object, and the framework knows the type of the bean, so when you configure the bean, you typically use the type attribute to indicate the interface that the bean implements. For example, we created our own objectfactory, and we can use the bean configuration in the Struts.xml file:

<struts>
    <bean type= "com.opensymphony.xwork2.ObjectFactory" name= "MyFactory" class= " Com.company.myapp.MyObjectFactory "/>
</struts>

In the second usage, a value injection is used, allowing the bean to receive the constant of the framework without creating the bean. The bean uses value injection and must use the static property, such as a configuration entry in the Struts-default.xml file:

<bean class= "Com.opensymphony.xwork2.ObjectFactory" static= "true"/>

We seldom use bean elements in real-world development, because we generally do not extend or replace the core components of STRUT2.
configuration of Constants (Constant)
With constant configuration, you can change the behavior of the STRUTS2 framework and plug-ins to meet the needs of different Web applications.
Constant configuration in Struts.xml:

<struts>
    <constant name= "Struts.devmode" value= "true"/>
</struts>

Constant configuration in Struts.properties
Struts.devmode=true
Constant configuration in Web. xml

<filter>
    <filter-name>struts</filter>
    <filter-class> org.apache.struts2.dispatcher.filterdispatcher</filter-class>
    <init-param>
        <param-name >struts.devMode</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>

The package in the

package configuration
Struts2 is similar to a package in Java, providing a way to organize the action, result, result type, interceptor, and interceptor stack into a single logical unit. Improve reusability. Unlike packages in Java, packages in STRUTS2 can extend additional packages to "inherit" all definitions of the original package, including all action, result, interceptor, and interceptor stacks, and can add their own package-specific configuration, as well as modify some of the original package configuration. The
uses the package element in the Struts.xml file to define packages. The package element has a required property name, which specifies the name of the packet, which will be used as the key to refer to the package (key). The package's name must be unique, and the extends property of the packages element is optional, allowing a package to inherit the configuration from one or more implementation-defined packages, separated by commas if multiple packages are specified.
For example:

<struts> <!--defines a package named default that inherits from the Struts-default abstraction package defined in the Struts-default.xml file--<package name= "Defau LT "extends=" Struts-default "> <interceptors> <!--definition of interceptor stack named Crudstack-<interceptor- Stack name= "Crudstack" > <interceptor-ref name= "checkbox"/> <interceptor-ref name= "par  AMS "/> <interceptor-ref name=" Static-params "/> </interceptor-ref name=" Defaultstack " /> </interceptor-stack> <action name= "Viewsource" class= "Org.apache.struts2.showcase.source.ViewS Ourceaction "> <result>viewSource.jsp</result> </action> </package> <!-- Defines a package named skill that inherits from the previously defined default package--<package name= "skill" extends= "default" namespace= "/skill" > <!- -Set the default interceptor reference, Crudstack is inherited from the default package--<default-interceptor-ref name= "Crudstack"/> <action N Ame= "List" class= "Org.apaChe.struts.showcase.action.SkillAction "method=" List "> <result>/empmanager/listskills.jsp</result&
            Gt
        <!--set an action-referenced interceptor that overrides the default interceptor reference--<interceptor-ref name= "Basicstack"/> </action> <action name= "list" class= "org.apache.struts.showcase.action.SkillAction" method= "List" > <resul T>/empmanager/editskills.jsp</result> <!--set an action-referenced interceptor that overrides the default interceptor reference--<interce Ptor-ref name= "params"/> <interceptor-ref name= "Basicstack"/> </action> </pack Age> <!--Define a package named employee, inherited from the default package that was previously defined--<package name= "employee" extends= "Default" namespace= "/ Employee "> <default-interceptor-ref name=" crudstack "/> <action name=" list "class=" Org.apache. Struts2.showcase.action.EmployeeAction "method=" List "> <result>/empmanager/listskills.jsp</result&
    Gt    </action> <!--Set the mode interceptor reference, Basicstack is inherited from the Struts-default package--<interceptor-ref name= "b Asicstack "/> </package> </struts>

configuration with (include)
The include element has only one required property file, which specifies the filename of the included file, for example:

<struts>
    <include file= "Struts-chat.xml"/>
</struts>

It should be noted that each contained file must have the same format as Struts.xml and that the contained file itself is also a complete configuration file.
If the included configuration file and Struts.xml are not in the same directory, for example, in the Org/sunxin/user directory, configure the file:

<include file= "Org/sunxin/user/struts-user.xml"/>

Interceptor (Interceptor) configuration
Interceptors allow you to insert code execution before and after the action is executed. The Interceptor in Struts2 is a powerful tool that can dynamically add input validation, object assembly, permission control, journaling, and so on for action without modifying the action.
To configure a referenced interceptor for an action, you first need to define the interceptor using the interceptor element in the interceptors element, and then use the INTERCEPTOR-REF element in the action element to specify the referenced interceptor. The interceptor element has two required properties: Name and class, which specifies the name of the Interceptor, which specifies the complete class name of the Interceptor. If we want to configure two interceptors for resourceaction logger and security, as follows:

<package name= "Default" extends= "Struts-default" >
    <interceptors>
        <!--define interceptors named Logger--
        <interceptor name= "Logger" class= "Org.sunxin.interceptor.LogInterceptor"/>
        <!-- Define an interceptor named security--
        <interceptor name= "Security" class= "Org.sunxin.interceptor.ValidationInterceptor"/ >
    </interceptors>
    <action name= "resource" class= "Org.sunxin.action.ResourceAction" >
        <result name= "Input" >login.jsp</result>
        <result>resource.jsp</result>
        <!-- Specifies the interceptor reference for the resource action, and when resource action is invoked, the logger and security interceptors are also called-
        <interceptor-ref name= "Logger" />
        <interceptor-ref name= "security"/>
    </action>
</package>

Interceptors are executed in the order in which the action refers to the interceptor, and if an action requires multiple interceptors, one reference is cumbersome, multiple interceptors can be grouped together to form an interceptor stack, and then the interceptor is referenced directly in the action, for example:

<package name= "Default" extends= "Struts-default" > <interceptors> <!-- Define interceptors named Logger-<interceptor name= "logger" class= "Org.sunxin.interceptor.LogInterceptor"/> < ;! --Define an interceptor named security--<interceptor name= "security" class= "Org.sunxin.interceptor.ValidationInterceptor"/&G
    T </interceptors> <interceptor-stack name= "Loggerandsecuritystack" > <interceptor-ref name= "Logge R "/> <interceptor-ref name=" security "/> </intercetpor-stack> <action name=" Resource "C lass= "Org.sunxin.action.ResourceAction" > <result name= "input" >login.jsp</result> <result >resource.jsp</result> <interceptor-ref name= "Loggerandsecuritystack"/> </action> </p Ackage> 

The

Struts2 does not differentiate between interceptors and interceptor stacks when referencing interceptors, so we can also refer to other interceptor stacks when defining the interceptor stack.
If more than one action needs to refer to the same interceptor, then we can use the Default-interceptor-ref element to define a default interceptor or interceptor stack reference so that no reference information is specified for each action. For example:

<package name= "Default" extends= "Struts-default" > <interceptors> <!-- Define interceptors named Logger-<interceptor name= "logger" class= "Org.sunxin.interceptor.LogInterceptor"/> < ;! --Define an interceptor named security--<interceptor name= "security" class= "Org.sunxin.interceptor.ValidationInterceptor"/&G
    T </interceptors> <interceptor-stack name= "Loggerandsecuritystack" > <interceptor-ref name= "Logge
    R "/> <interceptor-ref name=" Security "/> </intercetpor-stack> <!--define default interceptor Stack reference-- <default-intercetpor-ref name= "Loggerandsecuritystack" > <!--resource action will use the default interceptor stack reference---<acti On Name= "resource" class= "Org.sunxin.action.ResourceAction" > <result name= "Input" >login.jsp</result>
        ; <result>resource.jsp</result> </action> </package> 

Note that if you define a different interceptor reference in an action, the action will no longer use the default interceptor reference, for example:

<package name= "Default" extends= "Struts-default" > <interceptors> <!--define interceptors named Logger-- <interceptor name= "Logger" class= "Org.sunxin.interceptor.LogInterceptor"/> <!--define an interceptor named security--&G
        T <interceptor name= "Security" class= "Org.sunxin.interceptor.ValidationInterceptor"/> </interceptors> & Lt;interceptor-stack name= "Loggerandsecuritystack" > <interceptor-ref name= "Logger"/> <interc Eptor-ref name= "Security"/> </intercetpor-stack> <!--definition Default interceptor Stack reference--<default-intercetpor-r EF name= "Loggerandsecuritystack" > <!--Resource Action will no longer use the default interceptor stack reference and use a timer---<action name= "Resourc E "class=" org.sunxin.action.ResourceAction "> <result name=" input ">login.jsp</result> <re sult>resource.jsp</result> <interceptor-ref name= "Timer"/> </action> </package>

If the action wants to add a new interceptor based on the default interceptor reference, then only the interceptor in the default interceptor reference can be reconfigured in the action

<action name= "Resource" class= "Org.sunxin.action.ResourceAction" >
     <result name= "Input" >login.jsp </result>
     <result>resource.jsp</result>
     <interceptor-ref name= "Timer"/>
     <interceptor-ref name= "Loggerandsecuritystack"/>
</action>

Next, we continue to learn struts.xml about action configuration

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.