默了默

          java hibernate struts2 spring
             :: 首頁 ::  ::  ::  :: 管理

          Struts2 的 struts.xml 配置中 namespace 的使用

          Posted on 2009-03-05 17:25 fd 閱讀(1269) 評論(0)  編輯  收藏 所屬分類: struts2
          Struts2 的 struts.xml 中是分 package 配置的,可以為 package 設置 namespace 屬性,如

          <package namespace="/secure"   ....>
              ......
          </package>

          如果沒有指定 namespace 屬性,默認 namespace 是 ""。使用 namespace 可以方便于按不同目的規劃對應用的訪問規則。比如不同 namespace 下配置了不同的攔截器就可以實現權限的控制,如 "/secure" 下已登陸用戶才能訪問,"/public" 下可公開訪問的。

          配置了 namespace 直接就是反應在訪問 URL 上,例如 namespace="/secure"  name="test" 的 action

           <package namespace="/secure"   ....>
                 <action name="test"  ....
          </package>

          訪問它的 URL 就是 http://ip:port/context/secure/test.action,那如果在 namespace "/secure" 下沒有 test action 會出現什么情況呢?Struts 還會嘗試在默認 namespace,即 "" 下找 test。

          再舉個例子,URL 是 http://ip:port/context/some/path/test.action 時,如果在 "/some/path" namespace 下找不到 test action,也是到 "" (default namespace) 下找 test action,但不會去 "/some" 下找的。

          用標簽 <s:url value="/secure/test.action"/>  對應頁面源文件是 /context/secure/test.action

          稍有麻的就是 <s:form action="/secure/test.action" .... 對應的源文件是 <form action="/context/secure/test.action" ...

          但是后臺會有警告:

          警告: No configuration found for the specified action: '/secure/test.action' in namespace: ''. Form action defaulting to 'action' attribute's literal value.

          Struts2 把 action 屬性值當整一個 Action Name 了,但這也不影響使用,這個 URL 正好能與 (package namespace) + (action name) 合上拍。

          但是對于使用了動態方法調用(struts.enable.DynamicMethodInvocation = true)就沒這么幸運了。很容易想當然的
           
          <s:form action="/secure/test!update.action" ....  生成的 HTML 源文件卻是 action="/TestStruts2/om/test"

          同時后臺的警告信息是:

          警告: No configuration found for the specified action: '/secure/test' in namespace: ''. Form action defaulting to 'action' attribute's literal value.

          很顯然對于這個 action="/TestStruts2/om/test",提交時是會得到 HTTP Status 404 - /context/secure/test  錯誤。

          正確的用法是 <s:action...> 也有一個 namespace 屬性,對了,就是

          <s:form namespace="/secure" action="test!login">  生成的 HTML 源文件是:<form action="/TestStruts2/om/test!login.action" ....>

          我們要的就是這個。

          如果不配置 namespace 屬性,我們能不能在訪問 action 時也用上目錄層次呢?可以,那是在 struts1 習慣的做法,配置 <action name="secure/test" ....> name 中使用斜杠,但在 Struts2 中 Action Name 中使用斜杠需要設置

          struts.enable.SlashesInActionNames=true                      默認為 false

          可是 Struts2 大概不贊同這種做法,力挺 namespace 的作用。

          對于上面使用了斜框的 Action Name,<s:form 中的寫法要用

          <s:form action="secure/test">                 生成 HTML 源文件:<form action="/context/secure/test.action" .....

          <s:form action="secure/test!update">            生成 HTML 源文件:<form action="/context/secure/test!login.action" .....



          上面的 action 后加不加 .action 無所謂,只是要保證 <s:form>  的 action 屬性一定要與 struts.xml 中的 <action> 的 name 匹配上,如果你自作多情的在前面加個斜杠,如寫成了
           
          <s:form action="/secure/test!update"> 、 <s:form action="/secure/test">  或者 <s:form action="/secure/test!update.action">   生成的 HTML 源文件就都成了:<form action="/context/secure/test" .....

          這也是從 Struts1 帶來的弊病,因為 Struts1 中 <html:form> action 屬性對應的是 <action> 的 path,而 Struts2 中 <s:form> 的 action 屬性對應的是 <action> 的 name;name 要完全匹配,path 可以加些層次。
          主站蜘蛛池模板: 徐州市| 荆州市| 龙泉市| 西充县| 南溪县| 吉水县| 彰化县| 丹巴县| 阿巴嘎旗| 浦北县| 柏乡县| 麟游县| 汪清县| 雷波县| 阳泉市| 衡南县| 同德县| 梅河口市| 鹤峰县| 墨竹工卡县| 铅山县| 永登县| 馆陶县| 盘锦市| 瑞金市| 新泰市| 靖江市| 甘南县| 台北市| 个旧市| 北碚区| 溧水县| 仲巴县| 潼南县| 吉安市| 弥渡县| 木兰县| 郁南县| 卢龙县| 新野县| 云南省|