) Struts2 Convention Zero Configuration

Source: Internet
Author: User
Starting from struts2.1, struts2 no longer recommends codebehind as the zero-configuration plug-in, but instead uses the Convention plug-in to support Zero Configuration. Compared with codebehind, the Convention plug-in is more thorough and completely discards configuration information, not only does it need to use struts. the XML file does not even need to use annotation for configuration, but is automatically configured by struts2 according to the Conventions.

How to Use Convention

1. Copy the struts-Convention-plugin-2.1.6.jar file to the WEB-INF/lib path

2. For the Convention plug-in, it will automatically search for all Java classes located under the action, actions, struts, and struts2 packages. The Convention plug-in will treat the following two Java classes as actions:

1) All Java classes that implement COM. opensymphony. xwork2.action

2) All Java classes whose names end with actions

3. The Convention plug-in can also set the following three constants:

1) struts. Convention. Exclude. packges: Specifies which packages are not scanned for Java classes. The Java classes in these package structures are not automatically mapped to actions;

2) struts. Convention. Package. locators: the Convention plug-in uses the package specified by this constant as the root package for Action search. For the actions. Fore. loginaction class, it should be mapped to/Fore/login according to the Conventions. If this constant is set to fore, the action will be mapped to/login.

3) struts. Convention. Action. Packages: the Convention plug-in uses the specified constant package as the root package to search for the action class. In addition to scanning the classes of the four packages of action, actions, struts, and struts2, the Convention plug-in also scans one or more packages specified by the constant, and tries to find the action class from it.

Note: struts. Convention. Package. locators and Struts. Convention. Action. packages have subtle functions. Developers must be careful when using these two constants.

For example, the namespace mapped to the package where the action is located is as follows:

Com. Fun. Actions. loginaction ing/

Com. Fun. Actions. myoffice. carinfoaction ing to/myoffice

Com. Fun. Struts. myoffice. entinfoaction ing to/myofiice

4. When ing the action name, follow the following two steps:

1) if the action class name contains the action suffix, remove the action Suffix of the action class name. Otherwise, no processing is performed.

2) convert the camper of the action Class Name (the first letter of each word is written in uppercase and the other letters are written in lowercase) into a line-to-line (all letters are written in lowercase and words are separated from a line-to-line)

For example, the name attribute of the acion mapped by loginaction is login, the name attribute of the Action mapped by getbooks is get-books, and the name attribute of the Action mapped by addemployeeaction is add-employee.

5. By default. Convention always locates physical resources under the WEB-INF/content path of the Web application, and the Convention for locating resources is: actionrul + resultcode + suffix. When a logical view cannot find the corresponding resource, convention automatically uses actionurl + suffix as the physical view resource.

For example, when actions. Fore. loginaction returns the success string, Convention takes precedence over using the WEB-INF under the login-success.jsp/content/Fore directory as the view resource. If the file cannot be found, login. jsp can also be used as the corresponding resource. If an input string is returned, Convention looks for the WEB-INF in the login-input.jsp/content/Fore

6. To view the effect of actions and other resources in the struts2 application, struts2 provides the config browser plug-in. Use method to copy the struts2-config-browser-plugin-2.1.6.jar file to the WEB-INF \ lib directory of the struts2 application.

Open the home page: http: // localhost: 8080/Application name/config-Browser/actionnames. Action. Here you can see the home page of the config browser plug-in.

Note: No matter whether the developer uses the struts. xml file for configuration, you can see the struts configuration information.

7. Action chain conventions

If you want an action to be processed, instead of entering an attempt page, the action chain is formed by another action. With the Convention plug-in, you only need to follow the following three conventions.

1) the logical view string returned by the first action does not have the corresponding view Resource

2) The second action and the first action are in the same package.

3) the URL of the second action is firstactionurl + resultcode.

For example: the first action is oneaction, there is a method returned as "two", then you have to ensure that the WEB-INF/content/under no one. JSP or one-two.jsp for the second action it should be named onetwoaction, and the corresponding URL should be: "one-two.action"

Note: Since the Convention plug-in dynamically generates ing based on the action and JSP pages, both the acion change and the JSP page change require the Convention plug-in to reload the ing. You only need to configure the following two constants for the struts2 application.

<! -- Configure struts2 to apply to development mode --!>

<Constant name = "struts. devmode" value = "true"/>

<! -Configure the Convention plug-in to automatically reload the ing --!>

<Constant name = "struts. Convention. classes. Reload" value = "true"/>

Several important constants

Struts. Convention. Action. disablejarscanning --- Search action from the package

Struts. Convention. Action. Package -------- the Convention plug-in uses this constant to specify the package as the root package

Struts. Convention. Result. Path --- set the root path of the view resource for the Convention plug-in. Default Value:/WEB-INF/Content

Struts. Convention. Result. flatlayout-if it is false, you can try to place the directory corresponding to the action without placing it in the WEB-INF/Content

8. annotation of Convention

1) The two annotation related to action are @ action and @ actions.

2) A value attribute can be specified in @ action. Similar to specifying the <action name = ""/> Attribute Value

3) @ action can also specify a Params attribute, which is a string array used for the parameter name and parameter value specified by the acion. Params attributes must comply with the following format: {"name1", "value1", "name2", "value2 "}

4) @ actions is also used to modify the methods in the action class and map the method to multiple URLs. @ actions is used to organize multiple @ actions. therefore, it can map a method to multiple logical actions.

For example:

Package com. Fun. actions;

Import org. Apache. struts2.convention. annotation. Action;

Import org. Apache. struts2.convention. annotation. actions;

Import com. Fun. Service. loginservice;

Import com. opensymphony. xwork2.actionsupport;

Public class loginaction extends actionsupport {

Private string STR;



@ Actions ({

@ Action (value = "login1", Params = {"str", "this is already injected! "}),

@ Action (value = "login2 ")

})

Public String login (){

Return "str ";

}



@ Action (value = "ggg ")

Public String ABC (){

Return "ABC ";

}

Public String getstr (){

Return STR;

}

Public void setstr (string Str ){

This. Str = STR;

}

}

We can access it through/login1.action. During access, the STR attribute has a value, which is str = "this is already injected !" The returned view is the login1-str.jsp

When we use/login2.action for access, the STR value is null. The returned view is the login2-str.jsp; while we call the ABC () method through/ggg. Action, the returned view is/ggg-abc.jsp.

9. annotation related to result Configuration

1) @ resultpath @ result and results

2) @ results is used to organize multiple @ results. Therefore, it only needs to specify a value attribute value. The value attribute value is multiple @ results.

3) @ result is equivalent to the <result/> element in the Struts. xml file. To use @ result, you must specify a name attribute, which is equivalent to <result name = ""/>. In addition, it has several optional attributes.

☆Type is equivalent to <result type = ""/> specifying the type of the returned view Resource

☆Location is equivalent to <result> ..... </Result> specifies the actual view location.

☆Params: This attribute is equivalent to multiple <Param ../> sub-elements in the <result/> element. It is used to specify the parameter value for the result. This attribute must meet the {"name1", "value1", "name2", "value2"} format

4) @ result has the following usage:

1. Action-level result ing: Combine multiple @ actions with @ actions to modify the action class. This result ing applies to all methods in the action.

2. Method-level result ing: multiple @ results are combined into an array and used as the results attribute value of @ action. This result ing is only valid for the modified method.

5) @ resultpath is used to modify the package and action class and change the root path of the physical view resource corresponding to the modified action. For example, by default, Convention will find physical view resources in the WEB-INF/content path, once we use @ resultpath ("/ABC") to modify this action, the system will return to the ABC directory to find the physical view resource.

10. annotation related to packages and namespaces:

@ Namespace: modifies the action class or its package. This annotation specifies a value attribute value to specify the namespace of the modified action.

@ Namespaces: modifies the action class or its package, used to combine multiple @ namespace

@ Parentpackage: Specifies the parent package of the modified action package.

11. annotation related to Exception Handling

@ Exceptionmappings is used to organize multiple @ exceptionmapping. Therefore, you only need to specify a value attribute value. The value attribute value is multiple @ exceptionmapping.

@ Exceptionmapping is used to define the correspondence between the exception class and the physical view, which is also equivalent to struts. <exception-mapping .. /> when using an element, pay attention to the following two attributes:

Exception: Specifies the exception class.

Result: Specifies the logical view.

@ Exceptionmpping:

Action-level exception definition: Combine multiple @ exceptionmappings and modify the action classes after @ exceptionmapping. This exception definition is valid for all methods in action.

Method-level exception definition: An array consisting of multiple @ exceptionmapping is used as the value of @ Action's exceptionmappings attribute. This exception definition is only valid for the modified method.

12. annotation related to interceptor Configuration

The annotation configured with the interceptor includes @ interceptorref, @ interceptorrefs, and @ defaultinterceptorref.

@ Interceptorrefs is used to organize multiple @ interceptorref. Therefore, it only needs to specify one value attribute value. The value attribute value is multiple @ interceptorref.

@ Interceptorref refers to lanjieq or interceptor stack for a specified action. This is equivalent to the <Interceptor-ref.../> sub-element in strut. XML that is located inside the <action ../> element. When @ interceptorrefannotation is used, a value attribute must be set to specify the name of the referenced interceptor or interceptor stack. It is equivalent to the role of the name attribute in the sub-element <Interceptor-ref ../>.

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/vincent080928/archive/2010/03/20/5395492.aspx

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.