图解一、大纲(下面有其详解,最下面是文字讲解)
1、struts.xml
(1).package配置
package中的namespace对应的地址:localhost:8080/hello/helloAction
<!--
package 它跟项目中的包没有关系,是用来管理action,可以配置多个action
name 给这个package起个名字,多个package的名字不能重复,没什么意义,可以随便命名;
一般情况下它的命名是按照当前package管理action的分类来命名的。
namespace 访问路径地址前缀,给当前的action设置一个访问前路径跟name没有关系
跟其他的package中的namespace能不能重复,没有要求。
extends 继承自struts-default必须写名字不能改
从struts2 核心包中 struts-2.2-core 下面的struts-default.xml
这里配置了很多默认的属性。
abstract 抽象的 声明的一个标志,当前的这个配置文件不能独立运行,等待被继承。-->
<package name="hello"namespace="/hello" extends="struts-default">
(2).action配置
、<!--
action 配置详解
name 给action起个名字,决定了访问路径最后的地址
class 类的完整路径名
method 访问类中的方法-->
<action name="helloAction"class="cn.hd.hello.HelloAction" method="hello">
(3).result配置
<!--result
name 对应的是action类中的method的返回值
名字可以随便写,只要和action类中的返回值结果是一样的
但是我们一般情况下使用特定的。
type dispatcher 转发
redirect 重定向
标签中间的值 跳转页面的地址-->
<result name="success"type="redirect">/hello.html</result>
2、常量配置
默认的常量配置 在struts2的核心包下的
默认常量配置的文件名是default.propertites
如何修改这些常量的方法:
(1).在struts.xml中去修改
<!--i18n 国际化 相当于我们在Servlet中设置的编码 解决了post请求的中文乱码问题-->
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
(2).在src目录下创建struts.properties文件 直接在该文件中去修改常量即可
(3).在web.xml文件中去修改,添加一个<context-param>。
在param-name中书写常量名,在param-value中书写常量值。
三个地方可以同时修改,但是生效的顺序为web.xml>struts.properties>struts.xml.推荐使用第一个!
常用的常量配置
(1).i18n.ecoding
<!--i18n 国际化 相当于我们在Servlet中设置的编码 解决了post请求的中文乱码问题-->
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
(2).扩展名 struts.action.extension
<constantname="struts.action.extension"value="action"></constant>
(3).struts.devMode设置开发者模式 默认关闭
<!--developerMode 开发者模式
1.热部署 当修改配置文件后,等一段时间会自动加载
2.提高错误信息的提示(友好的错误提示)-->
<constant name="struts.devMode"value="true"></constant>
(4).include 在src struts.xml可以读取其他位置的xml配置文件。
要求 新创建的xml 必须也有约束
<includefile="cn\hd\dynamic\struts.xml"></include>
3、高级配置
为什么要使用动态方法:
1.如果使用的是 POJOaction ,动态方法调用可能会引起安全问题;
2. 动态方法调用和从 Struts 1 中引用过来的通配符方法有重叠;如果你的应用涉及到安全,或者习惯在 action 配置中使用感叹号作为通配符的话,需要在struts 应用的 struts.properties 配置文件中将 struts.enable.DynamicMethodInvocation 设置为 false 。
如何配置动态方法:
(1).推荐使用
(访问路径:localhost://8080/user/userAction_add.action)
<action name="userAction_*"class="cn.hd.dynamic.UserAction" method="{1}">
<resultname="success">/hello.html</result>
</action>
action:配置中的name属性,决定着浏览器的访问地址
struts2可以采用通配符的方式默认去读取 method{1} 会自动将路径中userAction… 配置到method中。
(2).开启动态方法
首先要开启动态方法的常量(访问路径: http://localhost:8080/user/userAction!delete.action)
<!--动态方法调用的常量,默认是关闭的-->
<constant name="struts.enable.DynamicMethodInvocation"value="true"></constant>
<action name="userAction"class="cn.hd.dynamic.UserAction">
<resultname="success">/hello.html</result>
</action>
将action中的method属性删除掉
完成了动态方法的开启
测试的时候,在浏览器的路径中 输入action的name值 + ! +方法名(Action中的方法名)
注意:
第一种方法 在struts2的高版本中会无效
首先你也要开启动态方法,然后增加allowed-methods属性
<action name="userAction_*"class="cn.hd.dynamic.UserAction" method="{1}">
<resultname="success">/hello.html</result>
<!--高版本要加这个属性,不然版本会不生效-->
<allowed-methods>update,add,delete,select</allowed-methods>
</action>
4、默认配置
(1).删减配置
<!--
method 属性可以不写 默认方法 execute
result 标签中的name属性可以不写success 默认为success
type 可以不写默认是dispatcher(转发)
class 可以不写 默认的类是核心包Struts2-default中的ActionSupport
打开这个类文件它里面有execute方法,该方法返回一个success字符串
-->
<actionname="defaultAction"class="cn.hd.default_demo.DefaultAction">
<result>/hello.html</result>
</action>
(2).默认的action地址
<!--default-action-ref 默认的action地址
如果请求地址不存在,那么会默认访问default-action-ref里面的action-->
<default-action-refname="defaultAction"></default-action-ref>