1、Struts2的配置文件的加载顺序(了解)
(1)Struts2的配置文件加载顺序
init_DefaultProperties(); // [1] 加载default.properties。init_TraditionalXmlConfigurations(); // [2] 加载struts-default.xml、struts-plugin.xml、struts.xml。init_LegacyStrutsProperties(); // [3] 加载struts.propertiesinit_CustomConfigurationProviders(); // [5],源码中没有4 加载配置提供类。init_FilterInitParameters() ; // [6] 加载web.xml中过滤器初始化参数。init_AliasStandardObjects() ; // [7] 加载Bean对象。(2)加载文件的顺序 default.properties struts-default.xml struts-plugin.xml struts.xml struts.properties web.xml
前三个是Struts2的jar里面的,改不了。 后面三个文件是我们自己可以配置的。
注意:后配置的常量值会覆盖先前配置的常量值。
2、Action的配置
(1)package标签
package标签称为包,这个包与Java中的包的概念不一致。 包为了更好管理action的配置。
(2)package标签的4个属性
name :包的名称任意,只要在一个项目中不重名即可。extends :继承哪个包,通常值为struts-default。namespace :名称空间,与标签中的name属性共同决定访问路径。abstract :抽象的,是否允许其他包继承本包。(3)名称空间有三种写法:
带名称的名称空间:namespace=”/aaa”根名称空间:namespace=”/”默认名称空间:namespace=””(4)action标签配置Action类
action标签的4个属性:
name :与package的namespace属性共同决定访问路径。class :Action类的全路径。method :执行Action中的哪个方法的方法名,默认值execute。converter :用于设置类型转换器。2、Struts2的常量配置
(1)什么是常量? public final 修饰的全局变量称为常量。
在Struts2的框架中,提供了非常多的常量:(在default.properties属性文件中)
例如:struts.i18n.encoding=UTF-8 Struts2中所有的post请求的中文乱码不用处理。
struts.action.extension=action,, Struts2请求的默认的扩展名。默认扩展名是.action或者什么都不写。
(2)在Struts2中修改一些常量的值
修改常量的值,可以有三个位置进行修正,也就是上面我们说的6个加载文件中的后3个。
struts.xml中进行修改(常用的) <constant name="struts.action.extension" value="action" /> struts.properties中进行修改 struts.action.extension=action web.xml的过滤器中进行修改 <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> <!-- 在这里也可以配置常量 --> <init-param> <param-name>struts.action.extension</param-name> <param-value>action</param-value> </init-param> </filter>以上三个位置都可以配置常量。 从代码简介和便利的角度上看,通常我们都是在struts.xml中配置常量。
3、include的配置
通常用在分模块开发中,在struts.xml中引入其他的配置文件。
例如: 张三写了一个模块,需要对struts.xml进行一段配置。 李四写了一个模块,也需要对struts.xml进行一段配置。
则让他们各自写一个配置文件,然后在struts.xml中引入他们写的配置文件即可。
<include file="com/pipi/struts/test01/zhangsan.xml" /> <include file="com/pipi/struts/test01/lisi.xml" />这样分配有序,各自写各的,不会乱。