Struts 2 Reading Notes-Struts action configuration

Source: Internet
Author: User

when action after processing the class, in struts. XML Configure the file action . Configure action the purpose is to struts 2 know which action request to process. That is, to complete the user request and action .

in action You can specify result types , exception processor and interceptor, but only action name attributes must be specified, the attribute stone references the unique identifier of the package. Other attributes can also be found in package defined within the range for this package all action reference

1. Package and namespace

Struts 2Organize and manage packagesAction. DefinitionActionUse<Package.../>Under<Action.../>Sub-RMB is always complete.

InStruts. xmlFile,<Package.../>Element is used to define the package configuration. Each<Package.../>The element defines a package configuration. It has the following attributes:

struts 2 The namespace is mainly used to process the same Web the application contains the same name action . Because struts 2 Manage as a package action , therefore, the same package cannot contain action .

If you configure<Package.../>Not SpecifiedNamespaceProperties, allActionAll are in the default package space.

There areStruts. xmlConfiguration

 

 1 < Package Name = "mystruts1" Extends = "Struts-Default"> 2 <Action name = "* Action" Class = "Com. Action. loginaction"> 3   ......  4 </Action> 5 </ Package > 6 <! -- The configuration name is mystruts2. The package continues with the default struts 2 package, and the namespace is/book. --> 7 < Package Name = "mystruts2" Extends = "Struts-Default" namespace = "/book"> 8 <Action name = "getbooks" Class = "Com. Action. getbooksaction"> 9   ......  10 </Action> 11 </ Package >

 

When a package has a namespace specified, allActionProcessedURLIt should be a namespace.+ ActionName. For example:

Http: // localhost: 8080/struts_05/book/getbooks. Action

Struts_05Is the application name;BookYesActionThe namespace corresponding to the package, andGetbooksYesActionName

 

2. ActionBasic Configuration

In addition to the above configuration, we also need to specifyClassAttribute. This attribute specifiesAction. If this attribute is not specified, the system usesActionsupportClass.

We are configuring<Action.../>You can also specifyMethodAttribute. This attribute specifiesActionCall the specified method.

<Action name = "login"Class= "Com. App. Action. loginaction" method = "login"/>

 

The above instance definesLoginOfAction,MethodProperty specifies the method to process user requestsLogin

You do not need to specifyMethodAttribute. The system callsExecuteMethod to process user requests.

There should be an identical method signature for processing user requests: The method parameter list is empty, and the method return value isString. 

 1   <! --  The configuration name is mystruts1. The processing class loginaction uses the execute method by default to process requests.  -->  2       <  Package  Name  = "Mystruts1"  Extends  = "Struts-Default"  > 3           <  Action  Name  = "* Action"  Class  = "Com. Action. loginaction"   >  4   ......  5           </  Action  >  6       </  Package >  7       <! --  The configuration name is mystruts2, the processing class is getbooksaction, and the processing method is getbooks.  -->  8       <  Package  Name  = "Mystruts2"  Extends  = "Struts-Default"   >  9           <  Action Name  = "Getbooks"  Class  = "Com. Action. getbooksaction"  Method  = "Getbooks"  >  10   ......  11           </  Action  >  12       </  Package  >  

 

 

3. Configuration processing result

ActionIt is just a logic controller that does not directly respond to viewers. Therefore,ActionAfter processing user requests,ActionYou need to present the specified resource to the user. ThereforeActionYou should configure the correspondence between logical attempts and physical view resources.

Result configuration isStruts 2: WhenActionAfter the user request processing is complete, what is the next step of the system, and what physical view resource should be called by the system next to display the processing result.

Configuration<Result.../>The following two attributes must be specified for an element:

<Result.../>The element configuration is as follows:

1 <ActionName= "Login"Class= "Com. App. Action. loginaction>2 <! -- Configure result for the logic view of success. Type attribute specifies the result type ---> <result name ="Success "Type= "Dispatcher">/Welcome. jsp</Result>3</Action>

 

 

Generally,Struts 2The specified result type can be omitted.

 
<ActionName= "Login"Class= "Com. App. Action. loginaction> <! -- Configure result for the logic view of success. Omit type ---> <result name ="Success">/Welcome. jsp</Result> </Action> 

 

Struts 2The logical attempt name can also be omitted.NameAttribute

 
1 <ActionName= "Login"Class= "Com. App. Action. loginaction>2 <! -- Configure result for the logic view of success. Omit type, name --->3 <Result>/welcome. jsp </result>4 </Action>

 

If we omit <result.../>ElementNameAttribute, the system will use the defaultNameAttribute Value, defaultNameThe property value isSuccess.

 

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.