Struts2-学习笔记系列(6)-动态调用action

动态调用之前需要配置:

<!--动态方法调用-->
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>

 

配置struts

 1 <package name="zcx.controller" namespace="/" extends="struts-default">
 2 
 3     <action name="login" class="zcx.controller.LoginAction">
 4 
 5         <result name="success">/WEB-INF/content/welcome.jsp</result>
 6 
 7     </action>
 8 
 9     <action name="regist" class="zcx.controller.LoginAction" method="regist">
10 
11         <result name="success">/WEB-INF/content/welcome.jsp</result>
12 
13     </action>
14 
15     <!--处理所有的action-->
16 
17     <action name="*">
18 
19         <!--返回对应的页面-->
20 
21         <result>/WEB-INF/content/{1}.jsp</result>
22 
23     </action>
24 
25 </package>

 

 

实现regist方法

 1 public String regist() throws Exception
 2 
 3 {
 4 
 5     ActionContext.getContext().getSession()
 6 
 7             .put("user" , getUser());
 8 
 9     addActionMessage("恭喜您," + getUser() + ",您已经注册成功!");
10 
11     return SUCCESS;
12 
13 }

 

 

6.3通配符

 1 <package name="zcx" extends="struts-default">
 2 
 3     <!-- 使用模式字符串定义Action的name,指定所有以Action结尾的请求,
 4 
 5     都可用LoginRegistAction来处理,method属性使用{1},
 6 
 7     这个{1}代表进行模式匹配时第一个*所代替的字符串 -->
 8 
 9     <action name="*Action" class="zcx.controller.LoginAction"
10 
11             method="{1}">
12 
13         <!-- 定义逻辑视图和物理视图之间的映射关系 -->
14 
15         <result name="error">/WEB-INF/content/error.jsp</result>
16 
17         <result>/WEB-INF/content/welcome.jsp</result>
18 
19     </action>
20 
21     <action name="*">
22 
23         <result>/WEB-INF/content/{1}.jsp</result>
24 
25     </action>
26 
27 </package>

 

action result type

解压:struts2-core-2.3.16.3.jar文件,找到里面的struts.default文件可查看详细结果类型配置

Redirect:重定向到其他页面;同时可以使用表达式:test.action?getdata=${input.name}

RedirectAction:重定向到其他action

 

全局result

对所有action都有效。比如,若是系统出错需要跳转到一个页面,可以使用全局result

 

posted @ 2016-06-04 22:23  逆风飞行  阅读(144)  评论(0编辑  收藏  举报