1.访问应用路径的设置
测试Action名称的搜索顺序:<br>
<a href="${pageContext.request.contextPath}/aaaa/primer/aaa/primer/helloWorldAction.action">helloWorld</a><br>
<a href="${pageContext.request.contextPath}/primer/primer/aaa/primer/helloWorldAction.action">helloWorld</a><br>
<a href="${pageContext.request.contextPath}/primer/primer/helloWorldAction.action">helloWorld</a><br>
<a href="${pageContext.request.contextPath}/primer/helloWorldAction.action">helloWorld</a><br>
* 后面3个都可以,以<a href="${pageContext.request.contextPath}/primer/primer/aaa/primer/helloWorldAction.action">helloWorld</a><br>为例,开始在
/primer/primer/aaa/primer命名空间找action找不到到/primer/primer/aaa命名空间下找,一直到/primer命名空间下找action
2.没有为action指定class
访问路径如下:
<a href="${pageContext.request.contextPath}/primer/actionNoClass.action">helloWorld</a><br>
struts.xml文件配置如下:
<action name="actionNoClass">
<result name="success">/primer/success.jsp</result>
</action>
* 可以执行 struts.default里面配置了默认class
* result结果类型,会返回因为ActionSupport的execute方法返回的是success
<!-- 配置在struts2框架运行时,如果没有为action指定class的话,默认要执行的class的类名 -->
<default-class-ref class="com.opensymphony.xwork2.ActionSupport" />
* 执行ActionSupport里的execute方法
3.没有action的时候
<a href="${pageContext.request.contextPath}/primer/userAction.action">helloWorld</a><br>
* 在primer命名空间下没有userAction
<!--
如果找不到对应的action名的时候,配置默认要执行的action
* name:指定action的名称
-->
<default-action-ref name="helloWorldAction" />
4.常用配置
<constant name="struts.action.extension" value="do,love"></constant>
* 设置访问路径的后缀名
* constant:配置常量
* name:指定的是struts2框架底层提供的default.properties资源文件中配置的"常量"
* value:指定的是配置常量的值
* 在struts.xml文件中,配置的常量的值会覆盖底层提供的default.properties资源文件中配置的常量的值
* 配置struts2框架的页面中请求连接的后缀名,如果指定多个的话,用","隔开
* 如果在struts.xml中和struts.properties资源文件中同时进行配置,struts.properties的配置起作用
* 因为常量可以在多个配置文件中进行定义,所以我们需要了解下struts2加载常量的搜索顺序:
1 struts-default.xml
2 struts-plugin.xml
3 struts.xml
4 struts.properties(自己创建)
5 web.xml
<!-- 配置国际化资源文件修改时,是否重新加载。默认是false为不加载,true是加载 -->
<!-- <constant name="struts.i18n.reload" value="true"></constant> -->
<!-- 配置struts2框架的配置文件修改时,是否重新加载。默认是false为不加载,true是加载 -->
<!-- <constant name="struts.configuration.xml.reload" value="true"></constant> -->
<!--
配置struts2框架的模式
* 默认值是false,是生产模式
* true是开发模式,需要更多的调试信息
### includes:
### - struts.i18n.reload = true
### - struts.configuration.xml.reload = true
-->
<constant name="struts.devMode" value="true"></constant>
* 配置为开发模式,则国际化资源文件与框架的配置文件修改是,自动重新加载,前面两项不需要配置
5.struts.xml文件的分散配置
* 在项目开发中,struts.xml文件为公共文件,改动不宜太频繁,在svn版本控制中,也容易造成冲突,所以必须对struts.xml文件进行分散配置
<include file="cn/itcast/primer/struts_primer.xml"></include>