今天學習struts2的namespace,遇到一個比較怪的問題:
struts.xml配置如下:
- <package name="front" namespace="/front" extends="struts-default">
- <action name="index">
- <result>
- /Namespace.jsp
- </result>
- </action>
- </package>
然後在瀏覽器中輸入:http://localhost:8080/projectname/front/xxx/yyy/index.action居然沒有報錯,當時真是百思不得其解啊。
於是baidu的一下,經過一番查找,終於明白了其中了原理:(namesapce的處理流程)
在baidu搜索到的答案如下:
首先會在/front/xxx/yyy/下查找是否存在index的action,如果存在,則執行此action對應的操作,否則就到
/front/xxx/下查找是否存在index的action,如果存在,則執行此actin對應的操作,否則繼續到
/font/下查找是否存在index的action,如果存在,則執行此action對應的操作,否則就到
/下查找是否存在index的action,如果存在,則執行此action對應的操作,否則報錯。
本來我也以爲是這樣的流程,後然我又去查了下官方文檔,文檔上說如果在/front/xxx/yyy/下找不到action的話,
就會調用默認的namespace,而不會一層一層的往上查找。所以上面的說法是錯誤的。
附官方文檔原文如下
Namespaces 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. |