STRUTS2 Convention Plugin Chinese DocumentsCategory: Sturts2 2013-04-16 13:54 190 people Read reviews (0) Favorite reports
Directory (?) [+] struts2convention Plugin Chinese document (i)
Introduction
Starting with the struts2.1 version, the Convention plugin implements the plugin 0 configuration as replacement replacement Codebehind Struts2.
• Package naming habits To specify the action location
• Naming habit-setting results (support Jsp,freemarker, etc.) path
• Convention conversions for class names to URLs
• Contract conversions for package name to namespace (namespace)
• Follow the SEO specification's link address (i.e., use my-action to replace myaction)
• Annotation-based action name
• Annotation-based interceptors (interceptor)
• Annotation-based namespaces (Nameespace)
• Annotation-based xwork package
• Default action and default results (for example:/products will try to find com.example.actions.Products or com.example.actions.products.Index to process)
There is no need to configure Convention to use some of the convention,convention's constraints can be controlled by configuration properties, or you can override the methods in the class to reach the extended target.
installation
With the Convention plugin, you'll need to put its jar file in your app's Web-inf/lib directory, or you can add a bread dependency in your Macen project's pom file.
1. <dependency>
2. <groupId>org.apache.struts</groupId>
3. <artifactId>struts2-convention-plugin</artifactId>
4. <version>2.1.6</version>
5. </dependency>
conversion based on Codebehind project to Convention
jump to this page to see the changes and tips you need to change
If you want to combine convention plugins in your system, use rest. You need to add the following configuration to your project's Struts.xml
1. <constant name= "Struts.convention.action.suffix" value= "Controller"/>
2. <constant name= "Struts.convention.action.mapAllMatches" value= "true"/>
3. <constant name= "Struts.convention.default.parent.package" value= "Rest-default"/>
Hello World
So far, you've added support for convention plug-ins in your project, first we start with a very simple example. In this example, we will demonstrate access to a fixed action based on the access URL, by default, all of the resulting pages are stored under Web-inf/content, convention. You can also set the value of Struts.convention.result.path to a new path in the Struts properties file. The end of the path "/" is unnecessary and convention will be processed automatically. The following is the JSP file for this example
web-inf/content/hello-world.jsp
1.
2. <body>
3. Hello world!
4. </body>
5.
Launch Tomcat or other JEE containers you use, and in the browser access Http://localhost:8080/hello-world, you can see the following information:
Hello world!
This indicates that the Convention has been able to function properly and found the results. Even in the absence of an action, convention will find the resulting page based on the URL rules.
Code behind Hello world
We continue to extend this example and add the code implementation class. In order to implement this function, we first need convention to find our action class correctly, by default, convention will find the Com.opensymphony.xwork2.Action implementation class, or the class that ends with Action in the package Action
Convention uses the following methods to search the Classpath, first, convention will look for the package name from the root bundle containing any packages of struts, struts2, action or actions. Next, convention the implementation of the package that was found in the previous step and the com.opensymphony.xwork2.Action in its sub-package, and the class that ends with Action, the class that Convention looks for
1. Com.example.actions.MainAction
2. Com.example.actions.products.Display (Implements Com.opensymphony.xwork2.Action)
3. Com.example.struts.company.details.ShowCompanyDetailsAction
4. Com.example.actions.MainAction
5. Com.example.actions.products.Display (Implements Com.opensymphony.xwork2.Action)
6. Com.example.struts.company.details.ShowCompanyDetailsAction
Each of the Convention found action corresponds to an explicit URL address, which is based on the package name and the action class name.
First convention the Path (namespace) in the corresponding URL from the root package and the package name in which the class resides, the following is the URL determined by the package namespace
1. Com.example.actions.MainAction/
2. Com.example.actions.products.Display-/products
3. Com.example.struts.company.details.ShowCompanyDetailsAction-/company/details
Next convention need to determine the specific resource portion of the URL. The first step cancels the action in the class name and divides the other parts of the class name with "-", and converts the first letter of each division to lowercase. As shown below
1. Com.example.actions.MainAction-/main
2. Com.example.actions.products.Display-/products/display
3. Com.example.struts.company.details.ShowCompanyDetailsAction-/company/details/show-company-details
You can also configure Struts.convention.exclude.packages to tell convention to ignore certain packages, or to set struts.convention.package.locators Used to change the Convention default root packages, and finally you can set the struts.convention.action.packages. To let convention only search for action under a specific package
The following is the implementation code for the Action class:
1. Package com.example.actions;
2.
3. Import Com.opensymphony.xwork2.ActionSupport;
4.
5. Public class HelloWorld extends Actionsupport {
6. Private String message;
7.
8. Public String GetMessage () {
9. Return message;
10.}
11.
Public String execute () {
Message = "Hello world!";