STRUTS2运行机制

前言: 

 首先感谢中国最专业的java培训班《北京尚学堂》的无私奉献精神,免费提供这么多学习资料。

以及我的偶像马士兵老师,感觉您特帅,技术特别牛,今生若能相见,甚好。谢谢。

 

struts2的运行机制:

探讨任何的运行机制,一定是这里开始,

http://localhost:8080/Struts2_Instroduction/hello.action)(.action可省略)

客户端在浏览器输入一个URL地址,这个请求通过http协议发送给tomcattomcat接收到请求后,查看请求的是哪一个webapplication,(如:Struts2_Instroduction,然后把Struts2_Instroduction交给对应的程序处理,首先读取它的web.xml看它是怎么配置的:

首先在web.xml里配了一个filter,它会过滤所有的URL地址(因为url-partten/*),所以当我们在地址栏敲http://localhost:8080/Struts2_Instroduction/hello的时候,会被filter-class里的org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter接收到,首先它会找到我们访问的namespace(就是hello前的反斜杠/),然后到struts.xml里去查。(因为namespace是和我们访问的路径一一对应的),当它找到一个namespace="/",它就看我们访问的namespace/后的hello,然后在action里找,如果找到hello,就把里边的result里的jsp的结果反馈。

[(注意我们访问的地址

http://localhost:8080/Struts2_Instroduction/hello)

 

 

hello前的反斜杠/就是struts.xml里的<package>标签里的namespace的值):

补充:

namespace决定了action的访问路径,默认为"",可以接受所有路径的action

namespace可以写为/,或者/xxx,或者/xxx/yyy,对应的action访问路径为/index.action,/xxx/index.action,或

者/xxx/yyy/index.action。

namespace最好也用模块来进行命名

action名字和jsp名字不是一回事,我们访问的是actionname,如果把namespace改为namespace="/admin" ,我们访问Hello.jsp的正确路径应为http://localhost:8080/Struts2_Introduction/admin/hello,否则会报错:在项目下没有一个action映射到namespace/

  • There is no Action mapped for      namespace [/] and action name [hello] associated with context path      [/Struts2_Instroduction].
  • 附时序图:

posted @ 2019-08-31 10:01  DarJeely  阅读(195)  评论(0编辑  收藏  举报