默了默

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

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

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

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

          如果沒有指定 namespace 屬性,默認(rèn) namespace 是 ""。使用 namespace 可以方便于按不同目的規(guī)劃對(duì)應(yīng)用的訪問規(guī)則。比如不同 namespace 下配置了不同的攔截器就可以實(shí)現(xiàn)權(quán)限的控制,如 "/secure" 下已登陸用戶才能訪問,"/public" 下可公開訪問的。

          配置了 namespace 直接就是反應(yīng)在訪問 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 會(huì)出現(xiàn)什么情況呢?Struts 還會(huì)嘗試在默認(rèn) namespace,即 "" 下找 test。

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

          用標(biāo)簽 <s:url value="/secure/test.action"/>  對(duì)應(yīng)頁面源文件是 /context/secure/test.action

          稍有麻的就是 <s:form action="/secure/test.action" .... 對(duì)應(yīng)的源文件是 <form action="/context/secure/test.action" ...

          但是后臺(tái)會(huì)有警告:

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

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

          但是對(duì)于使用了動(dòng)態(tài)方法調(diào)用(struts.enable.DynamicMethodInvocation = true)就沒這么幸運(yùn)了。很容易想當(dāng)然的
           
          <s:form action="/secure/test!update.action" ....  生成的 HTML 源文件卻是 action="/TestStruts2/om/test"

          同時(shí)后臺(tái)的警告信息是:

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

          很顯然對(duì)于這個(gè) action="/TestStruts2/om/test",提交時(shí)是會(huì)得到 HTTP Status 404 - /context/secure/test  錯(cuò)誤。

          正確的用法是 <s:action...> 也有一個(gè) namespace 屬性,對(duì)了,就是

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

          我們要的就是這個(gè)。

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

          struts.enable.SlashesInActionNames=true                      默認(rèn)為 false

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

          對(duì)于上面使用了斜框的 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 匹配上,如果你自作多情的在前面加個(gè)斜杠,如寫成了
           
          <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 帶來的弊病,因?yàn)?Struts1 中 <html:form> action 屬性對(duì)應(yīng)的是 <action> 的 path,而 Struts2 中 <s:form> 的 action 屬性對(duì)應(yīng)的是 <action> 的 name;name 要完全匹配,path 可以加些層次。
          主站蜘蛛池模板: 海淀区| 句容市| 拉孜县| 东兰县| 抚宁县| 湘潭市| 广丰县| 康马县| 张北县| 文水县| 西丰县| 四子王旗| 万州区| 托克逊县| 张家口市| 凌源市| 固阳县| 铁岭县| 和硕县| 诸暨市| 承德县| 安泽县| 华亭县| 麻江县| 延安市| 成都市| 淮北市| 特克斯县| 景宁| 武汉市| 梁河县| 调兵山市| 龙里县| 收藏| 射阳县| 武宣县| 绥宁县| 武功县| 丹巴县| 织金县| 蕉岭县|