3

In Struts 2 I am seeing the root namespace giving the same behaviour as the root namespace, ie acting as a "catch all". I need to restrict actions in my app to be accessible from only one URL, including those without a namespace in the URL. My understanding is using root namespace should do this but I haven't seen it work.

I can reproduce this problem with the Struts 2 tutorial's HelloWorld example for Eclipse available here.

The struts.xml contains

<package name="basicstruts2" extends="struts-default">
    ...
    <action name="index">
        <result>/index.jsp</result>
    </action>
    ...
</package>

So both the following links show the index.jsp result

  • localhost:8080/Basic_Struts2_Ant/index.action
  • localhost:8080/Basic_Struts2_Ant/foo/index.action

Good so far.

If I change the struts.xml to

<package name="basicstruts2" namespace="/foo" extends="struts-default">
  • localhost:8080/Basic_Struts2_Ant/index.action fails with "There is no Action mapped for namespace / and action name index."
  • localhost:8080/Basic_Struts2_Ant/foo/index.action shows index.jsp

Also good.

Now if I change struts xml to say

<package name="basicstruts2" namespace="/" extends="struts-default">

Both the following links show the index.jsp result (same as when no namespace is defined)

  • localhost:8080/Basic_Struts2_Ant/index.action
  • localhost:8080/Basic_Struts2_Ant/foo/index.action

If I have understood the namespace documentation correctly I would expect localhost:8080/Basic_Struts2_Ant/foo/index.action to fail with "There is no Action mapped for namespace /foo and action name index."

I have also tried other variants, replacing "foo" with "alksdja" etc, to eliminate browser caching as a possibility.

Have I misunderstood what the root namespace does? And how to disable /foo/index.action from working while allowing /index.action?

Denees
  • 9,100
  • 13
  • 47
  • 76
Sandio
  • 31
  • 1
  • 3
  • See my answer to [this question](http://stackoverflow.com/a/8424913/438992) and see if it works for you. – Dave Newton Jan 16 '12 at 15:22
  • Now I'm reading about all the settings it seems either struts.mapper.alwaysSelectFullNamespace or struts.enable.SlashesInActionNames would fix it depending on what you want the extra stuff in the middle of the URL to be associated with. – Sandio Jan 16 '12 at 16:34

2 Answers2

2

You need to define both default package and root package. The Logic is , if some action can not be found in your package which namespace is "/foo" , struts2 will search the same action name in default package. If default package not exist, it will search root package. So what you need is define both default package and root package.

<package name="basicstruts2_default" extends="struts-default">
  <!--put actions that share in all namespace and global settings-->
</package>
<package name="basicstruts2_root" namespace="/"  extends="basicstruts2_default">
     <action name="index">
        <result>/index.jsp</result>
    </action>
</package>
<package name="basicstruts2_foo" namespace="/foo" extends="basicstruts2_default">
 <action name="show">
   <result>/foo/show.jsp</result>
 </action>
</package>
0

I faced the same. I have tested by defining both default and root namespace. It works like: If action cannot be found with the specified namespace it search root namespace first and search default namespace only after that action does not exit in the root namespace. It is different from struts2 documentation I have read. It may be coz of struts2 version? I used struts 2.3.16.

Lin
  • 1