Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Namespaces

Namespaces are an important concept that help manage large applications with hundreds of actions. Within the Web, namespaces are managed by paths. When a request comes in, the ServletDispatcher will extract the action and namespace to determine the appropriate action to execute. For more information regarding namespaces, please refer to the XWork documentation regarding namespaces (see Configuration). Below gives a short example of using namespaces with WebWork.

Note: If a request is made to /barspace/foo.action, the action foo will be searched for in a namespace of /barspace. If the action is not found, the action will then be searched for in the default namespace. Unless specified, the default namespace will be "". In our example below, their is no action foo in the namespace /barspace, therefore the default will be searched and /foo.action will be executed.The namespace attribute allows you to segregate action configurations into namespaces, so that you may use the same action alias in more than one namespace with different classes, parameters, etc. This is in contrast to Webwork 1.x, where all action names and aliases were global and could not be re-used in an application. The default namespace, which is "" (an empty string) is used as a "catch-all" namespace, so if an action configuration is not found in a specified namespace, the default namespace will also be searched. This allows you to have global action configurations outside of the "extends" hierarchy, as well as to allow the previous Webwork 1.x behavior by not specifying namespaces. It is also intended that the namespace functionality can be used for security, for instance by having the path before the action name be used as the namespace by the Webwork 2.0 ServletDispatcher, thus allowing the use of J2EE declarative security on paths to be easily implemented and maintained.

Namespace example

Code Block
<package name="default">

    <action name="foo" class="mypackage.simpleAction>
        <result name="success" type="dispatcher">greeting.jsp</result>
    </action>
    <action name="bar" class="mypackage.simpleAction"> 
        <result name="success" type="dispatcher">bar1.jsp</result> 
    </action> 

</package>


<package name="barmypackage" namespace="/barspace"> 

    <action name="bar" class="mypackage.simpleAction"> 
        <result name="success" type="dispatcher">bar2.jsp</result> 
    </action> 

</package>

If a request for /barspace/bar.action is made, then the package named mypackage is searched and the bar action is executed. If success is returned, then bar2.jsp is displayed.

Note: If a request is made to /barspace/foo.action, the action foo will be searched for in a namespace of /barspace. If the action is not found, the action will then be searched for in the default namespace. Unless specified, the default namespace will be "". In our example above, their is no action foo in the namespace /barspace, therefore the default will be searched and /foo.action will be executed.