Struts2 struts2-convention-plugin (Zero Configuration) Basics

Source: Internet
Author: User

Zero Configuration is not configured. Instead, the configuration is greatly reduced by means of conventions greater than the configuration. A large number of conventions are used to schedule page jumps.

I. 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 the com. opensymphony. xwork2.action Interface
(2) All Java classes whose names end with actions

3. 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.

The following is the actiing between actiong and the result template:

URL Result
File that cocould match Result type
/Hello Success /WEB-INF/content/Hello. jsp Dispatcher
/Hello Success /WEB-INF/content/hello-success.htm Dispatcher
/Hello Success /WEB-INF/content/Hello. FTL Freemarker
/Hello-world Input /WEB-INF/content/hello-world-input.vm Velocity
/Test1/Test2/Hello Error /WEB-INF/content/test/Test2/hello-error.html Dispatcher

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. 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

5. 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 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"


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

Note:

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.

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.