当tomcat启动的时候,因为配置了struts2的过滤器
所以加载了三个配置文件:
struts-default.xml, struts-plugin.xml, struts.xml
1、struts-default.xml,struts.xml文件在classpath下
底层在过滤器的init方法中加载了这3个配置文件
2、在struts-default.xml文件中写了很多内容
定义了拦截器 、结果集、等等。
3、struts-default.xml和struts.xml文件的dtd的内容是一样的
所以可以使用相同的元素。
struts2的配置文件中的namespace解析:
当配置为"/"时:客户端直接在工程名根目录下就可以访问到该配置的action。
而如果配置为/aa时:
例:
1、http://localhost:8080/itheima03_struts2/aa/userAction.action 可以访问到
2、http://localhost:8080/itheima03_struts2/userAction.action 不能访问到
3、http://localhost:8080/itheima03_struts2/aa/bb/userAction.action 可以访问到
结论:
命名空间的寻找机制是从当前空间开始寻找action,如果当层没有找到,则返回上一层寻找,
以此类推,直至根目录。
结果集也会随着namespace的改变而发生变化 。
例:
当namespace="/"时
结果集的配置为任意页面即可,寻找该页面的出发点既是当前工程的根目录。
而如果当namespace="/aa/bb"时
则结果集的寻找出发点则是在/aa/bb目录开始寻找这个页面,这时就会出现结果集找不到的异常。
解决方法:
按照上例的namespace的配置:
结果集需要这样配置即可解决:
<result name="index">../../index.jsp</result>
结论:namespace有几层,结果集中就用相对路径../来配置几层,以保证能够返回正确的页面给客户端。
扩展:可以将所有的页面全部扔到web-inf目录下,然后在结果集 中将页面的路径写死.
struts2的配置文件中的extends属性:
<package name="user" namespace="/aa" extends="struts-default">
<action name="userAction" method="add" class="cn.itheima03.struts2.action.UserAction">
<result name="index">index.jsp</result>
</action>
</package>
<package name="aa" namespace="/bb" extends="user"></package>
对于继承的说明:
根据上例得到以下结论:
1、struts2配置文件中的包具有继承机制。
2、name为aa的包已经把name为user的包的所有的内容继承过来了,包括struts-default。
3、访问命名空间为bb的userAction也行。
4、因为在struts-default.xml文件中拥有struts2运行时的所有的内容,所以必须继承过来才能使用struts2。
5、所以一个配置文件中的package必须继承struts-default.xml文件。
6、在struts-default.xml文件中,定义了一些结果集,这些结果集是struts2运行的关键。
<result-types>
<result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>
<result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>
<result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>
<result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/>
<result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>
<result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>
<result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>
<result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/>
<result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>
<result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" />
</result-types>
action的几种写法:
1、简单的一个javabean
2、让一个action 实现接口Action
public class ExecutionAction implements Action{
execute(){
}
}
在配置文件中:
<action name="executeAction" class="cn.itheima03.struts2.action.ExecuteAction">
*这里不需要写method属性,默认执行execute方法
3、让一个action类继承actionSupport(最常用)
public class ActionSupport implements Action, Validateable, ValidationAware.
说明:在ActionSupport类中做了很多的操作,比如:验证器、国际化等。程序员只要重写execute方法即可
<result name="success">index.jsp</result>
</action>
原理:在struts-default.xml中注册了一条默认的class:
<default-class-ref class="com.opensymphony.xwork2.ActionSupport" />
所以当一个action没有配置class属性,则struts2会去查找ActionSupport
而ActionSupport中的execute方法的返回值是"SUCCESS",所以只要结果集的name="success"时就可以利用此种特性。
此特性仅供了解,基本很少用。
所以加载了三个配置文件:
struts-default.xml, struts-plugin.xml, struts.xml
1、struts-default.xml,struts.xml文件在classpath下
底层在过滤器的init方法中加载了这3个配置文件
2、在struts-default.xml文件中写了很多内容
定义了拦截器 、结果集、等等。
3、struts-default.xml和struts.xml文件的dtd的内容是一样的
所以可以使用相同的元素。
struts2的配置文件中的namespace解析:
当配置为"/"时:客户端直接在工程名根目录下就可以访问到该配置的action。
而如果配置为/aa时:
例:
1、http://localhost:8080/itheima03_struts2/aa/userAction.action 可以访问到
2、http://localhost:8080/itheima03_struts2/userAction.action 不能访问到
3、http://localhost:8080/itheima03_struts2/aa/bb/userAction.action 可以访问到
结论:
命名空间的寻找机制是从当前空间开始寻找action,如果当层没有找到,则返回上一层寻找,
以此类推,直至根目录。
结果集也会随着namespace的改变而发生变化 。
例:
当namespace="/"时
结果集的配置为任意页面即可,寻找该页面的出发点既是当前工程的根目录。
而如果当namespace="/aa/bb"时
则结果集的寻找出发点则是在/aa/bb目录开始寻找这个页面,这时就会出现结果集找不到的异常。
解决方法:
按照上例的namespace的配置:
结果集需要这样配置即可解决:
<result name="index">../../index.jsp</result>
结论:namespace有几层,结果集中就用相对路径../来配置几层,以保证能够返回正确的页面给客户端。
扩展:可以将所有的页面全部扔到web-inf目录下,然后在结果集 中将页面的路径写死.
struts2的配置文件中的extends属性:
<package name="user" namespace="/aa" extends="struts-default">
<action name="userAction" method="add" class="cn.itheima03.struts2.action.UserAction">
<result name="index">index.jsp</result>
</action>
</package>
<package name="aa" namespace="/bb" extends="user"></package>
对于继承的说明:
根据上例得到以下结论:
1、struts2配置文件中的包具有继承机制。
2、name为aa的包已经把name为user的包的所有的内容继承过来了,包括struts-default。
3、访问命名空间为bb的userAction也行。
4、因为在struts-default.xml文件中拥有struts2运行时的所有的内容,所以必须继承过来才能使用struts2。
5、所以一个配置文件中的package必须继承struts-default.xml文件。
6、在struts-default.xml文件中,定义了一些结果集,这些结果集是struts2运行的关键。
<result-types>
<result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>
<result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>
<result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>
<result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/>
<result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>
<result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>
<result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>
<result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/>
<result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>
<result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" />
</result-types>
action的几种写法:
1、简单的一个javabean
2、让一个action 实现接口Action
public class ExecutionAction implements Action{
execute(){
}
}
在配置文件中:
<action name="executeAction" class="cn.itheima03.struts2.action.ExecuteAction">
*这里不需要写method属性,默认执行execute方法
3、让一个action类继承actionSupport(最常用)
public class ActionSupport implements Action, Validateable, ValidationAware.
说明:在ActionSupport类中做了很多的操作,比如:验证器、国际化等。程序员只要重写execute方法即可
4、在struts的配置文件中,在写action元素的时候,可以不写class属性
<action name="defaultAction"><result name="success">index.jsp</result>
</action>
原理:在struts-default.xml中注册了一条默认的class:
<default-class-ref class="com.opensymphony.xwork2.ActionSupport" />
所以当一个action没有配置class属性,则struts2会去查找ActionSupport
而ActionSupport中的execute方法的返回值是"SUCCESS",所以只要结果集的name="success"时就可以利用此种特性。
此特性仅供了解,基本很少用。