Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

Namespaces

The namespace attribute allows you to segregate subdivides 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 applicationlogical modules, each with its own identifying prefix. Namespaces avoid conflicts between action names. Each namespace can have its own "menu" or "help" action, each with its own implementation. While the prefix appears in the browser URI, the tags are "namespace aware", so the namespace prefix does not need to be embedded in forms and links.

Tip

Struts 2 Namespaces are the equivalent of Struts Action 1 modules, but more convenient and flexible.

Default Namespace

The default namespace , which is "" ( - an empty string) . The default namespace is used as a "catch-all" namespace, so if . If an action configuration is not found in a specified namespace, the default namespace will is also be searched. This allows you The local/global strategy allows an application to have global action configurations outside of the action element "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.

Root Namesapce

.

The namespace prefix can be registered with Java declarative security, to ensure only authorized users can access the actions in a given namespace.

Root Namespace

A root namespace ("/") is also supported. The root is Root namespace, which is "/" is also allowed in WebWork. It will be the namespace when a request directly under the context path is received. As with other namespacenamespaces, it will fall back to the default ("") namespace if no such a local action alias is not found in it.

Namespace

...

Example

Code Block
xml
xml
<package name="default">
    <action name="foo" class="mypackage.simpleAction>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="mypackage1" namespace="/">
    <action name="moo" class="mypackage.simpleActtionsimpleAction">
        <result name="success" type="dispatcher">moo.jsp</result>
    </action>
</package>

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

...

How the Code Works

If a request for /barspace/bar.action is made, 'the /barspace' namespace is searched and if it is found for the bar action. If found, the bar action is executed, else it will fall back to the default namespace. In this example bar alias do exists the Namespace Example, the bar action does exist in the '/barspace' namespace, so it the bar action will get be executed, and if "success" is returned, the request will be forwarded to bar2.jsp.Note:

Tip
titleFalling Back to Foo

If a request is made to /barspace/foo.action, the

...

namespace /barspace will be

...

checked for action foo. If

...

a local action is not found,

...

the default namespace is checked.

...

In the Namespace Example, there is no action foo in the namespace /barspace, therefore the default will be

...

checked and /foo.action will be executed.

If In the Namespace Example, if a request for / moo.action is made, the root namespace ('/') is searched for 'a moo' action alias, if it ; if a root action is not found it will fall back to trying to find it in , the default namespace is checked. In this examplecase, the moo action alias does exists exist and hence will be executed. Upon sucesssuccess, the request will get would be forwarded to bar2.jsp.Note:

Tip
titleGetting to the Root

If a request is made

...

for /foo.action

...

,

...

the root /

...

namespace will be

...

checked. If foo is found

...

, the root action will be

...

selected. Otherwise, the framework will check the default namespace

...

. In

...

the Namespace Example, the foo action

...

does not exist in the

...

root namespace,

...

so the default namespace is checked, and

...

the default foo

...

action is executed.

Note
titleNamespaces are not a path!

Namespace are not hierarchical like a file system path. There is one namespace

...

level. For example if the

...

URL /barspace/myspace/bar.action

...

is requested,

...

the framework will first look for namespace

...

/barspace/myspace

...

. If the action does not exist

...

at /barspace/myspace, the search will immediately fall back to the default namespace

...

"". The framework will not parse the namespace into a series of "folders". In the Namespace Example, the bar action in the default

...

namespace would be

...

selected.