在Strust2中action可以定义自己的方法,调用方法有两种方式,一种方式是struts.xml中指定method来表示需要用到的方法,但是这种方法缺点在于如果你的Action中有很多方法则要多次配置,这样很繁琐,另一种方法是使用动态方法调用DMI。
举一个简单的例子——
比如LoginAction中有add,dell方法,则
1、配置struts.xml文件
1
2? ? ?
3? ? ? ? ? ? /add.jsp
4? ? ? ? ? ? /dell.jsp
5? ? ?
6
2、在 loginAction中 编写add方法和dell方法:
public? String? add()
{
? return "add";
}
public? String dell()
{
? return "dell";
}
3、链接中使用!动态调用所需方法:
调用add方法
调用dell方法
常见错误:
?There is no Action mapped for namespace [/XXX] and action name [xxx!xxx] associated with context path [/xxx_method].?
解决方法:
?默认的Struts.xml中是这样的
?这样我们在地址栏输入感叹号动态调用方法的时候会出错,我们只需更改这一句即可
?
??