Struts2學習筆記一之工做原理和struts.xml解析

  Struts是MVC的一種實現,它將 Servlet和 JSP 標記(屬於 J2EE 規範)用做實現的一部分。web

  Struts的核心是ActionSevlet,ActionSevlet的核心是struts.xml。(這個xml文件做爲ActionServlet的配置文件)apache

  1、Struts的響應過程:jsp

  1.當Tomcat收到client訪問請求,好比http://localhost:8080/Struts2_01_Introduction/Hello_struts,會訪問Struts2_01_Introduction項目下的web.xml。url

  2.在web.xml中,定義了一個filter,它會過濾全部的url地址,好比當咱們輸入Hello_struts時,這個地址就會被org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter這個類接收到。spa

  3.而後,這個類會在struts.xml中查詢,首先查namespace,這裏的namespace是/,而後會在action中查找Hello_struts,若是找到了,將result返回給client。

  簡單說,Struts2的做用是將請求和顯示結果分開。code

 

  2、struts.xml解析xml

  

<struts>

    <!-- 開啓開發模式(當修改action的name而後進行訪問時 不會出錯) -->
    <constant name="struts.DevMode" value="true"/>
    <package name="package" namespace="/c" extends="struts-default">
        <action name="aaa">
            <result>/index.jsp</result>
        </action>
    </package>

</struts>    

  1.package做用形同Java中的包,用來區分模塊。blog

  2.namespace決定了action的訪問路徑,默認爲"",此時能夠接受全部路徑的action。namespace能夠寫成"/","/xxx",或者"/xxx/yyy",對應的action訪問路徑爲/index.jsp,/xxx/index.jsp,/xxx/yyy/index.jsp。好比,要訪問上述代碼叫作"aaa"的action,須要輸入http://localhost:8080/Project Name/c/aaa 才能訪問。開發

相關文章
相關標籤/搜索