Struts2中Action的搜素順序

當我們在struts.xml中配置action的時候,設置了package的namepace,但瀏覽器打開的路徑與其不相同也能運行action。
比如:我們的創建一個struts2項目,項目名爲:struts2。struts.xml配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">
        <struts>
            <package  name="default" namespace="/test" extends="struts-default">
                <action name="helloworld"  class="action.HelloWorldAction">
                        <result name="success">/result.jsp</result>
                </action>
            </package>
        </struts>

理論上,我們在瀏覽器中輸入的地址爲:
http://localhost:8080/struts2/test/helloworld.action才能運行helloworld.action。但是我們把地址改爲
http://localhost:8080/struts2/aaaa/helloworld.action也能訪問。


Struts2中Action的搜素順序
當我們訪問http://localhost:8080/strutsProject/path1/path2/path3/Test.action的時候:

首先,判斷package是否存在,如path1/path2/path3

  • 如果存在:判斷Action是否存在,比如:Test.action。如果不存在則跳轉到默認的namespace(在struts.xml中指定)中去找Action;如果不存在,則直接報錯
  • 如果不存在:依次檢查上一級package是否存在(直到默認namespace),執行 ①。

雖然理論上我們只要正確輸入Action就能運行,但推薦還是要正確輸入package哦

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章