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

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反馈到客户端。

猜你喜欢

转载自blog.csdn.net/qq_32444825/article/details/80212778