Java – the default package name for Struts in struts.xml


I created a new class called RegesterAction but I didn't keep this class in any package. How can I configure this class in the struts.xml?

Below is the struts.xml file but I'm unable to understand the attribute values "default" and struts-default.

    <package name="default" extends="struts-default">
        <action name="*Register" method="{1}" class="RegisterAction">
            <result name="populate">/register.jsp</result>

Best Solution

Packages in Struts2 slightly different that the packages in the Java but usage is the same to separate the code into different modules used with their own namespace, so it's not clashes with action names. To better understand the concept of packages and namespaces you may look at Struts 2 Namespace configuration example and explanation tutorial.

If you keep classes in the source folder and it doesn't belong to the package sooner or later you will end up with the problem naming new classes and heavy code management.

The Struts2 framework has the package named struts-default that contains all necessary things to be able to use in your application configuration via extending your packages by putting this name into extends attribute of your <package> tag. You could also include other default packages from other plugins, i.e. json-default, etc.

As long as namespace is related to the package name you could compare the package names used by the Java language and package namespaces in the Struts2 framework and you may find it similar, however they are absolutely different. For example the default namespace used by the Struts2 is "" that is used when the action or result name not found in the other packages/namespaces. This is almost equivalent if you use namespace="/" which is the package root of the application.

It doesn't matter how do you name your root package with default of root but not with the name already used by the framework i.e. struts-default, if you omit the namespace attribute then the default namespace will be used. That is similar to the Java, if you omit the package declaration then default package is used.

The concept default might spread through other terms, for example there's a default action, default result, default interceptor stack, etc. The framework is also utilizes name default for default implementations of the known interfaces, and so on. There's no a singular meaning the word default. Generally if you omit something with the code then framework will use it's default settings.