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
?
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.
The package name is completely independent of your code artifacts.
The struts-default package is... the Struts 2 default package. It defines default result types, interceptors, interceptor stacks, and a few other defaults.. You do not need to extend it, but if you don't, you have a fair amount of manual work to do in your own package.
I believe you should put your action classes in a package, and can think of no reasonable argument against the best practice of doing so. Again, your action class packages are completely independent of package names, or what (if any) the package extends.
yes u can include both in struts.xml..like this here
<package name="default" extends="json-default,hibernate-default" namespace= "/">