记录一下本人对struts2简单项目的执行过程的了解。
简单记录:首页访问-->web.xml-->struts.xml-->namespace-->action-->class-->result--->action或jsp或html
详细如下文:
1.浏览器中输入url=127.0.0.1:8080/struts_1208
2.访问web.xml文件:跳转到Hello.jsp,也就是用户访问url=127.0.0.1:8080/struts_1208/Hello.jsp
<welcome-file-list>
<welcome-file>Hello.jsp</welcome-file>
</welcome-file-list>
web.xml中的filter,访问src下的struts.xml文件
<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>
3.访问struts.xml文件
通过index.jps页面的超链接或form等找到对应的action
<constant name="struts.devMode" value="true" /> //<span style="color:#ff0000;">修改struts.xml中的内容,不许重启tomcat服务器,就能生效</span>
<package name="123t" namespace="/test" extends="struts-default">
<action name="Hello" class="com.HelloAction" method="add">
<result name="success">
/Hello.jsp
</result>
</action>
</package>
由于浏览器的url=127.0.0.1:8080/struts_1208/test/Hello
package-->namespace的name="/test"--->action的name="Hello"--->class="com.HelloAction"--->查看对应的class文件以及class中调用的method“add()”--->访问result的name为success下的Hello.jsp
注意:
1>namespace值,遇到通配符的,由上而下(处于struts.xml中的位置)开始匹配。
2>method的默认值为“excute”,此外可自己定义如:method="add"
3>result 的name 默认值为“success”,另外也可自己定义,等于method方法的返回值;
4>result中的内容必须加上"/",如:“/Hello.jsp”
5>若不配置action中的class,默认调用xwork-core.jar中的com.opensymphony.xwork2中的ActionSupport.class.否者自己新建action类继承ActionSupport类