struts.xml訪問路徑問題一:
注意(重點):namespace可以寫爲/,或者/a,或者/a/b,對應的action訪問路徑爲/download.action,/a/download.action,或者/a/b/download.action
名稱空間=/a/b/c/d
名稱空間=/a/b/c
名稱空間=/a/b
名稱空間=/a
名稱空間=/(默認)
一):namespace決定了action的訪問路徑,默認爲"",可以接受所有路徑的action
第一種方式配置struts.xml文件如下:
<struts>
<!--沒有配置namespace屬性,默認可以接受所有的action的訪問-->
<package name="download" extends="struts-default">
<!--指定action的名字-->
<action name="download" class="cn.lsh.web.struts2.download.DownloadAction" method="downloadMethod">
<result name="success" type="dispatcher">/WEB-INF/message.jsp</result>
</action>
</package>
</struts>
例如:我新建的web工程名稱爲struts2,
在cn.lsh.web.struts2.download.DownloadAction包下:新建的action爲DownloadAction,
那麼當你部署到tomcat服務器上時,在瀏覽器地址欄中訪問的路徑:
http://localhost:8080/struts/download ---->ok
http://localhost:8080/struts/a/download ---->ok
http://localhost:8080/struts/a/b/download ---->ok
http://localhost:8080/struts/a/b/c/download ---->ok
第二種方式配置struts.xml文件如下:
<struts>
<!--沒有配置namespace屬性爲naspace="/",同樣和第一種方式一樣都可以接受所有的action的訪問-->
<package name="download" naspace="/" extends="struts-default">
<!--指定action的名字-->
<action name="download" class="cn.lsh.web.struts2.download.DownloadAction" method="downloadMethod">
<result name="success" type="dispatcher">/WEB-INF/message.jsp</result>
</action>
</package>
</struts>
那麼當你部署到tomcat服務器上時,在瀏覽器地址欄中訪問的路徑:
http://localhost:8080/struts/download ---->ok(測試通過沒問題)
http://localhost:8080/struts/a/download ---->ok(測試通過沒問題)
http://localhost:8080/struts/a/b/download ---->ok(測試通過沒問題)
http://localhost:8080/struts/a/b/c/download ---->ok(測試通過沒問題)
二)struts.xml文件如下:
<struts>
<!--指定了配置namespace屬性爲:namespace="/aa"-->
<package name="download" namespace="/aa" extends="struts-default">
<!--指定action的名字-->
<action name="download" class="cn.lsh.web.struts2.download.DownloadAction" method="downloadMethod">
<result name="success" type="dispatcher">/WEB-INF/message.jsp</result>
</action>
</package>
</struts>
例如:我新建的web工程名稱爲struts2,
在cn.lsh.web.struts2.download.DownloadAction包下:新建的action爲DownloadAction,
那麼當你部署到tomcat服務器上時,在瀏覽器地址欄中訪問的路徑:
http://localhost:8080/struts/download ---->ok(測試通過沒問題)
http://localhost:8080/struts/aa/download ---->ok (測試通過沒問題)
struts.xml訪問路徑問題二:
1):例如我在struts.xml中配置了<constant name="struts.action.extension" value="action"/>,
那麼在瀏覽器地址欄中訪問的路徑必須是:http://localhost:8080/struts/download.action.
如果在瀏覽器地址欄中訪問的路徑是:http://localhost:8080/struts/download 那麼則會報404錯誤。
2):如果沒有在struts.xml中配置<constant name="struts.action.extension" value="action"/>,
那麼(.action)是可以默認不寫,在瀏覽器地址欄中訪問的路徑可以是:http://localhost:8080/struts/download , 也可以是http://localhost:8080/struts/download.action.