调用Action的自定义方法
调用Action的自定义方法(非execute()方法)
动态方法调用(DMI)的方式
使用通配符* 进行配置的方式
动态方法调用
实际应用中,一个Action通常不止包含一个处理逻辑
比如说:用户管理(用户的CRUD操作)通常会利用一个Action(如UserAction)中的不同方法来进行处理(如add,del,upd,qry),这种方式即为动态方法调用Dynamic Method Invocation。
动态方法调用可以采用
在请求地址中指明调用Action中的哪个方法
or
通配符结合method属性的方式来指明调用Action中的哪个方法
Action中自定义执行方法: 除名字外与execute()方法相同即可。
即: public String func() throws Exception{
}
示例说明
UserAction:包含了登陆与注册2种逻辑的处理,分别是login()和register()。
登陆表单页:
注册表单页:
在请求地址中指明调用的方法
格式:
action名称!方法名
示例:
:
<form action="user!login" method="post">
:
<form action="user!register" method="post">
使用通配符+method配置的方式
通配符* :代表长度不为0的任意字符串
method属性用于指明调用action中的哪个方法
{ ?} :其中?为从1开始对*的编号
{?}表示第?个*处的值会替换到{?}处
通配符示例1
action的配置:
<action name="user*" class="" method="{1}">
<result name="toSuc" >/</result>
<result name="toFail">/</result>
</action>
若表单发出请求时的地址为:userlogin,则表明要调用UserAction中的login()方法;
若表单发出请求时的地址为:userregister,则表明要调用UserAction中的register()方法
表单中的请求地址:
:
<form action="userlogin" method="post">
:
<form action="userregister" method="post“>
通配符示例2
action的配置:
<action name="*_*" class="useraction.{1}Action" method="{2}">
<result name="toSuc" >/</result>
<result name="toFail">/</result>
</action>
若表单发出请求时的地址为: User_login ,则表明要调用UserAction中的login()方法;
若表单发出请求时的地址为: User_register ,则表明要调用UserAction中的register()方法
Action里的自定义方法 来自淘豆网m.daumloan.com转载请标明出处.