Struts2 Action中动态方法调用、通配符的使用
一、Struts2执行过程图:

二、struts2配置文件的加载顺序
struts-default.xml---struts-plugin.xml---struts.xml
具体步骤:







三、Action中动态方法调用<Dynamic Method Invocation> DMI
第一种方式:
自定义DMIAction类,使它继承ActionSupport类,该类无需手动重写execute(),底层有默认实现。因此我们也可以自定义方法list。
struts.xml中的action元素植入method调用前台返回的方法list


若一个类中有多个方法,在struts.xml中需植入多个action元素,因此该方法的安全性低
第二种方式:
在struts.xml中开启动态方法调用,即可使用一个action,并通过在Action的名称中使用感叹号(!)来标识要调用的方法名称
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
/* * 添加图书 */public String add() throws Exception { System.out.println("======add===="); return "add";}/* * 删除图书 */public String del() throws Exception { System.out.println("======del===="); return "del";}/* * 修改图书 */public String edit() throws Exception { System.out.println("======edit===="); return "edit";} |

执行效果:

四、Action中通配符的使用
通配符用星号(*)表示,用于配置0个或多个字符串,在配置Action时,可以在action元素的name属性中使用星号来匹配任意的字符串


实现效果:


浙公网安备 33010602011771号