action访问路径
struts1中,借助action标签的path属性指定调用的action的URL路径,如
<action path="/testAction.do"></action>
struts2中,调用action的URL路径(包的命名空间+”/”+action的名称)如:访问TestACtion的URL路径为:/test/testAction.action
<package name="test" namespace=“/test“ extends="struts-default">
<action name="testAction" class="cn.test.TestAction">
<result name="success" >/success.jsp</result>
</action>
</package>
Action访问路径的匹配顺序
根据uri按namespace从长到短的顺序去匹配package
举例:如url,http://ip/strutsapp/path1/path2/path3/myAction.action
查找顺序如下:
首先获取请求路径uri:strutsapp/path1/path2/path3/myAction.action
1.寻找namespace为/path1/path2/path3的package,若存在该package,则从该package寻找名为myAction的action,如不存在则继续查找。
2.寻找namespace为/path1/path2的package,若存在该package,则从该package中寻找名为myAction的action,如不存在继续寻找。
3.寻找namespace为/path1的package,如存在该package,则从该package中寻找名为myAction的action;如不存在,就去默认的namaspace的package下面去找名为myAction的action(默认命名空间为“/” )。
如果最后还是找不到,则提示action不存在。
action配置中常见的默认值
如尚未为action指定class,默认com.opensymphony.xwork2.ActionSupport
执行ActionSupport中的execute方法。
1.如没有为action指定class,默认是ActionSupport。
2.如没有为action指定method,默认执行action中的execute() 方法。
ActionSupport的execute方法只有一句return “success”;3.如果没有指定result的name属性,默认值为success。
ActionSupport
默认 Action 类是com.opensymphony.xwork2.ActionSupport,在编
写 Action 类时, 通常会对这个类进行扩展。
struts2常量
Struts2的常量一般定义在(org.apache.struts2包下的)struts.properties(default.properties)文件中,修改常量,需要在struts.xml或struts.properties中配置,推荐在struts.xml中配置。
例如:
在struts.xml文件中配置常量,配置拦截的请求后缀
<struts>
<constant name="struts.action.extension" value="go"/>
</struts>
StrutsPrepareAndExecuteFilter是Struts 2框架核心控制器,负责拦截由/*指定所有用户请求。默认情况下,如用户请求路径不带后缀或者后缀以.action结尾,请求将被转入Struts 2框架处理,否则Struts 2框架将略过该请求。
因常量可在多个配置文件中进行定义,struts2加载常量的搜索顺序:
1 struts-default.xml
2 struts-plugin.xml
3 struts.xml
4 struts.properties
5 web.xml
如在多个文件中配置同一个常量,则后一个文件中配置的常量值会覆盖前一个文件配置的常量值.
struts2常见常量配置
1)、struts.i18n.encoding=UTF-8
指定默认编码集,作用于HttpServletRequest的setCharacterEncoding方法和freemarker 、velocity输出。
2)、struts.action.extension=action,,
指定需要Struts 2处理的请求后缀,默认是action或没有后缀名,如用户需指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开
3)、struts.serve.static.browserCache=true
设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭.
4)、struts.configuration.xml.reload=true
系统是否自动重新加载修改后的struts配置文件,默认值为false(生产环境下使用),开发阶段最好打开。
5)、struts.devMode=true
打印出更详细的错误信息(开发模式下使用)。
6)、struts.ui.theme=simple
默认视图主题。
7)、struts.objectFactory=spring
与spring集成时,指定由spring负责action对象的创建。
8)、struts.enable.DynamicMethodInvocation=true
该属性设置Struts 2是否支持动态方法调用(默认值是true)
9)、struts.multipart.maxSize=10701096
上传文件大小限制(字节)
使用多个struts配置文件
在大部分应用里,随着应用规模增大,Action数量会大量增加,导致struts.xml配置文件变得臃肿。为了避免struts.xml文件过于庞大、臃肿,提高并发开发效率,增强配置文件文件可读性,可将一个struts.xml配置文件分解成多个配置文件,在struts.xml文件中引入这些配置文件。
举例:
下面的struts.xml通过<include>元素指定多个配置文件:
<struts>
<include file="struts1.xml"/>
<include file="struts2.xml"/>
<include file="struts3.xml"/>
......
</struts>