STRUTS2 0 Configuration by convention (one)

Source: Internet
Author: User
Tags constant naming convention

As Struts2 continues to escalate, struts begins using Convention-plugin instead of Codebehind-plugin to implement the 0 configuration of struts. The so-called 0 configuration is not required by any configuration, but rather the way the convention is larger than the configuration.

In the Web development process, we no longer need to configure any information in Struts.xml, based on the default conventions of Convention-plugin.

First, let's take a look at the HelloWorld of struts "0 configuration" based on the Convention-plugin implementation.


Struts.xml

<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE struts public
	"-//apache software foundation//dtd struts Configuration 2.3//en"
	"/http Struts.apache.org/dtds/struts-2.3.dtd ">
<struts>
	<package name=" Default "namespace="/" extends= "Struts-default"/>
</struts>

My HelloWorld.


Package com.example.actions;

Import Com.opensymphony.xwork2.ActionSupport;

public class HelloWorld extends Actionsupport {

    private static final long Serialversionuid = -6662538623355755690l;
  private String message;

    Public String GetMessage () {
        return message;
    }

    Public String Execute () {
        if (System.currenttimemillis ()% 2 = = 0) {
            message = "It's 0";
            return "zero";
        }

        message = "It ' s 1";
        return SUCCESS;
    }
}

For a simple HelloWorld, the configuration of the struts.xml is simply streamlined to the extreme. No need to write a bunch of <action> configurations. And all we need to do is join a struts2-convention-plugin-x.x.x.x.jar.

No configuration is required in HelloWorld because Struts2-convention-plugin makes the following series of conventions. Action location by package naming conventions Com.example. Action. Main Action com.example. Actions.products.Display (implements Com.opensymphony.xwork2.Action) Com.example. Struts.company.details.ShowCompanyDetails Action com.example. Struts2.company.details.ShowCompanyDetailsAction
Convention plugin By default all packages containing action,actions,struts,struts2 in the package path are searched as the path to the action class. You can modify this configuration by setting the Struts.convention.package.locators property. Convention plugin default configuration is as follows:

<constant name= "struts.convention.package.locators" value= "Action,actions,struts,struts2"/>

Convention plugin finds classes in the packages and their sub-packages that have the suffix action, or com.opensymphony.xwork2.Action implemented. You can do this by setting

Struts.convention.action.suffix property to modify this configuration. Convention plugin default configuration is as follows:

<constant name= "Struts.convention.action.suffix" value= "action"/>

Result (JSP, Freemarker, etc) location by naming conventions

When you enter Http://localhost:8080/hello-world access in the browser, the page jumps to web-inf/content/hello-world.jsp.
By default, convention plugin assumes that all results are stored under web-inf/content/. Can be modified by setting Struts.convention.result.path. Convention plugin default configuration is as follows:

<constant name= "Struts.convention.result.path" value= "/web-inf/content/"/>

Convention plugin even if the corresponding action is not found, the corresponding result (/hello-world.jsp) can be found through the action URL (here is/hello-world).
Class name to URL naming convention Com.example.actions.MainAction-/main
Com.example.actions.products.Display-/products/display

Com.example.struts.company.details.ShowCompanyDetailsAction-/company/details/show-company-details

Take the com.example.struts.company.details.ShowCompanyDetailsAction corresponding action URL as an example, convention Plugin will convert the com.example.struts.company.details.ShowCompanyDetailsAction after the struts of the child package into a namespace "/company/details/", Then the "Action" suffix in the showcompanydetailsaction is removed, and the camel-named uppercase is lowercase, and a "-" (Of course "-" and Convention plugin default) is added to the final form/company/ Details/show-company-details. This is the "contract" between the class name and the action URL.


Package name to namespace Convention Com.example.actions.MainAction
Com.example.actions.products.Display-/products

Com.example.struts.company.details.ShowCompanyDetailsAction-/company/details

Convention plugin default to actions, action, struts, struts2 as the root package, the Com.example.actions.MainAction namespace is "/", The Com.example.actions.products.Display namespace is "/products".


It is these "conventions" that make our HelloWorld possible to achieve zero configuration.

The actual development process if only these conventions, may not fully meet the needs of the actual project. Fortunately, the configuration of the Convention plugin is quite flexible.

In the future, you will continue to learn the flexible annotation-based configuration.

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.