天天看點

3.1.struts.xml中package的namespace屬性

 struts.xml檔案中package标簽除了有name、extends屬性外,還有一個十分重要的屬性:namespace。如下:

<struts>

    <package name=”struts2” extends=”struts-default” namespace=”/abc”>

     <!– 使用者登入Action –>

     <action name=”login” class=”com.cheng.sln.mvc.struts2.action.LoginAction”>

      <result name=”success”>/jsp/struts2/result.jsp</result>

      <result name=”input”>/jsp/struts2/login2.jsp</result>

     </action>

    </package>

</struts>

該屬性預設為namespace=”“,是以在通路該Action時,直接http://localhost:8080/應用名稱/login.action就可以請求到該Action。

當namespace=”/abc”時,再通路該Action時,其URL應為:http://localhost:8080/應用名稱/abc/login.action。

這樣在大型項目中有很多個Action時容易分類區分。

問題:如果namespace=”“,此時用http://localhost:8080/應用名稱/abc/login.action通路該

Action,會不會成功?

答案是:會。因為當程式找不到相應的namespace時,它會自動去預設的namespace中去找Action。