struts2中struts.xml和web.xml文件解析及工作原理


web.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app id="WebApp_9" version="2.4" xmlns="http://Java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 3 
 4     <display-name>Struts Blank</display-name>
 5 
 6     <filter>
 7         <filter-name>struts2</filter-name>
 8         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
 9     </filter>
10 
11     <filter-mapping>
12         <filter-name>struts2</filter-name>
13         <url-pattern>/*</url-pattern>
14     </filter-mapping>
15 
16     <welcome-file-list>
17         <welcome-file>index.html</welcome-file>
18     </welcome-file-list>
19 
20 </web-app>

 

struts.xml

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC
 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 4 "http://struts.apache.org/dtds/struts-2.3.dtd">
 5 
 6 <struts>
 7 
 8 <!-- consant 常量,struts.devMode表示開發者模式,當為true時如果改變了其中的一些代碼,可以不用重新啟動tomcat-->
 9 <constant name="struts.devMode" value="true" />
10 
11 <!--package可以有多個解決重名的情況, namespace 可以不寫,默認是如何路徑都可以,也可以寫成/xx/yy ,必須是/開頭-->
12     <package name="default" namespace="/" extends="struts-default">
13 
14        <default-action-ref name="index" />
15 
16        <global-results>
17            <result name="error">/error.jsp</result>
18        </global-results>
19 
20        <global-exception-mappings>
21            <exception-mapping exception="java.lang.Exception" result="error"/>
22        </global-exception-mappings>
23 
24 <!-- action里面那個name屬性值得是URL輸入的路徑名 ,如“http://localhost:8080/Struts2Demo/hello”,則會根據result反饋Hello.jsp-->
25        <action name="hello" class="com.styspace.struts2.action.action2">
26            <result>  /Hello.jsp</result>
27        </action>
28 
29 <!-- action里面class屬性值,會有對應的這個類,執行該類里面的execute()方法-->
30 
31        <action name="action" class="com.styspace.struts2.action.action2">
32            <result name="success">/Action.jsp</result>
33        </action>
34 
35 <!-- action里面method屬性值,會有對應class這個類中的add方法,然后執行該方法-->
36 
37 <action name="userAdd" class="com.bjsxt.struts2.user.action.UserAction" method="add">
38             <result>/user_add_success.jsp</result>
39         </action>
40 
41 <!-- 一般action里面不用method屬性值,而是用DMI(動態方法調用)可以通過http://localhost:8080/Struts2Demo/user!addURL調用,其中user指的是action中的name值-->
42         
43         <action name="user" class="com.bjsxt.struts2.user.action.UserAction">
44             <result>/user_add_success.jsp</result>
45         </action>
46 
47 <!-- 更簡單的方法,通配符,name=“student*”會匹配URL中所有Studentxx,而method=“{1}”指的是name中第一個“*”匹配的值 同理,result中{1}也是一樣的-->
48 
49 
50         <action name="Student*" class="com.bjsxt.struts2.action.StudentAction" method="{1}">
51 
52             <result>/Student{1}_success.jsp</result>
53         </action>
54 <!-- 甚至可以有多個通配符,class屬性中也可以用{1}來匹配,最簡化-->
55         <action name="*_*" class="com.bjsxt.struts2.action.{1}Action" method="{2}">
56             <result>/{1}_{2}_success.jsp</result>
57             <!-- {0}_success.jsp -->
58         </action>
59 
60 
61     </package>
62     <!-- Add packages here -->
63 
64 </struts>
65 
66  

 

工作原理:

1、在瀏覽器中輸入 http://localhost:8080/Struts2Demo/hello,就會向服務器端(tomcat)發送一個請求
 
2、tomcat會解析URL,從中找到一個webApplication(可理解為即項目名)為Struts2Demo,然后就會在這個項目里面找到web.xml文件
 
3、在web.xml中找到filter標簽,然后在filter中定義的 filter-class處理URL中的hello(這其中其實還包含一個步驟,就是web.xml中<url-pattern>/*</url-pattern>會過濾掉所有地址,這樣地址才會被filter-class中定義的類接收到)
 
4、StrutsPrepareAndExecuteFilter接收到地址之后,首先查詢namespace(在struts.xml中的package標簽中的namespace中得到它的值),然后將URL中namespace值(這里是/)后面的路徑讀取到(這里是hello)
 
5、繼續在struts的action標簽中查找是否有hello這個值的,如果有且發現action中有class屬性,則會new一個class中聲明的類,執行里面的一個execute()方法,該方法返回一個String字符串,返回該字符串之后才能得到result中的值,如果action中沒有class屬性,則默認有一個ActionSupport類,該類中也有一個execute方法,返回一個String值
 
6、上一步中講到execute()方法,但是不一定非要執行execute()方法,當action標簽中有method屬性時,就會執行該屬性定義的方法名稱,然后同樣會返回一個String字符串
 
7、根據返回的String字符串(success),在result中找到name屬性值為返回的String字符串的標簽(這里就是Action.jsp),如果沒有找到,則會返回error頁面;如果action中沒有class則直接找到該action下面的result中的值(這里是/Hello.jsp),然后將請求forword到Action.jsp/Hello.jsp
 
8、最后jsp反饋到客戶端。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM