What is the default package name for Struts in struts.xml?

冷暖自知 提交于 2019-11-30 09:46:03

问题


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.

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

回答1:


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.




回答2:


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.




回答3:


yes u can include both in struts.xml..like this here

<package name="default" extends="json-default,hibernate-default" namespace= "/">


来源:https://stackoverflow.com/questions/16621970/what-is-the-default-package-name-for-struts-in-struts-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!