0、Struts2的运行机制
当你在客户端敲http://localhost:8080/strust2test/hello
首先找到:strust2test这个web application,找到后去执行这个web application下的web.xml
Tomcat接收到请求之后,会发现这个web.xml下面,配了一个filter,而这个filter过滤所有的url地址,所以当我们在地址栏敲http://localhost:8080/strust2test/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
<package name="default" namespace="/" extends="struts-default"> <action name="hello" > <result>/hello.jsp</result> </action></package>
查到“/”后面的hello,它就会去package下面查是否有name属性叫“hello”的action,有的话,找里面对应的result是谁--?hello.jsp
Struts的好处就是:
我可以把“请求”和“视图展现”分开,而不是写死。分开的好处就是:如果要换成其他视图,配一下就好了,所以更加灵活。Struts核心的本质就是解决了:把你的请求和最后的结果分开。