Struts.xml commonly used label detailed

Source: Internet
Author: User
Tags constant cos file size file upload xslt

Struts.xml is the core configuration file for Struts 2, which is primarily responsible for managing the action mappings in the app, as well as the result definitions that the action contains. Let's take a look at Struts.xml's most streamlined basic configuration:

<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE struts public 
"-//apache software foundation//dtd struts Configuration 2.0//en" 
"Http://struts.apache . org/dtds/struts-2.0.dtd ">
<struts>
  <action name=" action name "class=" Action class Path "method=" method name " >
     <result name= "Logical view name" type= "result type" >
        <param name= "parameter name" > Parameter value </param>
     </result >
  </action>
</struts>

Note: Struts2 the order in which the configuration files are loaded:

Struts-default.xml (Struts2-core-2.x.x.x.jar), Struts-plugin.xml (in each plug-in), Struts.xml (custom)

NOTE: If you configure the same information in multiple configurations, the configuration in the latter configuration overrides the previous configuration.


The following is a brief introduction to the element tags commonly used in the Struts.xml file:

<struts> Tags

The tag is the root tag of the Struts.xml file, which contains the following child elements:<package>, <constant>, <include>, <bean>. The following explanations:


First, <package> label

The label uses the package mechanism to resolve compatibility issues with action of the same name, which supports multiple inheritance, such as <package name= "test" extends= "Struts-default, Jfreechart-default" > equivalent to All configuration information under the <package name= "Struts-default"/> is inherited, which can greatly reduce the amount of code So usually the tag inherits from the Struts-default package in Struts-default.xml and loads the system's default configuration information. It contains the following sub-elements:<action>, <default-action-ref>, <default-class-ref>, <default-interceptor-ref>, <global-exception-mappings>, <global-results>, <interceptors>, <result-types>.

Label Properties

is required Function description
Name Is Package name, which is used as a tag for other packages to apply this package
Extends Whether To set this package to inherit other packages, you must inherit the Struts-defalut.xml package
Namespace Whether Set the namespace of the package, and more importantly, resolve the problem with the name of the action
Abstact Whether Set as abstract package

Note: The namespace attribute is detailed:

 (1)默认命名空间:
  <package name= "Wwfy" extends= "Struts-default" >
     <action name= "Login" class= "Wwfy.action.LoginAction" >
        <result>/success.jsp</result>
     </action>
   </package>

The package does not specify a namespace, with the default namespace "" (equivalent to Namespace= "").
The URL of the action under this configuration is Http://localhost:8080/projectName/login.action
If the action resource is not searched under the specified path, it is searched under the default namespace, with the same root namespace.
Eg: we visited: http://localhost:8080/projectName/test/login.action, search in the namespace of/test without the action resource of login, it will be searched directly into the default namespace.

(2)自定义命名空间:
  <package name= "Wwfy" extends= "Struts-default" namespace= "/user" > 
     <action name= "Login" class= " Wwfy.action.LoginAction ">
       <result>/success.jsp</result>
     </action>
  </ Package>

The URL of the action under this configuration is Http://localhost:8080/projectName/user/login.action

(3)根命名空间:
  <package name= "Wwfy" extends= "Struts-default" namespace= "/" >
     <action name= "Login" class= " Wwfy.action.LoginAction ">
       <result>/success.jsp</result>
     </action>
  </ Package>

The namespace of the package is the root namespace, at which point the action request under all the root paths will look for the corresponding resource information in the package.
All http://localhost:8080/projectname/*.action in this configuration will look for resources in packages that are set to the root namespace.

Description: Namespace's search order:

Default namespace, root namespace, custom namespace


1. <action> Label

This label is used to configure the action map to determine how to handle client requests. It contains the following child elements:<result>, <param>, <interceptor-ref>, <exception-mapping>.

Label Properties Whether you must Function description Default value
Name Is The requested action name
Class Whether The action handler class corresponds to the specific path Actionsupport
Method Whether Specify the name of the method in the action Execute
Converter Whether Specifies the type converter used by the action

Note: Use of wildcard characters:

* denotes generic content, {0} represents the content represented by all wildcard characters *, {i} represents the content represented by the I-wildcard *. Examples are as follows:

public class Testaction {public
   String test1 () {
       return "RESULT1";
   }
   Public String test2 () {
       return "result2";
   }
   Public String test3 () {
      return ' RESULT3 ';
   }
}

Struts.xml is configured in:

<package name= "Wwfy" extends= "Struts-default" >
  <action name= "test*" class= "Wwfy.action.TestAction" >
    <result name= "Result{1}" >/result{1}.jsp</result>
  </action>
</package>

US visit: http://localhost:8080/projectName/test1.action Jump to result1.jsp
Http://localhost:8080/projectName/test2.action Jump to Result2.jsp
Http://localhost:8080/projectName/test3.action Jump to Result3.jsp


1.1 <result> Tags

This tab is used to set the result view.

Label Properties Whether you must Function description Default value
Name Whether Corresponding action returns the logical view name Success
Type Whether return result type Dispatcher

Note: Several primary values of the type attribute:

(1)redirect:重定位到URL
<result type= "redirect" >/index.jsp?username=${username}</resul>
(2)redirectAction:重定向Action
<result type= "Redirectaction" >  //redirect Action <param name= "ActionName" under a namespace
    >xxx</param >   
    <param name= "namespace" >/test</param>
 </result>
(3)plainText:用于显示实际视图资源的代码
<result type= "PlainText" >
<param name= "Location" >/index.jsp</param>//This parameter specifies the actual resource corresponding to the logical view
<param name= "CharSet" >UTF-8</param>//This parameter specifies the encoding scheme in use
 </result>


2. <interceptors> Label

The tag allows you to register an interceptor or interceptor stack with the STRUTS2 framework, which is typically used to register a custom interceptor or interceptor stack.

<1--in the same package can only occur once- 
<interceptors>
   <interceptor name= "Interceptor name" class= "Interceptor class"/>
   < Interceptor-stack name= "Interceptor stack name" >
     <interceptor-ref name= "Interceptor name"/>
   </interceptor-stack>
</interceptors>


3. <default-interceptor-ref> Label

This tag is used to set the default interceptor information to be applied by all actions in the entire package scope, that is, all the action of the interceptor acting on that package.

<default-interceptor-ref name= "Mydefault" class= "Interceptor class"/>

<!--call the default interceptor in Struts-default--
< Default-interceptor-ref name= "Defaultstack"/>

Note: When the Interceptor function is added separately for an action, the interceptor specified in,<default-interceptor-ref> will no longer function with this action.


3.1 <interceptor-ref> Tags

This tab allows you to add interceptor functionality for the action it is in.


4. <global-results> Label

The label is used to identify the global result set within the package scope, which is used to share the result set. Examples are as follows:

(1)同一包内共享结果集:
<package name= "userpackage" namespace= "/user" extends= "Struts-default" >           <!--global result configuration, automatically jumps to the specified view when the returned view is input or error-    < global-results>       <result name= "error" >/error.jsp</result> <result Name= "Nosession" >/login.jsp</result>     </global-results>     <!- -When the action is configured with the same result as the global result view, the result view in the action overrides the global result view. -->    <action name= "Login" class= "action. Useraction "method=" login >        <result name= "Success" >/INDEX.JSP</RESULT&G
         T <result name= "error" >/loginError.jsp</result>    </action>     <!--
Note that the:<global-results> tag must be configured in front of all <action> tags, otherwise compilation error Unable to load configuration--> </package> 
Test access: http://localhost:8080/projectName/user/login.action/

Test result: Jump to loginerror.jsp view.

(2)不同包外共享结果集(使用继承实现):
  <package name= "xxx" extends= "Struts-default" > 
    <global-results>
       <result name= "message" >/ login.jsp</result>
    </global-results>
  </package>
  <package name= "xxxx" namespace= "Test" extends= "xxx" >
    <action name= "Login" class= "action. Loginaction "method=" Login > 
    </action>
  </package>

Visit: http://localhost:8080/projectName/test/login.action/


5. <default-action-ref> label

The label is used to configure the default action that is called when the system cannot find the specified action. By default, the system cannot find the specified action to throw a 404 exception. Examples are as follows:

<package name= "userpackage" namespace= "/user" extends= "Struts-default" > 
    
     <!-- Jumps to the default action when the specified action is not found--
     -<default-action-ref name= "UnKnown"/>

     <!--The default action's Jump view, Use the Default-action-ref label to point to it-
     <action name= "UnKnown" >  
         <result>/unknownaction.jsp</ result>  
     </action>  

     <!--Note the:<default-action-ref> label must be configured in front of all <action> tags, Otherwise, compile error Unable to load configuration-->

</package>

Test access: Http://localhost:8080/projectName/user/login.action//login.action does not define a claim.

Test result: Jump to the Unknowaction.jsp interface


6. <default-class-ref> Label

The label is used to configure the default class that is specified by the tag when no specific class is specified for an action.

<package name= "userpackage" namespace= "/user" extends= "Struts-default" > 
   
    <!--default class associated with
    <default-class-ref class= "action. Useraction "/>
    
    <!--Not configured class action--
    <action name=" Login "method=" login ">
       <result name=" Success ">/index.jsp</result>
    </action>

</package>

Test access: Http://localhost:8080/projectName/user/login.action

Test result: Jumps to the index.jsp view.


7. <global-exception-mappings> Label

The label is used to configure the global (Package scope) exception view mappings. It has only one child element: <exception-mapping> usually requires two combinations.


7.1 <exception-mapping> Tags

The label is used to configure the exception view mappings for the action range.

<package name= "Default" extends= "Struts-default" > 
        
        <global-exception-mappings>
              < Exception-mapping result= "SqlException" exception= "java.sql.SQLException"/> <exception-mapping
              result= " Unknown "exception=" java.lang.Exception "/>
              <exception-mapping result=" error "exception=" Java.lang.RuntimeException "/> 
        </global-exception-mappings>
        
        <action name=" Unknown ">
           <result>/unKnown.jso</result>
        </action>
        
        <action name= "SqlException" >
           < result>/sqlexception.jso</result>
        </action>
</package>


8. <result-types> Label

This label is used to define how the result type corresponds to the implementation. The following is the result type for the default configuration of Struts-default:

<result-types> <result-type name= "Chain" class= "Com.opensymphony.xwork2.ActionChainResult"/> &lt ; Result-type name= "Dispatcher" class= "Org.apache.struts2.dispatcher.ServletDispatcherResult" default= "true"/> & Lt;result-type name= "Freemarker" class= "Org.apache.struts2.views.freemarker.FreemarkerResult"/> < Result-type name= "Httpheader" class= "Org.apache.struts2.dispatcher.HttpHeaderResult"/> <result-type name= "re Direct "class=" Org.apache.struts2.dispatcher.ServletRedirectResult "/> <result-type name=" redirectaction "CLA ss= "Org.apache.struts2.dispatcher.ServletActionRedirectResult"/> <result-type name= "stream" class= " Org.apache.struts2.dispatcher.StreamResult "/> <result-type name=" Velocity "class=" Org.apache.struts2.dispatcher.VelocityResult "/> <result-type name=" xslt "class=" Org.apache.struts2.views.xslt.XSLTResult "/> <result-type name=" plaintext "class=" Org.apache.Struts2.dispatcher.PlainTextResult "/> </result-types> 

Second, <constant> label

This label is used to configure constants.

<!--This property specifies the request suffix that requires Struts2 processing, and the default value of this property is action, which means that all requests that match *.action are handled by STRUTS2. If the user needs to specify multiple request suffixes, the multiple suffixes are separated by commas (,) and <constant name= "struts.action.extension" value= "Do,action"/> <!-- Specifies the default encoding set for the Web App, which by default is "iso8859-1" equivalent to calling HttpServletRequest Setcharacterencoding Method--<constant Name= " Struts.i18n.encoding "value=" UTF-8 "/> <!--set whether the browser caches static content, the default value is True (used in a production environment), the development phase is best off because we want each request to get the latest response from the server-- > <constant name= "Struts.serve.static.browserCache" value= "false"/> <!--when a struts profile is modified, the system reloads the file automatically, The default value is False (used in a production environment), and the development phase is best opened-<constant name= "Struts.configuration.xml.reload" value= "true"/> <!-- This property sets whether the Struts 2 app uses development mode. Development mode, so you can print out more detailed error messages-<constant name= "Struts.devmode" value= "true"/> <!--default View theme--< Constant Name= "Struts.ui.theme" value= "simple"/> <!--This property specifies that the action in Struts2 is created by the spring container--<constant name= "Struts.objectfactory" value= "Spring"/> <!--This property sets whether Struts2 supports dynamic method calls, and the default value for this property is true-and <conStant name= "Struts.enable.DynamicMethodInvocation" value= "false"/> <!--This property sets whether STRUTS2 allows the use of slashes in the action name, The default value for this property is false--<constant name= "Struts.enable.SlashesInActionNames" value= "true"/> <!-- This property specifies whether the expression syntax is allowed in the Struts2 label, and the default value of this property is true. --<constant name= "Struts.tag.altSyntax" value= "true"/> <!--This property specifies the configuration file that the Struts 2 framework loads by default, if you need to specify that multiple profiles be loaded by default, The file names of multiple profiles are separated by commas (,). The default value for this property is struts-default.xml,struts-plugin.xml,struts.xml--> <constant name= "Struts.configuration.files" Value= "Struts-default.xml,struts-plugin.xml,struts.xml"/> <!--upload file size limit, By default, Struts2 is not received, that is, STRUTS2 does not receive files that exceed his default size limit note: The size of the uploaded file is not the size of the single file but the size of the uploaded file and the total number of uploads <constant name= " Struts.multipart.maxSize "value=" 10701096 "/> <!--settings upload temporary files to save the directory, the default is Javax.servlet.context.tempdir-< Constant Name= "Struts.multipart.saveDir" value= "d:/" ></constant> <!--this property specifies handling multipart/ The framework of the MIME type (file upload) request for Form-data, which supports attribute values such as COS, Pell, and Jakarta, which correspond to the file upload frame, Pell upload, and common-file that use the COS, respectivelyUpload file Upload framework. The default value for this property is Jakarta. --<constant name= "Struts.multipart.parser" value= "Jakarta" ></constant>
Note: Struts2 load constants in the search order:

Struts-default.xml--Struts-plugin.xml--struts.xml---struts.properties
Note: If the same constant is defined in more than one configuration, the constant value in the latter configuration overrides the previously configured constant value.


Third, <include> label

This label is mainly used to split a struts.xml profile into multiple profiles for easy reading and management.

<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE struts public
"-//apache software foundation//dtd struts Configuration 2.0//en"
"/http Struts.apache.org/dtds/struts-2.0.dtd ">
<struts>
  <include file=" User.xml "/>
  < Include file= "Goods.xml"/>
</struts>


Iv. <bean> Labels



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.