天天看點

Struts2的運作機制

當你在用戶端敲http://localhost:8080/strust2_0100_Introduction/hello,這個url請求會通過HTTP協定發送給伺服器Tomcat,Tomcat收到請求之後,看收到的是哪個web application,strust2_0100_Introduction這個web application,找到後去執行這個web application下的web.xml

Tomcat接收到請求之後,會發現這個web.xml下面,配了一個filter,而這個filter過濾所有的url位址,是以當我們在位址欄敲http://localhost:8080/strust2_0100_Introduction/hello後,這個位址會被StrutsPrepareAndExecuteFilter接收到

<filter>

        <filter-name>struts2</filter-name>   

<filter-class>

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

  </filter>

  <filter-mapping>

        <filter-name>struts2</filter-name>

        <url-pattern>/*</url-pattern>

</filter-mapping>

StrutsPrepareAndExecuteFilter接收到後url情求後,它首先看namespace

Struts.xml

<packagename="default"namespace="/"extends="struts-default">

        <actionname="hello">

            <result>/hello.jsp</result>

        </action>

</package>

查到“/”後面的hello,它就會去package下面查是否有name屬性叫“hello”的action,有的話,找裡面對應的result是什麼--hello.jsp

Struts的核心:把請求和視圖分開。分開的好處就是:增加靈活性