web.xml

 <?xml version="1.0" encoding="UTF-8"?>
<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"> <display-name>Struts Blank</display-name> <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> <welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list> </web-app>

struts.xml

 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!-- consant 常量,struts.devMode表示开发者模式,当为true时如果改变了其中的一些代码,可以不用重新启动tomcat-->
<constant name="struts.devMode" value="true" /> <!--package可以有多个解决重名的情况, namespace 可以不写,默认是如何路径都可以,也可以写成/xx/yy ,必须是/开头-->
<package name="default" namespace="/" extends="struts-default"> <default-action-ref name="index" /> <global-results>
<result name="error">/error.jsp</result>
</global-results> <global-exception-mappings>
<exception-mapping exception="java.lang.Exception" result="error"/>
</global-exception-mappings> <!-- action里面那个name属性值得是URL输入的路径名 ,如“http://localhost:8080/Struts2Demo/hello”,则会根据result反馈Hello.jsp-->
<action name="hello" class="com.styspace.struts2.action.action2">
<result> /Hello.jsp</result>
</action> <!-- action里面class属性值,会有对应的这个类,执行该类里面的execute()方法--> <action name="action" class="com.styspace.struts2.action.action2">
<result name="success">/Action.jsp</result>
</action> <!-- action里面method属性值,会有对应class这个类中的add方法,然后执行该方法--> <action name="userAdd" class="com.bjsxt.struts2.user.action.UserAction" method="add">
<result>/user_add_success.jsp</result>
</action> <!-- 一般action里面不用method属性值,而是用DMI(动态方法调用)可以通过http://localhost:8080/Struts2Demo/user!addURL调用,其中user指的是action中的name值--> <action name="user" class="com.bjsxt.struts2.user.action.UserAction">
<result>/user_add_success.jsp</result>
</action> <!-- 更简单的方法,通配符,name=“student*”会匹配URL中所有Studentxx,而method=“{1}”指的是name中第一个“*”匹配的值 同理,result中{1}也是一样的--> <action name="Student*" class="com.bjsxt.struts2.action.StudentAction" method="{1}"> <result>/Student{1}_success.jsp</result>
</action>
<!-- 甚至可以有多个通配符,class属性中也可以用{1}来匹配,最简化-->
<action name="*_*" class="com.bjsxt.struts2.action.{1}Action" method="{2}">
<result>/{1}_{2}_success.jsp</result>
<!-- {0}_success.jsp -->
</action> </package>
<!-- Add packages here --> </struts>

工作原理:

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

struts2中struts.xml和web.xml文件解析及工作原理的更多相关文章

  1. struts2中struts.xml 放置路径的问题

    在搭建struts2项目时如果在web.xml中不指定struts.xml文件的路径,struts2会默认到/WEB-INF/classes中寻找加载其配置文件的,但我就是想把struts的配置文件放 ...

  2. 关于TOMCAT中的两个Web.xml

    关于TOMCAT中的两个Web.xml (2013-01-19 17:32:57) 转载▼ 标签: 杂谈   初学JAVA web开发.. Servlet定义的时候,我发现在${catalina.ho ...

  3. Eclispe创建maven工程缺失web.xml报web.xml is missing and <failOnMissingWebXml> is set to true的错误

    Eclispe创建maven工程缺失web.xml报web.xml is missing and <failOnMissingWebXml> is set to true的错误,一看,还缺 ...

  4. 游戏开发中IIS常见支持MIME类型文件解析

    游戏开发中IIS常见支持MIME类型文件解析 .apkapplication/vnd.android .ipaapplication/vnd.iphone .csbapplication/octet- ...

  5. Atitit。Tree文件解析器的原理流程与设计实现  java  c# php js

    Atitit.Tree文件解析器的原理流程与设计实现  java  c# php js 1. 解析原理与流程1 1.1. 判断目录  ,表示服  dirFlagChar = "└├─&quo ...

  6. JAVA之旅(二十五)——文件复制,字符流的缓冲区,BufferedWriter,BufferedReader,通过缓冲区复制文件,readLine工作原理,自定义readLine

    JAVA之旅(二十五)--文件复制,字符流的缓冲区,BufferedWriter,BufferedReader,通过缓冲区复制文件,readLine工作原理,自定义readLine 我们继续IO上个篇 ...

  7. 一图看懂hadoop分布式文件存储系统HDFS工作原理

    一图看懂hadoop分布式文件存储系统HDFS工作原理

  8. struts2中struts.xml配置文件详解【未整理】

    1.    深入Struts2的配置文件 本部分主要介绍struts.xml的常用配置. 1.1.    包配置: Struts2框架中核心组件就是Action.拦截器等,Struts2框架使用包来管 ...

  9. Struts2配置文件复用代码【web.xml、struts.xml、常量配置】

    web.xml的分发器代码: <!-- 引入struts核心过滤器 --> <filter> <filter-name>struts2</filter-nam ...

随机推荐

  1. HttpClient详解,Java发送Http的post、get方式请求 --待整理

    http://www.cnblogs.com/loveyakamoz/archive/2011/07/21/2112804.html http://blog.csdn.net/wangpeng047/ ...

  2. Mark 装修建材 清单

    装修攻略 介绍 装修公司:东易.龙发.金螳螂.乐豪斯乳胶漆:多乐士,立邦.三棵树.晨阳水漆.华润.都芳瓷砖:马可波罗.东鹏瓷砖.蒙娜丽莎.诺贝尔.简一瓷砖.欧神诺瓷砖.金舵瓷砖.卓远瓷砖.鹰牌.兴辉瓷 ...

  3. MySQL Proxy 实现 MySQL 读写分离提高并发负载

    还在学习,学完了在写笔记 (这个先安装lua:https://www.cnblogs.com/fps2tao/p/9163959.html ) 工作拓扑: MySQL Proxy有一项强大功能是实现“ ...

  4. 今天遇到的一个bug,折腾了一早上,不过解决了,还是很高兴

    1.总结出错的问题 当我在用flask做项目的时候,需要创建表,创建表的时候,我用的是Flask-Migrate组件,直接用python manage.py init ,python manage.p ...

  5. IO模型-java版

    描述IO,我们需要从两个层面: 编程语言 实现原理 底层基础 从编程语言层面 BIO | NIO | AIO 以Java的角度,理解,linux c里也有AIO的概念(库),本文只从Java角度入手. ...

  6. Ubuntu和centos下查看包的安装路径

    安装包后,如何查看安装的具体路径? Ubuntu下: dpkg -L  <包名> CentOS下: rpm -ql   <包名> 助记: l为list的首字母. q为query ...

  7. 源码编绎的时候报错 tengine-2.1.0 error: the HTTP rewrite module requires the PCRE library.

    ./configure: error: the HTTP rewrite module requires the PCRE library. You can either disable the mo ...

  8. mongoose中的versionKey

    通过mongoose中的save方法保存记录时document文档默认最后会有一个字段"__v",这个字段表示该文档是否是刚刚创建的,如果是则字段"__v"的值 ...

  9. Android系统框架介绍

    src目录:主要是完成java代码的编写 assets目录:资源目录 res目录:存在图片,布局文件和字符串.菜单等文件 bin目录:输出文件夹,如生成的APK文件 project,propertie ...

  10. Activiti初学者教程 (zhuan)

    http://blog.csdn.NET/bluejoe2000/article/details/39521405 ****************************************** ...